<?php
namespace App\Subscriber\Content;
use App\Dictionary\ImageFilter;
use App\Entity\Content;
use App\Entity\ContentImageset;
use App\Entity\ContentTypeAbstract;
use App\Event\Content\ContentCacheFilesDeleteEvent;
use App\Event\Content\ContentDeleteEvent;
use App\Event\Content\ContentImagesetDeleteEvent;
use App\Event\Content\ContentVideoDeleteEvent;
use App\Service\ActivityFeedService;
use App\Service\Content\ContentService;
use App\Service\Content\PreviewService;
use App\Service\Media\StorageLayer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentRemovalSubscriber implements EventSubscriberInterface
{
protected ContentService $contentService;
protected ActivityFeedService $activityFeedService;
protected PreviewService $previewService;
protected StorageLayer $storageLayer;
/**
* ContentRemovalSubscriber constructor.
*/
public function __construct(ContentService $contentService, ActivityFeedService $activityFeedService,
PreviewService $previewService, StorageLayer $storageLayer)
{
$this->contentService = $contentService;
$this->activityFeedService = $activityFeedService;
$this->previewService = $previewService;
$this->storageLayer = $storageLayer;
}
public static function getSubscribedEvents(): array
{
return [
ContentVideoDeleteEvent::class => [
['onContentDeleteRemoveNfsFiles', 0],
['onContentDeleteRemoveActivities', 1],
['onVideoDeleteRemoveCacheFiles', 2],
],
ContentImagesetDeleteEvent::class => [
['onContentDeleteRemoveNfsFiles', 0],
['onContentDeleteRemoveActivities', 1],
['onImagesetDeleteRemoveCacheFiles', 2],
],
ContentCacheFilesDeleteEvent::class => [
['onContentDeleteRemoveCacheFiles', 2],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onContentDeleteRemoveActivities(ContentDeleteEvent $event)
{
$content = $event->getEntity()->getContent();
$this->activityFeedService->removeActivitiesForContent($content);
}
public function onContentDeleteRemoveNfsFiles(ContentDeleteEvent $event)
{
/**
* @var $content Content
*/
if ($content = $event->getEntity()->getContent()) {
// remove public path
$dir = $content->getContent()->getPublicPath();
$this->storageLayer->deletePublicDir($dir);
// remove local path with the sources
$dir = $this->storageLayer->getContentRawPath($content->getId());
$this->storageLayer->deleteRawDir($dir);
}
}
public function onImagesetDeleteRemoveCacheFiles(ContentImagesetDeleteEvent $event)
{
$imageset = $event->getEntity();
$this->executeCacheRemoval($imageset, [ImageFilter::THUMBS, ImageFilter::PHOTOS, ImageFilter::PREVIMG_BIG, ImageFilter::PREVIMG_SMALL]);
}
public function onVideoDeleteRemoveCacheFiles(ContentVideoDeleteEvent $event)
{
$video = $event->getEntity();
$this->executeCacheRemoval($video, ['preview_small', 'preview_big']);
}
public function onContentDeleteRemoveCacheFiles(ContentCacheFilesDeleteEvent $event)
{
$abstract = $event->getEntity();
$filters = ['preview_small', 'preview_big'];
if ($abstract instanceof ContentImageset) {
$filters = [ImageFilter::THUMBS, ImageFilter::PHOTOS, ImageFilter::PREVIMG_BIG, ImageFilter::PREVIMG_SMALL];
}
foreach ($filters as $filter) {
$this->storageLayer->deleteContentCacheDir($abstract, $filter);
}
}
protected function executeCacheRemoval(ContentTypeAbstract $abstract, array $filters)
{
foreach ($filters as $filter) {
$this->storageLayer->deleteContentCacheDir($abstract, $filter);
}
}
}