<?php
namespace App\Subscriber\Content;
use App\Event\Content\ContentVideoMetaStoredEvent;
use App\Event\Content\VideoFileUploadedEvent;
use App\Service\Content\VideoMetaService;
use App\Service\Content\VideoService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentVideoMetaSubscriber implements EventSubscriberInterface
{
protected VideoMetaService $service;
protected VideoService $videoService;
/**
* ContentVideoMetaSubscriber constructor.
*/
public function __construct(VideoMetaService $service, VideoService $videoService)
{
$this->service = $service;
$this->videoService = $videoService;
}
public static function getSubscribedEvents(): array
{
return [
VideoFileUploadedEvent::class => [
['onVideoUploadedReadMetaData', 9998],
],
ContentVideoMetaStoredEvent::class => [
['onVideoMetaStoredUpdateVideoDuration', 0],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onVideoUploadedReadMetaData(VideoFileUploadedEvent $event)
{
$video = $event->getEntity();
$meta = $this->service->createVideoMeta($video);
$this->service->storeVideoMeta($meta, $video);
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onVideoMetaStoredUpdateVideoDuration(ContentVideoMetaStoredEvent $event)
{
$meta = $event->getEntity();
$video = $event->getVideo();
$video->setDuration($meta->getDuration());
$this->videoService->storeContentVideo($video);
}
}