src/Console/Sales/RotateProfileAdBoardPlacementsCommand.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-05-06
  5.  * Time: 17:50
  6.  */
  7. namespace App\Console\Sales;
  8. use App\Lock\ApplicationLock;
  9. use App\Repository\CityRepository;
  10. use App\Repository\ProfileAdBoardPlacementRepository;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15. class RotateProfileAdBoardPlacementsCommand extends Command
  16. {
  17.     const ROTATIONS_PER_HOUR = 30;
  18.     public function __construct(
  19.         protected CityRepository $cityRepository,
  20.         protected ProfileAdBoardPlacementRepository $placementRepository,
  21.         private ApplicationLock $applicationLock
  22.     )
  23.     {
  24.         parent::__construct();
  25.     }
  26.     protected function configure()
  27.     {
  28.         $this->setName('sales:profile:adboard:rotate');
  29.         $this->setAliases(['sales:placements:rotate']);
  30.     }
  31.     protected function execute(InputInterface $input, OutputInterface $output): int
  32.     {
  33.         $io = new SymfonyStyle($input, $output);
  34.         $lock = $this->applicationLock->acquireAdBoardPipeline();
  35.         if (null === $lock) {
  36.             $io->warning('Skipped: adboard pipeline lock is already acquired by another process.');
  37.             return Command::SUCCESS;
  38.         }
  39.         try {
  40.             $io->title('Rotating profile adboard placements');
  41.             $io->progressStart();
  42.             foreach ($this->cityRepository->iterateAll() as $city) {
  43.                 $this->placementRepository->rotateAdBoardStandardPositions($city, self::ROTATIONS_PER_HOUR);
  44.                 $this->placementRepository->rotateAdBoardVipPositions($city, self::ROTATIONS_PER_HOUR);
  45.                 $this->placementRepository->rotateAdBoardUltraVipPositions($city, self::ROTATIONS_PER_HOUR);
  46.                 $io->progressAdvance();
  47.             }
  48.             $io->progressFinish();
  49.             $io->success('Success rotating profile placements');
  50.         } finally {
  51.             $lock->release();
  52.         }
  53.         return Command::SUCCESS;
  54.     }
  55. }