Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CleanTempDatabaseCommand
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 getAllowedAppEnvs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
12
 unlink
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Command;
4
5use Doctrine\Common\Collections\Criteria;
6use Olz\Command\Common\OlzCommand;
7use Olz\Entity\AuthRequest;
8use Olz\Entity\Counter;
9use Symfony\Component\Console\Attribute\AsCommand;
10use Symfony\Component\Console\Command\Command;
11use Symfony\Component\Console\Input\InputInterface;
12use Symfony\Component\Console\Output\OutputInterface;
13
14#[AsCommand(name: 'olz:clean-temp-database')]
15class 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}