src/Subscriber/Content/ContentVideoEncodingSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Dictionary\ContentVideoMode;
  4. use App\Event\Content\ContentVideoEncodingFinishedEvent;
  5. use App\Event\Content\ContentVideoEncodingProgressedEvent;
  6. use App\Event\Content\VideoFileUploadedEvent;
  7. use App\Lib\Content\ContentVideoServiceAwareInterface;
  8. use App\Lib\System\Job\VideoEncodingJob;
  9. use App\Service\Content\VideoEncoderService;
  10. use App\Service\Content\VideoService;
  11. use App\Service\System\JobService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ContentVideoEncodingSubscriber implements EventSubscriberInterfaceContentVideoServiceAwareInterface
  14. {
  15.     protected VideoService $videoService;
  16.     protected JobService $jobService;
  17.     protected VideoEncoderService $encoderService;
  18.     /**
  19.      * ContentVideoEncodingSubscriber constructor.
  20.      */
  21.     public function __construct(VideoService $videoServiceVideoEncoderService $encoderServiceJobService $jobService)
  22.     {
  23.         $this->setContentVideoService($videoService);
  24.         $this->jobService $jobService;
  25.         $this->encoderService $encoderService;
  26.     }
  27.     public function getContentVideoService(): VideoService
  28.     {
  29.         return $this->videoService;
  30.     }
  31.     /**
  32.      * @return $this|ContentVideoServiceAwareInterface
  33.      */
  34.     public function setContentVideoService(VideoService $service): ContentVideoServiceAwareInterface
  35.     {
  36.         $this->videoService $service;
  37.         return $this;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             VideoFileUploadedEvent::class => [
  43.                 ['onVideoFileUploadedCreateEncodeJobs'9996],
  44.             ],
  45.             ContentVideoEncodingProgressedEvent::class => [
  46.                 ['onEncodingProgressedUpdateJob'0],
  47.             ],
  48.             ContentVideoEncodingFinishedEvent::class => [
  49.                 ['onEncodingFinishedUpdateQualityFlag'0],
  50.             ],
  51.         ];
  52.     }
  53.     /**
  54.      * @throws \Doctrine\ORM\ORMException
  55.      * @throws \Doctrine\ORM\OptimisticLockException
  56.      */
  57.     public function onVideoFileUploadedCreateEncodeJobs(VideoFileUploadedEvent $event)
  58.     {
  59.         $video $event->getEntity();
  60.         if (!$meta $video->getVideoMeta()) {
  61.             return;
  62.         }
  63.         $modes = [
  64.             ContentVideoMode::QUALITY_360P,
  65.             ContentVideoMode::QUALITY_720P,
  66.             ContentVideoMode::QUALITY_1080P,
  67.         ];
  68.         foreach ($modes as $mode) {
  69.             if ($this->encoderService->videoSatisfiesMode($meta$mode)) {
  70.                 $job = new VideoEncodingJob($video->getId(), $mode);
  71.                 $queue VideoEncodingJob::queue;
  72.                 $this->jobService->createSaveAndSchedule($job''null$queue);
  73.             }
  74.         }
  75.     }
  76.     /**
  77.      * @throws \Doctrine\ORM\ORMException
  78.      * @throws \Doctrine\ORM\OptimisticLockException
  79.      */
  80.     public function onEncodingProgressedUpdateJob(ContentVideoEncodingProgressedEvent $event)
  81.     {
  82.         $job $event->getEntity();
  83.         $progress $event->getProgress();
  84.         $this->jobService->updateJobEntityProgress($job$progress);
  85.     }
  86.     /**
  87.      * @throws \Doctrine\ORM\ORMException
  88.      * @throws \Doctrine\ORM\OptimisticLockException
  89.      */
  90.     public function onEncodingFinishedUpdateQualityFlag(ContentVideoEncodingFinishedEvent $event)
  91.     {
  92.         $video $event->getEntity();
  93.         $mode $event->getVideoMode();
  94.         if (ContentVideoMode::QUALITY_1080P == $mode) {
  95.             $video->setIs1080p(true);
  96.         }
  97.         if (ContentVideoMode::QUALITY_720P == $mode) {
  98.             $video->setIs720p(true);
  99.         }
  100.         if (ContentVideoMode::QUALITY_360P == $mode) {
  101.             $video->setIs360p(true);
  102.         }
  103.         $this->getContentVideoService()->storeContentVideo($video);
  104.     }
  105. }