Skip to content

Commit 66d64c4

Browse files
committed
Add anonymise-donations CLI command
Sometimes we need to run the anonymiser in order to investigate issues in our application. This adds a command to allow it from the developer environment. Ticket: https://phabricator.wikimedia.org/T400323
1 parent 180c4d9 commit 66d64c4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cli/AnonymiseDonationsCommand.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare( strict_types = 1 );
4+
5+
namespace WMDE\Fundraising\Frontend\Cli;
6+
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use WMDE\Clock\SystemClock;
13+
use WMDE\Fundraising\DonationContext\DataAccess\DatabaseDonationAnonymizer;
14+
use WMDE\Fundraising\DonationContext\Domain\AnonymizationException;
15+
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
16+
17+
/**
18+
* This is for anonymising a donation in your local environment, to use it do the following:
19+
*
20+
* 1. Create a donation using the form
21+
* 2. Run: `docker compose exec app php bin/console app:anonymise-donations`
22+
*/
23+
#[AsCommand( name: 'app:anonymise-donations' )]
24+
class AnonymiseDonationsCommand extends Command {
25+
26+
public function __construct( private readonly FunFunFactory $ffFactory ) {
27+
parent::__construct();
28+
}
29+
30+
protected function execute( InputInterface $input, OutputInterface $output ): int {
31+
$donationAnonymizer = new DatabaseDonationAnonymizer(
32+
$this->ffFactory->getDonationRepository(),
33+
$this->ffFactory->getEntityManager(),
34+
new SystemClock(),
35+
new \DateInterval( 'P2D' )
36+
);
37+
38+
try {
39+
$donationAnonymizer->anonymizeAll();
40+
} catch ( AnonymizationException | \InvalidArgumentException $e ) {
41+
if ( get_class( $e ) == \InvalidArgumentException::class ) {
42+
return Command::INVALID;
43+
}
44+
return Command::FAILURE;
45+
}
46+
47+
return Command::SUCCESS;
48+
}
49+
}

0 commit comments

Comments
 (0)