Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CleanTempDatabaseCommand | |
0.00% |
0 / 71 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 1 |
| getAllowedAppEnvs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| cleanAccessTokens | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| cleanAuthRequests | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| cleanCounter | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| cleanForwardedEmails | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| cleanThrottlings | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 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\AccessToken; |
| 8 | use Olz\Entity\AuthRequest; |
| 9 | use Olz\Entity\Counter; |
| 10 | use Olz\Entity\ForwardedEmail; |
| 11 | use Olz\Entity\Throttling; |
| 12 | use Symfony\Component\Console\Attribute\AsCommand; |
| 13 | use Symfony\Component\Console\Command\Command; |
| 14 | use Symfony\Component\Console\Input\InputInterface; |
| 15 | use Symfony\Component\Console\Output\OutputInterface; |
| 16 | |
| 17 | #[AsCommand(name: 'olz:clean-temp-database')] |
| 18 | class CleanTempDatabaseCommand extends OlzCommand { |
| 19 | protected string $temp_realpath; |
| 20 | |
| 21 | protected \DateTime $three_years_ago; |
| 22 | |
| 23 | /** @return array<string> */ |
| 24 | protected function getAllowedAppEnvs(): array { |
| 25 | return ['dev', 'test', 'staging', 'prod']; |
| 26 | } |
| 27 | |
| 28 | protected function handle(InputInterface $input, OutputInterface $output): int { |
| 29 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
| 30 | $minus_three_years = \DateInterval::createFromDateString("-3 years"); |
| 31 | $this->three_years_ago = $now->add($minus_three_years); |
| 32 | |
| 33 | $this->cleanAccessTokens(); |
| 34 | $this->cleanAuthRequests(); |
| 35 | $this->cleanCounter(); |
| 36 | $this->cleanForwardedEmails(); |
| 37 | $this->cleanThrottlings(); |
| 38 | |
| 39 | return Command::SUCCESS; |
| 40 | } |
| 41 | |
| 42 | protected function cleanAccessTokens(): void { |
| 43 | $repo = $this->entityManager()->getRepository(AccessToken::class); |
| 44 | $criteria = Criteria::create() |
| 45 | ->where(Criteria::expr()->andX( |
| 46 | Criteria::expr()->lte('expires_at', $this->three_years_ago), |
| 47 | )) |
| 48 | ; |
| 49 | $entries = $repo->matching($criteria); |
| 50 | $num_entries = $entries->count(); |
| 51 | $this->log()->info("Cleaning up {$num_entries} access token entries..."); |
| 52 | foreach ($entries as $entry) { |
| 53 | $this->entityManager()->remove($entry); |
| 54 | } |
| 55 | $this->entityManager()->flush(); |
| 56 | } |
| 57 | |
| 58 | protected function cleanAuthRequests(): void { |
| 59 | $repo = $this->entityManager()->getRepository(AuthRequest::class); |
| 60 | $criteria = Criteria::create() |
| 61 | ->where(Criteria::expr()->andX( |
| 62 | Criteria::expr()->lte('timestamp', $this->three_years_ago), |
| 63 | )) |
| 64 | ; |
| 65 | $entries = $repo->matching($criteria); |
| 66 | $num_entries = $entries->count(); |
| 67 | $this->log()->info("Cleaning up {$num_entries} auth request entries..."); |
| 68 | foreach ($entries as $entry) { |
| 69 | $this->entityManager()->remove($entry); |
| 70 | } |
| 71 | $this->entityManager()->flush(); |
| 72 | } |
| 73 | |
| 74 | protected function cleanCounter(): void { |
| 75 | $repo = $this->entityManager()->getRepository(Counter::class); |
| 76 | $criteria = Criteria::create() |
| 77 | ->where(Criteria::expr()->andX( |
| 78 | Criteria::expr()->lte('date_range', $this->three_years_ago->format('Y-m-d')), |
| 79 | )) |
| 80 | ; |
| 81 | $entries = $repo->matching($criteria); |
| 82 | $num_entries = $entries->count(); |
| 83 | $this->log()->info("Cleaning up {$num_entries} counter entries..."); |
| 84 | foreach ($entries as $entry) { |
| 85 | $this->entityManager()->remove($entry); |
| 86 | } |
| 87 | $this->entityManager()->flush(); |
| 88 | } |
| 89 | |
| 90 | protected function cleanForwardedEmails(): void { |
| 91 | $repo = $this->entityManager()->getRepository(ForwardedEmail::class); |
| 92 | $criteria = Criteria::create() |
| 93 | ->where(Criteria::expr()->andX( |
| 94 | Criteria::expr()->lte('forwarded_at', $this->three_years_ago), |
| 95 | )) |
| 96 | ; |
| 97 | $entries = $repo->matching($criteria); |
| 98 | $num_entries = $entries->count(); |
| 99 | $this->log()->info("Cleaning up {$num_entries} forwarded email entries..."); |
| 100 | foreach ($entries as $entry) { |
| 101 | $this->entityManager()->remove($entry); |
| 102 | } |
| 103 | $this->entityManager()->flush(); |
| 104 | } |
| 105 | |
| 106 | protected function cleanThrottlings(): void { |
| 107 | $repo = $this->entityManager()->getRepository(Throttling::class); |
| 108 | $criteria = Criteria::create() |
| 109 | ->where(Criteria::expr()->andX( |
| 110 | Criteria::expr()->lte('last_occurrence', $this->three_years_ago), |
| 111 | )) |
| 112 | ; |
| 113 | $entries = $repo->matching($criteria); |
| 114 | $num_entries = $entries->count(); |
| 115 | $this->log()->info("Cleaning up {$num_entries} throttling entries..."); |
| 116 | foreach ($entries as $entry) { |
| 117 | $this->entityManager()->remove($entry); |
| 118 | } |
| 119 | $this->entityManager()->flush(); |
| 120 | } |
| 121 | |
| 122 | protected function unlink(string $path): void { |
| 123 | unlink($path); |
| 124 | } |
| 125 | |
| 126 | // @codeCoverageIgnoreEnd |
| 127 | } |