-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPullMetaCampaignFromSF.php
More file actions
88 lines (74 loc) · 3.07 KB
/
Copy pathPullMetaCampaignFromSF.php
File metadata and controls
88 lines (74 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace MatchBot\Application\Commands;
use Doctrine\ORM\EntityManagerInterface;
use MatchBot\Client\Campaign as CampaignClient;
use MatchBot\Domain\CampaignRepository;
use MatchBot\Domain\CampaignService;
use MatchBot\Domain\MetaCampaign;
use MatchBot\Domain\MetaCampaignRepository;
use MatchBot\Domain\MetaCampaignSlug;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
*
*/
#[AsCommand(
name: 'matchbot:pull-meta-campaign-from-sf',
description: 'Pulls already-known children of meta-campaign from Salesforce into the matchbot DB.'
)]
class PullMetaCampaignFromSF extends LockingCommand
{
public function __construct(
private CampaignRepository $campaignRepository,
private MetaCampaignRepository $metaCampaignRepository,
private CampaignService $campaignService,
private EntityManagerInterface $entityManager,
private CampaignClient $campaignClient,
private ClockInterface $clock,
) {
parent::__construct();
}
#[\Override]
public function configure(): void
{
$this->addArgument('metaCampaignSlug', InputArgument::REQUIRED);
}
public function pullCharityCampaigns(MetaCampaignSlug $metaCampaginSlug, OutputInterface $output): void
{
['newFetchCount' => $newFetchCount, 'updatedCount' => $updatedCount, 'campaigns' => $campaigns] =
$this->campaignRepository->fetchAlreadyKnownChildrenForMetaCampaign($metaCampaginSlug);
$total = $newFetchCount + $updatedCount;
$i = 0;
foreach ($campaigns as $campaign) {
$i++;
$output->writeln("Pulling funds for ($i of $total) '{$campaign->getCampaignName()}'");
$this->campaignService->pullFundsAndUpdateStats($campaign);
}
$output->writeln("Fetched $total campaigns total from Salesforce for '$metaCampaginSlug->slug'");
$output->writeln("$newFetchCount new campaigns added to DB, $updatedCount campaigns updated");
}
#[\Override]
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$slug = $input->getArgument('metaCampaignSlug');
\assert(is_string($slug));
$slug = MetaCampaignSlug::of($slug);
$existingMetaCampaignInDB = $this->metaCampaignRepository->getBySlug($slug);
$data = $this->campaignClient->getBySlug($slug);
if (\is_null($existingMetaCampaignInDB)) {
$metaCampaign = MetaCampaign::fromSfCampaignData($slug, $data);
$metaCampaign->setSalesforceLastPull(\DateTime::createFromInterface($this->clock->now()));
// create new one from SF data
$this->entityManager->persist($metaCampaign);
} else {
$existingMetaCampaignInDB->updateFromSfData($data);
}
$this->entityManager->flush();
$this->pullCharityCampaigns($slug, $output);
return 0;
}
}