<?php
namespace App\Subscriber\Caching;
use App\Event\Content\ActorAssignmentChangeEvent;
use App\Event\Content\CategoryAssignmentChangeEvent;
use App\Event\Content\ContentBoughtEvent;
use App\Event\Content\ContentCreatedEvent;
use App\Event\Content\ContentDeletedEvent;
use App\Event\Content\ContentSubmittedEvent;
use App\Event\Content\SingleContentCacheClearedEvent;
use Frivol\Common\Service\CacheService;
use App\Service\Client\Content\ContentService;
use App\Service\Client\Content\ImagesetService;
use App\Service\Client\Content\VideoService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class ContentSubscriber
* @package App\Subscriber\Caching
*/
class ContentSubscriber implements EventSubscriberInterface
{
/**
* @var ContentService
*/
protected ContentService $contentService;
/**
* @var VideoService
*/
protected VideoService $videoService;
/**
* @var ImagesetService
*/
protected ImagesetService $imagesetService;
/**
* @var CacheService
*/
protected CacheService $cacheService;
/**
* @param CacheService $cacheService
* @param ContentService $contentService
* @param VideoService $videoService
* @param ImagesetService $imagesetService
*/
public function __construct(CacheService $cacheService, ContentService $contentService, VideoService $videoService, ImagesetService $imagesetService)
{
$this->cacheService = $cacheService;
$this->contentService = $contentService;
$this->videoService = $videoService;
$this->imagesetService = $imagesetService;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
ActorAssignmentChangeEvent::class => [
['onActorAssignmentChange', 0],
],
CategoryAssignmentChangeEvent::class => [
['onCategoryAssignmentChange', 0],
],
SingleContentCacheClearedEvent::class => [
['onSingleContentCleared', 0]
],
ContentCreatedEvent::class => [
['onContentCreatedEvent', 0]
],
ContentDeletedEvent::class => [
['onContentDeletedEvent', 0]
],
ContentSubmittedEvent::class => [
['onContentSubmittedEvent', 0]
],
ContentBoughtEvent::class => [
['onContentBoughtEvent', 0]
]
];
}
/**
* @param ActorAssignmentChangeEvent $event
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function onActorAssignmentChange(ActorAssignmentChangeEvent $event)
{
$contentId = $event->getContent();
$this->cacheService->clearContentCache($contentId);
}
/**
* @param CategoryAssignmentChangeEvent $event
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function onCategoryAssignmentChange(CategoryAssignmentChangeEvent $event)
{
$contentId = $event->getContent();
$this->cacheService->clearContentCache($contentId);
}
/**
* @param SingleContentCacheClearedEvent $event
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function onSingleContentCleared(SingleContentCacheClearedEvent $event)
{
$contentSpecs = $this->contentService->getContentById($event->getContent());
if(isset($contentSpecs['member'])) {
$this->contentService->clearMemberContentCache($contentSpecs['member']['id']);
}
if(isset($contentSpecs['imageset'])) {
$this->imagesetService->clearImagesetCache($contentSpecs['imageset']['id']);
}
if(isset($contentSpecs['video'])) {
$this->videoService->clearVideoCache($contentSpecs['video']['id']);
}
}
/**
* @param ContentCreatedEvent $event
* @throws \Psr\Cache\InvalidArgumentException
*/
public function onContentCreatedEvent(ContentCreatedEvent $event)
{
$memberId = $event->getAuthor();
$this->contentService->clearMemberContentCache($memberId);
}
/**
* @param ContentDeletedEvent $event
* @throws \Psr\Cache\InvalidArgumentException
*/
public function onContentDeletedEvent(ContentDeletedEvent $event)
{
$memberId = $event->getAuthor();
$this->contentService->clearMemberContentCache($memberId);
}
/**
* @param ContentSubmittedEvent $event
* @throws \Psr\Cache\InvalidArgumentException
*/
public function onContentSubmittedEvent(ContentSubmittedEvent $event)
{
$memberId = $event->getAuthor();
$this->contentService->clearMemberContentCache($memberId);
}
/**
* @param ContentBoughtEvent $event
* @return void
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function onContentBoughtEvent(ContentBoughtEvent $event)
{
$this->cacheService->clearContentCache($event->getContent());
$this->cacheService->clearMemberCoinsCache($event->getBuyer());
$this->cacheService->clearMediaCenterCache($event->getBuyer());
}
}