-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateCampaigns.php
More file actions
122 lines (110 loc) · 5.01 KB
/
Copy pathUpdateCampaigns.php
File metadata and controls
122 lines (110 loc) · 5.01 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace MatchBot\Application\Commands;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use GuzzleHttp\Exception\TransferException;
use MatchBot\Application\AssertionFailedException;
use MatchBot\Client\NotFoundException;
use MatchBot\Domain\Campaign;
use MatchBot\Domain\CampaignRepository;
use MatchBot\Domain\CampaignService;
use MatchBot\Domain\DomainException\DomainCurrencyMustNotChangeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'matchbot:update-campaigns',
description: 'Now just loads funds for campaigns; core data is pushed by Salesforce',
)]
class UpdateCampaigns extends LockingCommand
{
public function __construct(
private CampaignRepository $campaignRepository,
/** @var EntityManager|EntityManagerInterface $entityManager */
private EntityManagerInterface $entityManager,
private CampaignService $campaignService,
private LoggerInterface $logger,
) {
parent::__construct();
}
#[\Override]
protected function configure(): void
{
$this->addOption('all', null, InputOption::VALUE_NONE, 'Expands the update to ALL historic known campaigns');
}
#[\Override]
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('all')) {
/** @var Campaign[] $campaigns */
$campaigns = $this->campaignRepository->findAll();
} else {
$campaigns = $this->campaignRepository->findCampaignsWhereFundsNeedToBeUpToDate();
}
foreach ($campaigns as $campaign) {
$salesforceId = $campaign->getSalesforceId();
try {
$this->pull($campaign, $output);
} catch (NotFoundException) {
if (getenv('APP_ENV') === 'production') {
if ($campaign->getEndDate() < new \DateTime()) {
$this->logger->info(sprintf(
'Skipping unknown PRODUCTION campaign %s whose end date had passed – charity inactive?',
$salesforceId
));
} else {
// TODO perhaps make this a warning, so it shows on dashboards but not alarm channels, and
// also inactivate the Campaign? While we are leaving the data as-is, warning is too noisy.
$this->logger->info(sprintf(
'Skipping unknown PRODUCTION campaign %s – charity inactive or rejected offer?',
$salesforceId
));
}
} else {
// Chances are a sandbox refresh has led to this campaign being deleted in the Salesforce sandbox.
$output->writeln('Skipping unknown sandbox campaign ' . $salesforceId);
}
} catch (DomainCurrencyMustNotChangeException $exception) {
$output->writeln('Skipping invalid currency change campaign ' . $salesforceId);
} catch (AssertionFailedException $exception) {
$output->writeln('Skipping campaign due to exception: ' . $exception->getMessage() . ", " . $salesforceId);
} catch (TransferException $exception) {
$this->logger->info(sprintf(
'Retrying campaign %s due to transfer error "%s"',
$salesforceId,
$exception->getMessage(),
));
try {
$this->pull($campaign, $output);
} catch (TransferException $retryException) {
$transferError = sprintf(
'Skipping campaign %s due to 2nd transfer error "%s"',
$salesforceId,
$retryException->getMessage(),
);
$output->writeln($transferError);
$this->logger->warning($transferError);
}
}
}
// This task is expected to bulk change lots of campaigns + funds in some cases.
// After the loop is the most efficient time to clear the result
// cache so future processes see all the new data straight away.
$this->entityManager->getConfiguration()->getResultCache()?->clear();
return 0;
}
/**
* @throws NotFoundException
* @throws TransferException
* @throws DomainCurrencyMustNotChangeException
* @throws AssertionFailedException if data given does not fit in our charity model.
*/
protected function pull(Campaign $campaign, OutputInterface $output): void
{
$this->campaignService->pullFundsAndUpdateStats($campaign);
$output->writeln('Updated campaign ' . $campaign->getSalesforceId());
}
}