<?php declare(strict_types=1);
namespace App\VideoProcessing\Processing;
use App\Entity\Feed;
use App\Entity\ShopVideoFile;
use App\RabbitMq\Processing\PopProcessingEvent;
use App\VideoProcessing\Ffmpeg\FileInfo\FetcherInterface;
use InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use UploadsBundle\RabbitMq\Handler\FileHandlerFactoryHandler;
class FeedVideoPopProcessing implements EventSubscriberInterface
{
private FetcherInterface $fetcher;
private bool $enabled = true;
public function __construct(FetcherInterface $fetcher)
{
$this->fetcher = $fetcher;
}
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]) || $arguments[0] !== 'feed_trailer') {
return;
}
if ($event->getMethod() !== 'move' && $event->getMethod() !== 'create') {
return;
}
if (!isset($arguments[1], $arguments[2])) {
throw new InvalidArgumentException('Can not use class without file paths.');
}
/** @var Feed $feed */
$feed = $event->getEntity();
$videoFile = new ShopVideoFile();
$this->fetcher->getFileInfo($videoFile, $arguments[1], $arguments[2]);
$feed->setVideoWidth($videoFile->getWidth());
$feed->setVideoHeight($videoFile->getHeight());
if (null === $runtime = $videoFile->getRunTime()) {
return;
}
$feed->setVideoRunTime($runtime);
}
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',
];
}
}