src/Subscriber/Content/VideoSnapshotSubscriber.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Entity\ContentVideo;
  4. use App\Event\Content\ContentVideoDeleteEvent;
  5. use App\Event\Content\SnapshotsCreatedEvent;
  6. use App\Event\Content\VideoFileUploadedEvent;
  7. use App\Lib\System\Job\VideoSnapshotGeneratorJob;
  8. use App\Service\Content\ContentService;
  9. use App\Service\Content\SnapshotService;
  10. use App\Service\Media\StorageLayer;
  11. use App\Service\System\JobService;
  12. use Liip\ImagineBundle\Service\FilterService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class VideoSnapshotSubscriber implements EventSubscriberInterface
  15. {
  16.     protected SnapshotService $service;
  17.     protected ContentService $contentService;
  18.     protected FilterService $filterService;
  19.     protected JobService $jobService;
  20.     protected StorageLayer $storageLayer;
  21.     /**
  22.      * VideoSnapshotSubscriber constructor.
  23.      */
  24.     public function __construct(SnapshotService $serviceContentService $contentService,
  25.         FilterService $filterServiceJobService $jobService,
  26.         StorageLayer $layer)
  27.     {
  28.         $this->service $service;
  29.         $this->contentService $contentService;
  30.         $this->filterService $filterService;
  31.         $this->jobService $jobService;
  32.         $this->storageLayer $layer;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             VideoFileUploadedEvent::class => [
  38.                 ['onVideoUploadedCreateSnapshots'9997],
  39.             ],
  40.             SnapshotsCreatedEvent::class => [
  41.                 ['onSnapshotsCreatedUpdateContent'0],
  42.             ],
  43.             ContentVideoDeleteEvent::class => [
  44.                 // remove liipimagine cache files first.
  45.                 // later, snapshot originals would not exist anymore.
  46.                 ['onVideoDeleteRemoveSnapshots'9999],
  47.             ],
  48.         ];
  49.     }
  50.     /**
  51.      * @throws \Doctrine\ORM\ORMException
  52.      * @throws \Doctrine\ORM\OptimisticLockException
  53.      */
  54.     public function onVideoUploadedCreateSnapshots(VideoFileUploadedEvent $event)
  55.     {
  56.         $video $event->getEntity();
  57.         // reset in DB if they will be re-created
  58.         if ($video->getPreviewsCreatedAt()) {
  59.             $video->setPreviewsCreatedAt(null);
  60.             $this->contentService->storeEntity($video);
  61.         }
  62.         $job = new VideoSnapshotGeneratorJob($video->getId());
  63.         $this->jobService->createSaveAndSchedule($job''nullVideoSnapshotGeneratorJob::queue);
  64.     }
  65.     public function onVideoDeleteRemoveSnapshots(ContentVideoDeleteEvent $event)
  66.     {
  67.         /**
  68.          * @var $video ContentVideo
  69.          */
  70.         $video $event->getEntity();
  71.         foreach ($this->service->getSnapshots($video) as $snapshot) {
  72.             $number $snapshot->getFilenameNumber();
  73.             $basename 'snapshot.'.$number.'.png';
  74.             $resolvePath $this->storageLayer->getContentPublicFilePath($video$basename);
  75.             foreach (['preview_small''preview_big'] as $filter) {
  76.                 $this->filterService->bustCache($resolvePath$filter);
  77.             }
  78.         }
  79.         if ($content $video->getContent()) {
  80.             $this->service->deleteSnapshotsForContent($content);
  81.         }
  82.     }
  83.     /**
  84.      * @throws \Doctrine\ORM\ORMException
  85.      * @throws \Doctrine\ORM\OptimisticLockException
  86.      */
  87.     public function onSnapshotsCreatedUpdateContent(SnapshotsCreatedEvent $event)
  88.     {
  89.         $video $event->getEntity();
  90.         $video->setPreviewsCreatedAt(new \DateTime());
  91.         $this->contentService->storeEntity($video);
  92.     }
  93. }