-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectRegularGivingForTest.php
86 lines (67 loc) · 2.83 KB
/
CollectRegularGivingForTest.php
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
<?php
declare(strict_types=1);
namespace MatchBot\Application\Actions;
use JetBrains\PhpStorm\Pure;
use MatchBot\Application\Commands\TakeRegularGivingDonations;
use MatchBot\Application\Environment;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpNotFoundException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Lock\LockFactory;
/**
* To test on dev machine, send GET request to e.g.
* http://localhost:30030/v1/test-donation-collection-for-date/
*/
class CollectRegularGivingForTest extends Action
{
#[Pure]
public function __construct(
LoggerInterface $logger,
private TakeRegularGivingDonations $cliCommand,
private LockFactory $lockFactory,
private Environment $environment,
) {
parent::__construct($logger);
}
protected function action(Request $request, Response $response, array $args): Response
{
if ($this->environment === Environment::Production) {
throw new HttpNotFoundException($request);
}
$secret = (string)$request->getQueryParams()['secret'];
$stream = $response->getBody();
if (!password_verify($secret, '$2y$12$JgCEyBfFQKBrIYHs1PNobef3aMswiWPHvKX/cWHWVePEOfLHRp2Oa')) {
$stream->write(
'Bad or missing secret - this command is only for use by authorized people within BG test/dev environments'
);
return $response;
}
try {
$date = new \DateTimeImmutable((string) $args['date']);
} catch (\DateMalformedStringException $e) {
$stream->write($e->getMessage());
return $response;
}
$this->cliCommand->setLockFactory($this->lockFactory);
$this->cliCommand->setLogger($this->logger);
$commandTest = new CommandTester($this->cliCommand);
$commandTest->execute(['--simulated-date' => $date->format('c')]);
$commandOutput = $commandTest->getDisplay();
$actualNow = new \DateTimeImmutable();
$response = $response->withHeader('Content-Type', 'text/plain');
$stream->write(<<<EOF
Big Give Matchbot
============================================================================================
Actual current date: {$actualNow->format('D, d M Y H:i:s')}
Running regular giving collection process with simulated timestamp {$date->format('D, d M Y H:i:s')}
This endpoint is only available in test/dev environments, not in production.
--------------
EOF
);
$stream->write($commandOutput);
$stream->write("--------------\n");
return $response;
}
}