<?php declare(strict_types=1);
namespace App\VideoProcessing\Processing;
use App\Entity\Deal;
use App\Entity\Project;
use App\Entity\ShopVideo;
use App\RabbitMq\Processing\PopProcessingEvent;
use App\RabbitMq\Processing\PushProcessingEvent;
use App\RabbitMq\TasksPool;
use App\VideoProcessing\PurgeVideoHandler;
use App\VideoProcessing\VideoProcessingHandler;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Persistence\ManagerRegistry;
use InvalidArgumentException;
use LogicException;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use UploadsBundle\RabbitMq\Handler\FileHandlerFactoryHandler;
/**
* Class PopListener
*
* So take my strong advice, just remember to always think twice.. (c) Michael
*/
class PopSubscriber implements EventSubscriberInterface
{
private VideoProcessingHandler $videoProcessingHandler;
private ManagerRegistry $registry;
private EventDispatcherInterface $eventDispatcher;
private TasksPool $tasksPool;
private LoggerInterface $logger;
private bool $enabled = true;
public function __construct(ManagerRegistry $registry, VideoProcessingHandler $videoProcessingHandler, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher, TasksPool $tasksPool)
{
$this->registry = $registry;
$this->videoProcessingHandler = $videoProcessingHandler;
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
$this->tasksPool = $tasksPool;
}
public function onPop(PopProcessingEvent $event): void
{
if (!$this->isEnabled()) {
return;
}
if ($event->getService() !== FileHandlerFactoryHandler::HANDLER) {
return;
}
/** @var string[] $arguments */
$arguments = $event->getArguments();
if (!isset($arguments[0]) || !in_array($arguments[0], ['deal_video', 'project_video', 'shop_video', 'project_custom_video'], true)) {
return;
}
/** @var Deal|Project|ShopVideo $videoContent */
$videoContent = $event->getEntity();
if ($event->getMethod() !== 'move' && $event->getMethod() !== 'create') {
return;
}
if (!isset($arguments[2])) {
throw new InvalidArgumentException('Can not use CDN77 for a non-existent file');
}
$this->logger->debug('ParametersProcessor: Proceed to VideoHandler. File:' . $arguments[2]);
// here we need to flush vidcaps generated so other processes will not create vidcaps again.
if (null === $videoContentId = $videoContent->getId()) {
throw new LogicException('Can not dispatch background tasks without an entity id.');
}
$this->videoProcessingHandler->handle($videoContent, $arguments[0], $arguments[1], $arguments[2]);
$this->registry->getManager()->flush();
$this->eventDispatcher->dispatch(
new PushProcessingEvent(
PurgeVideoHandler::TASK,
'handle',
$videoContent,
ClassUtils::getClass($videoContent),
$videoContentId
)
);
$this->tasksPool->send();
}
private function isEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
PopProcessingEvent::class => 'onPop',
];
}
}