src/Subscriber/Content/ContentRemovalSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Dictionary\ImageFilter;
  4. use App\Entity\Content;
  5. use App\Entity\ContentImageset;
  6. use App\Entity\ContentTypeAbstract;
  7. use App\Event\Content\ContentCacheFilesDeleteEvent;
  8. use App\Event\Content\ContentDeleteEvent;
  9. use App\Event\Content\ContentImagesetDeleteEvent;
  10. use App\Event\Content\ContentVideoDeleteEvent;
  11. use App\Service\ActivityFeedService;
  12. use App\Service\Content\ContentService;
  13. use App\Service\Content\PreviewService;
  14. use App\Service\Media\StorageLayer;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ContentRemovalSubscriber implements EventSubscriberInterface
  17. {
  18.     protected ContentService $contentService;
  19.     protected ActivityFeedService $activityFeedService;
  20.     protected PreviewService $previewService;
  21.     protected StorageLayer $storageLayer;
  22.     /**
  23.      * ContentRemovalSubscriber constructor.
  24.      */
  25.     public function __construct(ContentService $contentServiceActivityFeedService $activityFeedService,
  26.         PreviewService $previewServiceStorageLayer $storageLayer)
  27.     {
  28.         $this->contentService $contentService;
  29.         $this->activityFeedService $activityFeedService;
  30.         $this->previewService $previewService;
  31.         $this->storageLayer $storageLayer;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             ContentVideoDeleteEvent::class => [
  37.                 ['onContentDeleteRemoveNfsFiles'0],
  38.                 ['onContentDeleteRemoveActivities'1],
  39.                 ['onVideoDeleteRemoveCacheFiles'2],
  40.             ],
  41.             ContentImagesetDeleteEvent::class => [
  42.                 ['onContentDeleteRemoveNfsFiles'0],
  43.                 ['onContentDeleteRemoveActivities'1],
  44.                 ['onImagesetDeleteRemoveCacheFiles'2],
  45.             ],
  46.             ContentCacheFilesDeleteEvent::class => [
  47.                 ['onContentDeleteRemoveCacheFiles'2],
  48.             ],
  49.         ];
  50.     }
  51.     /**
  52.      * @throws \Doctrine\ORM\ORMException
  53.      * @throws \Doctrine\ORM\OptimisticLockException
  54.      */
  55.     public function onContentDeleteRemoveActivities(ContentDeleteEvent $event)
  56.     {
  57.         $content $event->getEntity()->getContent();
  58.         $this->activityFeedService->removeActivitiesForContent($content);
  59.     }
  60.     public function onContentDeleteRemoveNfsFiles(ContentDeleteEvent $event)
  61.     {
  62.         /**
  63.          * @var $content Content
  64.          */
  65.         if ($content $event->getEntity()->getContent()) {
  66.             // remove public path
  67.             $dir $content->getContent()->getPublicPath();
  68.             $this->storageLayer->deletePublicDir($dir);
  69.             // remove local path with the sources
  70.             $dir $this->storageLayer->getContentRawPath($content->getId());
  71.             $this->storageLayer->deleteRawDir($dir);
  72.         }
  73.     }
  74.     public function onImagesetDeleteRemoveCacheFiles(ContentImagesetDeleteEvent $event)
  75.     {
  76.         $imageset $event->getEntity();
  77.         $this->executeCacheRemoval($imageset, [ImageFilter::THUMBSImageFilter::PHOTOSImageFilter::PREVIMG_BIGImageFilter::PREVIMG_SMALL]);
  78.     }
  79.     public function onVideoDeleteRemoveCacheFiles(ContentVideoDeleteEvent $event)
  80.     {
  81.         $video $event->getEntity();
  82.         $this->executeCacheRemoval($video, ['preview_small''preview_big']);
  83.     }
  84.     public function onContentDeleteRemoveCacheFiles(ContentCacheFilesDeleteEvent $event)
  85.     {
  86.         $abstract $event->getEntity();
  87.         $filters = ['preview_small''preview_big'];
  88.         if ($abstract instanceof ContentImageset) {
  89.             $filters = [ImageFilter::THUMBSImageFilter::PHOTOSImageFilter::PREVIMG_BIGImageFilter::PREVIMG_SMALL];
  90.         }
  91.         foreach ($filters as $filter) {
  92.             $this->storageLayer->deleteContentCacheDir($abstract$filter);
  93.         }
  94.     }
  95.     protected function executeCacheRemoval(ContentTypeAbstract $abstract, array $filters)
  96.     {
  97.         foreach ($filters as $filter) {
  98.             $this->storageLayer->deleteContentCacheDir($abstract$filter);
  99.         }
  100.     }
  101. }