src/App/VideoProcessing/Processing/PopSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\VideoProcessing\Processing;
  3. use App\Entity\Deal;
  4. use App\Entity\Project;
  5. use App\Entity\ShopVideo;
  6. use App\RabbitMq\Processing\PopProcessingEvent;
  7. use App\RabbitMq\Processing\PushProcessingEvent;
  8. use App\RabbitMq\TasksPool;
  9. use App\VideoProcessing\PurgeVideoHandler;
  10. use App\VideoProcessing\VideoProcessingHandler;
  11. use Doctrine\Common\Util\ClassUtils;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use InvalidArgumentException;
  14. use LogicException;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use UploadsBundle\RabbitMq\Handler\FileHandlerFactoryHandler;
  19. /**
  20.  * Class PopListener
  21.  *
  22.  * So take my strong advice, just remember to always think twice.. (c) Michael
  23.  */
  24. class PopSubscriber implements EventSubscriberInterface
  25. {
  26.     private VideoProcessingHandler $videoProcessingHandler;
  27.     private ManagerRegistry $registry;
  28.     private EventDispatcherInterface $eventDispatcher;
  29.     private TasksPool $tasksPool;
  30.     private LoggerInterface $logger;
  31.     private bool $enabled true;
  32.     public function __construct(ManagerRegistry $registryVideoProcessingHandler $videoProcessingHandlerLoggerInterface $loggerEventDispatcherInterface $eventDispatcherTasksPool $tasksPool)
  33.     {
  34.         $this->registry $registry;
  35.         $this->videoProcessingHandler $videoProcessingHandler;
  36.         $this->logger $logger;
  37.         $this->eventDispatcher $eventDispatcher;
  38.         $this->tasksPool $tasksPool;
  39.     }
  40.     public function onPop(PopProcessingEvent $event): void
  41.     {
  42.         if (!$this->isEnabled()) {
  43.             return;
  44.         }
  45.         if ($event->getService() !== FileHandlerFactoryHandler::HANDLER) {
  46.             return;
  47.         }
  48.         /** @var string[] $arguments */
  49.         $arguments $event->getArguments();
  50.         if (!isset($arguments[0]) || !in_array($arguments[0], ['deal_video''project_video''shop_video''project_custom_video'], true)) {
  51.             return;
  52.         }
  53.         /** @var Deal|Project|ShopVideo $videoContent */
  54.         $videoContent $event->getEntity();
  55.         if ($event->getMethod() !== 'move' && $event->getMethod() !== 'create') {
  56.             return;
  57.         }
  58.         if (!isset($arguments[2])) {
  59.             throw new InvalidArgumentException('Can not use CDN77 for a non-existent file');
  60.         }
  61.         $this->logger->debug('ParametersProcessor: Proceed to VideoHandler. File:' $arguments[2]);
  62.         // here we need to flush vidcaps generated so other processes will not create vidcaps again.
  63.         if (null === $videoContentId $videoContent->getId()) {
  64.             throw new LogicException('Can not dispatch background tasks without an entity id.');
  65.         }
  66.         $this->videoProcessingHandler->handle($videoContent$arguments[0], $arguments[1], $arguments[2]);
  67.         $this->registry->getManager()->flush();
  68.         $this->eventDispatcher->dispatch(
  69.             new PushProcessingEvent(
  70.                 PurgeVideoHandler::TASK,
  71.                 'handle',
  72.                 $videoContent,
  73.                 ClassUtils::getClass($videoContent),
  74.                 $videoContentId
  75.             )
  76.         );
  77.         $this->tasksPool->send();
  78.     }
  79.     private function isEnabled(): bool
  80.     {
  81.         return $this->enabled;
  82.     }
  83.     public function setEnabled(bool $enabled): void
  84.     {
  85.         $this->enabled $enabled;
  86.     }
  87.     /**
  88.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  89.      */
  90.     public static function getSubscribedEvents(): array
  91.     {
  92.         return [
  93.             PopProcessingEvent::class => 'onPop',
  94.         ];
  95.     }
  96. }