-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCancelStaleDonationFundTips.php
More file actions
66 lines (55 loc) · 2.46 KB
/
CancelStaleDonationFundTips.php
File metadata and controls
66 lines (55 loc) · 2.46 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
<?php
declare(strict_types=1);
namespace MatchBot\Application\Commands;
use Doctrine\ORM\EntityManagerInterface;
use MatchBot\Application\Assertion;
use MatchBot\Application\Environment;
use MatchBot\Domain\DonationRepository;
use MatchBot\Domain\DonationService;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Finds and cancels donation-funds type tips that have been uncollected for 14 days or longer. These
* are likely to be from donors who either made a mistake when choosing a tip amount in the transfer form,
* or selected a tip but have since forgotten or changed their mind.
*
* In any case, cancelling the tip donation will allow them to go through the form again and make a new choice to
* tip us anything amount they prefer, or nothing.
*/
#[AsCommand(
name: 'matchbot:cancel-stale-donation-fund-tips',
description: 'Finds and cancels donation-funds type tips that have been uncollected for 14 days or longer.'
)]
class CancelStaleDonationFundTips extends LockingCommand
{
public function __construct(
private DonationRepository $donationRepository,
private DonationService $donationService,
private EntityManagerInterface $entityManager,
private ClockInterface $clock,
private Environment $environment,
) {
parent::__construct();
}
#[\Override]
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
// Cancel tips quicker outside prod to allow for easier testing:
$twoWeeks = new \DateInterval('P14D');
$tenMinutes = new \DateInterval('PT10M');
$cancelationDelay = $this->environment == Environment::Production ? $twoWeeks : $tenMinutes;
$staleDonationTipsUUIDS = $this->donationRepository->findStaleDonationFundsTips($this->clock->now(), $cancelationDelay);
foreach ($staleDonationTipsUUIDS as $tipDonationUUID) {
$this->entityManager->wrapInTransaction(function () use ($tipDonationUUID): void {
$donation = $this->donationRepository->findAndLockOneByUUID($tipDonationUUID);
Assertion::notNull($donation);
$this->donationService->cancel($donation);
});
$output->writeln("Cancelled tip donation {$tipDonationUUID->toString()}");
}
$this->entityManager->flush();
return 0;
}
}