Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.36% |
38 / 44 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CleandbCommand | |
86.36% |
38 / 44 |
|
66.67% |
2 / 3 |
13.43 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
6 | |||
| processWaypoint | |
73.91% |
17 / 23 |
|
0.00% |
0 / 1 |
6.64 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Command; |
| 6 | |
| 7 | use App\Entity\Waypoint; |
| 8 | use App\Repository\WaypointRepository; |
| 9 | use App\Service\WayPointHelper; |
| 10 | use Doctrine\ORM\EntityManagerInterface; |
| 11 | use Symfony\Component\Console\Attribute\AsCommand; |
| 12 | use Symfony\Component\Console\Command\Command; |
| 13 | use Symfony\Component\Console\Helper\ProgressBar; |
| 14 | use Symfony\Component\Console\Input\InputInterface; |
| 15 | use Symfony\Component\Console\Output\OutputInterface; |
| 16 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 17 | |
| 18 | #[AsCommand( |
| 19 | name: 'app:cleandb', |
| 20 | description: 'Cleanup the database' |
| 21 | )] |
| 22 | class CleandbCommand extends Command |
| 23 | { |
| 24 | public function __construct( |
| 25 | private readonly EntityManagerInterface $entityManager, |
| 26 | private readonly WaypointRepository $waypointRepository, |
| 27 | private readonly WayPointHelper $wayPointHelper |
| 28 | ) |
| 29 | { |
| 30 | parent::__construct(); |
| 31 | } |
| 32 | |
| 33 | protected function execute( |
| 34 | InputInterface $input, |
| 35 | OutputInterface $output |
| 36 | ): int |
| 37 | { |
| 38 | $errorCount = 0; |
| 39 | $warningCount = 0; |
| 40 | $io = new SymfonyStyle($input, $output); |
| 41 | |
| 42 | $waypoints = $this->waypointRepository->findAll(); |
| 43 | |
| 44 | $progressBar = new ProgressBar($output, count($waypoints)); |
| 45 | |
| 46 | foreach ($waypoints as $waypoint) { |
| 47 | [$errs, $warns] = $this->processWaypoint($waypoint, $io); |
| 48 | $errorCount += $errs; |
| 49 | $warningCount += $warns; |
| 50 | $progressBar->advance(); |
| 51 | } |
| 52 | |
| 53 | $this->entityManager->flush(); |
| 54 | |
| 55 | $progressBar->finish(); |
| 56 | |
| 57 | if ($errorCount !== 0) { |
| 58 | $io->error(sprintf('There have been %d errors', $errorCount)); |
| 59 | } |
| 60 | |
| 61 | if ($warningCount !== 0) { |
| 62 | $io->warning(sprintf('There have been %d warnings', $warningCount)); |
| 63 | } |
| 64 | |
| 65 | if (!$errorCount && !$warningCount) { |
| 66 | $io->success('Database is clean.'); |
| 67 | |
| 68 | return Command::SUCCESS; |
| 69 | } |
| 70 | |
| 71 | return Command::FAILURE; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return array{int, int} |
| 76 | */ |
| 77 | private function processWaypoint(Waypoint $waypoint, SymfonyStyle $io): array |
| 78 | { |
| 79 | $errorCount = 0; |
| 80 | $warningCount = 0; |
| 81 | |
| 82 | if (!$waypoint->getLat() || !$waypoint->getLon()) { |
| 83 | $io->error(sprintf('"%s" missing location', $waypoint->getName())); |
| 84 | ++$errorCount; |
| 85 | $this->entityManager->remove($waypoint); |
| 86 | } |
| 87 | |
| 88 | if (!$waypoint->getName()) { |
| 89 | $io->error(sprintf('"%s" missing name', $waypoint->getName())); |
| 90 | ++$errorCount; |
| 91 | $this->entityManager->remove($waypoint); |
| 92 | } |
| 93 | |
| 94 | if ('undefined' === $waypoint->getName()) { |
| 95 | $io->error(sprintf('"%s" name', $waypoint->getName())); |
| 96 | ++$errorCount; |
| 97 | $this->entityManager->remove($waypoint); |
| 98 | } |
| 99 | |
| 100 | $cleanName = $this->wayPointHelper->cleanName((string) $waypoint->getName()); |
| 101 | |
| 102 | if ($waypoint->getName() !== $cleanName) { |
| 103 | $io->warning( |
| 104 | sprintf('"%s" dirty title "%s" clean title', $waypoint->getName(), $cleanName) |
| 105 | ); |
| 106 | ++$warningCount; |
| 107 | $waypoint->setName($cleanName); |
| 108 | $this->entityManager->persist($waypoint); |
| 109 | } |
| 110 | |
| 111 | return [$errorCount, $warningCount]; |
| 112 | } |
| 113 | } |