Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
CleanTempDatabaseCommand | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
5 | |
0.00% |
0 / 1 |
getAllowedAppEnvs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
3 | |||
unlink | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Command; |
4 | |
5 | use Doctrine\Common\Collections\Criteria; |
6 | use Olz\Command\Common\OlzCommand; |
7 | use Olz\Entity\AuthRequest; |
8 | use Olz\Entity\Counter; |
9 | use Symfony\Component\Console\Attribute\AsCommand; |
10 | use Symfony\Component\Console\Command\Command; |
11 | use Symfony\Component\Console\Input\InputInterface; |
12 | use Symfony\Component\Console\Output\OutputInterface; |
13 | |
14 | #[AsCommand(name: 'olz:clean-temp-database')] |
15 | class CleanTempDatabaseCommand extends OlzCommand { |
16 | protected string $temp_realpath; |
17 | protected int $clean_older_than; |
18 | |
19 | /** @return array<string> */ |
20 | protected function getAllowedAppEnvs(): array { |
21 | return ['dev', 'test', 'staging', 'prod']; |
22 | } |
23 | |
24 | protected function handle(InputInterface $input, OutputInterface $output): int { |
25 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
26 | $minus_three_years = \DateInterval::createFromDateString("-3 years"); |
27 | $three_years_ago = $now->add($minus_three_years); |
28 | |
29 | // Remove access tokens |
30 | $auth_request_repo = $this->entityManager()->getRepository(AuthRequest::class); |
31 | $criteria = Criteria::create() |
32 | ->where(Criteria::expr()->andX( |
33 | Criteria::expr()->lte('timestamp', $three_years_ago), |
34 | )) |
35 | ; |
36 | $auth_requests = $auth_request_repo->matching($criteria); |
37 | $num_auth_requests = $auth_requests->count(); |
38 | $this->log()->info("Cleaning up {$num_auth_requests} auth request entries..."); |
39 | foreach ($auth_requests as $auth_request) { |
40 | $this->entityManager()->remove($auth_request); |
41 | } |
42 | $this->entityManager()->flush(); |
43 | |
44 | // Remove counter |
45 | $counter_repo = $this->entityManager()->getRepository(Counter::class); |
46 | $criteria = Criteria::create() |
47 | ->where(Criteria::expr()->andX( |
48 | Criteria::expr()->lte('date_range', $three_years_ago->format('Y-m-d')), |
49 | )) |
50 | ; |
51 | $counters = $counter_repo->matching($criteria); |
52 | $num_counters = $counters->count(); |
53 | $this->log()->info("Cleaning up {$num_counters} counter entries..."); |
54 | foreach ($counters as $counter) { |
55 | $this->entityManager()->remove($counter); |
56 | } |
57 | $this->entityManager()->flush(); |
58 | |
59 | return Command::SUCCESS; |
60 | } |
61 | |
62 | protected function unlink(string $path): void { |
63 | unlink($path); |
64 | } |
65 | |
66 | // @codeCoverageIgnoreEnd |
67 | } |