From 1294a411b6687975d90f2fe54a75e9963710da79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Beli=C3=ABn?= Date: Sun, 3 Sep 2023 18:41:50 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Split=20welcome=20process?= =?UTF-8?q?=20per=20continent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Command/UpdateCommand.php | 94 ++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/src/Command/UpdateCommand.php b/src/Command/UpdateCommand.php index bfc6591a3..201ac4714 100644 --- a/src/Command/UpdateCommand.php +++ b/src/Command/UpdateCommand.php @@ -5,14 +5,17 @@ use App\Entity\Region; use App\Service\RegionsProvider; use Doctrine\ORM\EntityManagerInterface; -use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Validator\Constraints\Callback; +use Symfony\Component\Validator\Context\ExecutionContextInterface; +use Symfony\Component\Validator\Validator\ValidatorInterface; #[AsCommand( name: 'welcome:update', @@ -21,64 +24,75 @@ class UpdateCommand extends Command { public function __construct( + private readonly ValidatorInterface $validator, private readonly RegionsProvider $provider, - private readonly EntityManagerInterface $entityManager, - private readonly CacheItemPoolInterface $cache + private readonly EntityManagerInterface $entityManager ) { parent::__construct(); } protected function configure(): void { - $this->addOption( - 'force', - 'f', - InputOption::VALUE_NONE, - 'Force process (even if it has already been processed today)' - ); + $this + ->addArgument('continent', InputArgument::REQUIRED, 'Continent') + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force process (even if it has already been processed today)'); + } + + protected function initialize(InputInterface $input, OutputInterface $output) + { + $validate = $this->validator->validate($input->getArgument('continent'), new Callback(function (string $value, ExecutionContextInterface $context) { + $regions = $this->provider->getRegions(); + + if (!isset($regions[$value]) || 0 === count($regions[$value])) { + $context + ->buildViolation(sprintf('Continent "%s" does not exist.', $value)) + ->addViolation(); + } + })); + + if ($validate->count() > 0) { + throw new \ErrorException($validate->get(0)->getMessage()); + } } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $deletedUsersCommand = $this->getApplication()->find('osm:deleted-users'); - $deletedUsersCommand->run(new ArrayInput([]), $output); + $continent = $input->getArgument('continent'); $lastUpdate = []; $regions = $this->provider->getRegions(); - foreach ($regions as $continent => $group) { - foreach ($group as $key => $region) { - /** @var Region|null */ - $r = $this->entityManager->find(Region::class, $key); - $lastUpdate = null === $r ? null : $r->getLastUpdate(); - - $io->title(sprintf('%s (%s)', $region['name'], date('Y-m-d'))); - - if (true === $input->getOption('force') || null === $lastUpdate || $lastUpdate->format('Y-m-d') < date('Y-m-d')) { - if (null === $lastUpdate) { - // If there never was an update, get new mappers from the last 5 days - $date = (new \DateTime())->sub(new \DateInterval('P5D'))->format('Y-m-d'); - $io->note(sprintf('Cache is not set, get new mappers from %s.', $date)); - } elseif (true === $input->getOption('force') && $lastUpdate->format('Y-m-d') === date('Y-m-d')) { - // If last update was today and process is forced, get new mappers from yesterday - $date = $lastUpdate->sub(new \DateInterval('P1D'))->format('Y-m-d'); - $io->note(sprintf('Get new mappers from %s (forced).', $date)); - } else { - // Get new mappers from the last update date - $date = $lastUpdate->format('Y-m-d'); - $io->note(sprintf('Get new mappers from %s.', $date)); - } - - try { - $this->process($key, $date, $output); - } catch (\Exception $e) { - $io->error($e->getMessage()); - } + foreach ($regions[$continent] as $key => $region) { + /** @var Region|null */ + $r = $this->entityManager->find(Region::class, $key); + $lastUpdate = null === $r ? null : $r->getLastUpdate(); + + $io->title(sprintf('%s (%s)', $region['name'], date('Y-m-d'))); + + if (true === $input->getOption('force') || null === $lastUpdate || $lastUpdate->format('Y-m-d') < date('Y-m-d')) { + if (null === $lastUpdate) { + // If there never was an update, get new mappers from the last 5 days + $date = (new \DateTime())->sub(new \DateInterval('P5D'))->format('Y-m-d'); + $io->note(sprintf('Cache is not set, get new mappers from %s.', $date)); + } elseif (true === $input->getOption('force') && $lastUpdate->format('Y-m-d') === date('Y-m-d')) { + // If last update was today and process is forced, get new mappers from yesterday + $date = $lastUpdate->sub(new \DateInterval('P1D'))->format('Y-m-d'); + $io->note(sprintf('Get new mappers from %s (forced).', $date)); } else { - $io->note('Skip, already processed.'); + // Get new mappers from the last update date + $date = $lastUpdate->format('Y-m-d'); + $io->note(sprintf('Get new mappers from %s.', $date)); + } + + try { + $this->process($key, $date, $output); + } catch (\Exception $e) { + $io->error($e->getMessage()); } + } else { + $io->note('Skip, already processed.'); } }