Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
48 / 48 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
OnContinuouslyCommand | |
100.00% |
48 / 48 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
getAllowedAppEnvs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
2 | |||
shouldSendDailyMailNow | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Olz\Command; |
4 | |
5 | use Olz\Command\Common\OlzCommand; |
6 | use Olz\Entity\Throttling; |
7 | use Symfony\Component\Console\Attribute\AsCommand; |
8 | use Symfony\Component\Console\Command\Command; |
9 | use Symfony\Component\Console\Input\ArrayInput; |
10 | use Symfony\Component\Console\Input\InputInterface; |
11 | use Symfony\Component\Console\Output\OutputInterface; |
12 | |
13 | #[AsCommand(name: 'olz:on-continuously')] |
14 | class OnContinuouslyCommand extends OlzCommand { |
15 | /** @return array<string> */ |
16 | protected function getAllowedAppEnvs(): array { |
17 | return ['dev', 'test', 'staging', 'prod']; |
18 | } |
19 | |
20 | protected function handle(InputInterface $input, OutputInterface $output): int { |
21 | set_time_limit(4000); |
22 | ignore_user_abort(true); |
23 | |
24 | $this->logAndOutput("Running continuously...", level: 'debug'); |
25 | $throttling_repo = $this->entityManager()->getRepository(Throttling::class); |
26 | $throttling_repo->recordOccurrenceOf('on_continuously', $this->dateUtils()->getIsoNow()); |
27 | |
28 | $this->logAndOutput("Continuously processing email...", level: 'debug'); |
29 | $this->symfonyUtils()->callCommand( |
30 | 'olz:process-email', |
31 | new ArrayInput([]), |
32 | $output, |
33 | ); |
34 | |
35 | if ($this->shouldSendDailyMailNow()) { |
36 | $this->logAndOutput("Sending daily mail at {$this->dateUtils()->getIsoNow()}...", level: 'debug'); |
37 | $throttling_repo->recordOccurrenceOf('daily_notifications', $this->dateUtils()->getIsoNow()); |
38 | |
39 | $this->symfonyUtils()->callCommand( |
40 | 'olz:send-daily-notifications', |
41 | new ArrayInput([]), |
42 | $output, |
43 | ); |
44 | } |
45 | |
46 | $this->logAndOutput("Stopping workers...", level: 'debug'); |
47 | $this->symfonyUtils()->callCommand( |
48 | 'messenger:stop-workers', |
49 | new ArrayInput([]), |
50 | $output, |
51 | ); |
52 | $this->logAndOutput("Consume messages...", level: 'debug'); |
53 | $this->symfonyUtils()->callCommand( |
54 | 'messenger:consume', |
55 | new ArrayInput([ |
56 | 'receivers' => ['async'], |
57 | '--no-reset' => '--no-reset', |
58 | ]), |
59 | $output, |
60 | ); |
61 | |
62 | $this->logAndOutput("Ran continuously.", level: 'debug'); |
63 | return Command::SUCCESS; |
64 | } |
65 | |
66 | public function shouldSendDailyMailNow(): bool { |
67 | $daily_notifications_time = '16:27:00'; |
68 | $throttling_repo = $this->entityManager()->getRepository(Throttling::class); |
69 | $last_daily_notifications = $throttling_repo->getLastOccurrenceOf('daily_notifications'); |
70 | $is_too_soon = false; |
71 | if ($last_daily_notifications) { |
72 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
73 | // Consider daylight saving change date => not 23 hours! |
74 | $min_interval = \DateInterval::createFromDateString('+22 hours'); |
75 | $min_now = $last_daily_notifications->add($min_interval); |
76 | $is_too_soon = $now < $min_now; |
77 | } |
78 | $is_right_time_of_day = $this->dateUtils()->getCurrentDateInFormat('H:i:s') >= $daily_notifications_time; |
79 | return !$is_too_soon && $is_right_time_of_day; |
80 | } |
81 | } |