-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPullIndividualCampaignFromSF.php
More file actions
78 lines (66 loc) · 2.83 KB
/
Copy pathPullIndividualCampaignFromSF.php
File metadata and controls
78 lines (66 loc) · 2.83 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
<?php
declare(strict_types=1);
namespace MatchBot\Application\Commands;
use GuzzleHttp\Exception\RequestException;
use MatchBot\Application\Assertion;
use MatchBot\Application\Environment;
use MatchBot\Domain\Campaign;
use MatchBot\Domain\CampaignRepository;
use MatchBot\Domain\CampaignService;
use MatchBot\Domain\Salesforce18Id;
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-campaign-from-sf',
description: 'Pulls an in individual campaigns) from Salesforce into the
matchbot db. May be used for manual testing on dev machines, also to force an update in any environment in case the automatic push from SF failed or wasn\'t set to run when the last change happened'
)]
class PullIndividualCampaignFromSF extends LockingCommand
{
public function __construct(
private Environment $environment,
private CampaignRepository $campaignRepository,
private CampaignService $campaignService,
) {
parent::__construct();
}
#[\Override]
public function configure(): void
{
$this->addArgument(
'CampaignSFID',
InputArgument::REQUIRED,
'18 Character Salesforce ID of the campaign, as found in the donate page URI etc',
);
}
#[\Override]
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
Assertion::notEq($this->environment, Environment::Production);
// @phpstan-ignore cast.string
$campaignId = Salesforce18Id::ofCampaign((string) $input->getArgument('CampaignSFID'));
$campaign = $this->campaignRepository->findOneBySalesforceId($campaignId);
if ($campaign) {
$this->campaignRepository->updateFromSf($campaign);
$output->writeln("Campaign {$this->campaignFullName($campaign)} found locally, updated from SF");
} else {
try {
$campaign = $this->campaignRepository->pullNewFromSf($campaignId);
} catch (RequestException $e) {
$output->writeln("<error>Request Exception from Salesforce, please check Campaign ID</error>");
$output->writeln("<error>{$e->getMessage()}</error>");
return 1;
}
$output->writeln("Campaign {$this->campaignFullName($campaign)} not found, pulled from SF");
}
$this->campaignService->pullFundsAndUpdateStats($campaign);
$output->writeln("Fund data updated for campaign " . $this->campaignFullName($campaign));
return 0;
}
private function campaignFullName(Campaign $campaign): string
{
return "{$campaign->getCharity()->getName()}: {$campaign->getCampaignName()}";
}
}