<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-05-06
* Time: 17:50
*/
namespace App\Console\Sales;
use App\Lock\ApplicationLock;
use App\Repository\CityRepository;
use App\Repository\ProfileAdBoardPlacementRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class RotateProfileAdBoardPlacementsCommand extends Command
{
const ROTATIONS_PER_HOUR = 30;
public function __construct(
protected CityRepository $cityRepository,
protected ProfileAdBoardPlacementRepository $placementRepository,
private ApplicationLock $applicationLock
)
{
parent::__construct();
}
protected function configure()
{
$this->setName('sales:profile:adboard:rotate');
$this->setAliases(['sales:placements:rotate']);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$lock = $this->applicationLock->acquireAdBoardPipeline();
if (null === $lock) {
$io->warning('Skipped: adboard pipeline lock is already acquired by another process.');
return Command::SUCCESS;
}
try {
$io->title('Rotating profile adboard placements');
$io->progressStart();
foreach ($this->cityRepository->iterateAll() as $city) {
$this->placementRepository->rotateAdBoardStandardPositions($city, self::ROTATIONS_PER_HOUR);
$this->placementRepository->rotateAdBoardVipPositions($city, self::ROTATIONS_PER_HOUR);
$this->placementRepository->rotateAdBoardUltraVipPositions($city, self::ROTATIONS_PER_HOUR);
$io->progressAdvance();
}
$io->progressFinish();
$io->success('Success rotating profile placements');
} finally {
$lock->release();
}
return Command::SUCCESS;
}
}