Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CleanTempDatabaseCommand | |
0.00% |
0 / 30 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
getAllowedAppEnvs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
12 | |||
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 | |
18 | /** @return array<string> */ |
19 | protected function getAllowedAppEnvs(): array { |
20 | return ['dev', 'test', 'staging', 'prod']; |
21 | } |
22 | |
23 | protected function handle(InputInterface $input, OutputInterface $output): int { |
24 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
25 | $minus_three_years = \DateInterval::createFromDateString("-3 years"); |
26 | $three_years_ago = $now->add($minus_three_years); |
27 | |
28 | // Remove access tokens |
29 | $auth_request_repo = $this->entityManager()->getRepository(AuthRequest::class); |
30 | $criteria = Criteria::create() |
31 | ->where(Criteria::expr()->andX( |
32 | Criteria::expr()->lte('timestamp', $three_years_ago), |
33 | )) |
34 | ; |
35 | $auth_requests = $auth_request_repo->matching($criteria); |
36 | $num_auth_requests = $auth_requests->count(); |
37 | $this->log()->info("Cleaning up {$num_auth_requests} auth request entries..."); |
38 | foreach ($auth_requests as $auth_request) { |
39 | $this->entityManager()->remove($auth_request); |
40 | } |
41 | $this->entityManager()->flush(); |
42 | |
43 | // Remove counter |
44 | $counter_repo = $this->entityManager()->getRepository(Counter::class); |
45 | $criteria = Criteria::create() |
46 | ->where(Criteria::expr()->andX( |
47 | Criteria::expr()->lte('date_range', $three_years_ago->format('Y-m-d')), |
48 | )) |
49 | ; |
50 | $counters = $counter_repo->matching($criteria); |
51 | $num_counters = $counters->count(); |
52 | $this->log()->info("Cleaning up {$num_counters} counter entries..."); |
53 | foreach ($counters as $counter) { |
54 | $this->entityManager()->remove($counter); |
55 | } |
56 | $this->entityManager()->flush(); |
57 | |
58 | return Command::SUCCESS; |
59 | } |
60 | |
61 | protected function unlink(string $path): void { |
62 | unlink($path); |
63 | } |
64 | |
65 | // @codeCoverageIgnoreEnd |
66 | } |