Skip to content

Commit

Permalink
♻️ Split welcome process per continent
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelien committed Sep 3, 2023
1 parent bc1a569 commit 1294a41
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.');
}
}

Expand Down

0 comments on commit 1294a41

Please sign in to comment.