src/Subscriber/Caching/ContentSubscriber.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Caching;
  3. use App\Event\Content\ActorAssignmentChangeEvent;
  4. use App\Event\Content\CategoryAssignmentChangeEvent;
  5. use App\Event\Content\ContentBoughtEvent;
  6. use App\Event\Content\ContentCreatedEvent;
  7. use App\Event\Content\ContentDeletedEvent;
  8. use App\Event\Content\ContentSubmittedEvent;
  9. use App\Event\Content\SingleContentCacheClearedEvent;
  10. use Frivol\Common\Service\CacheService;
  11. use App\Service\Client\Content\ContentService;
  12. use App\Service\Client\Content\ImagesetService;
  13. use App\Service\Client\Content\VideoService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * Class ContentSubscriber
  17.  * @package App\Subscriber\Caching
  18.  */
  19. class ContentSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var ContentService
  23.      */
  24.     protected ContentService $contentService;
  25.     /**
  26.      * @var VideoService
  27.      */
  28.     protected VideoService $videoService;
  29.     /**
  30.      * @var ImagesetService
  31.      */
  32.     protected ImagesetService $imagesetService;
  33.     /**
  34.      * @var CacheService
  35.      */
  36.     protected CacheService $cacheService;
  37.     /**
  38.      * @param CacheService $cacheService
  39.      * @param ContentService $contentService
  40.      * @param VideoService $videoService
  41.      * @param ImagesetService $imagesetService
  42.      */
  43.     public function __construct(CacheService $cacheServiceContentService $contentServiceVideoService $videoServiceImagesetService $imagesetService)
  44.     {
  45.         $this->cacheService $cacheService;
  46.         $this->contentService $contentService;
  47.         $this->videoService $videoService;
  48.         $this->imagesetService $imagesetService;
  49.     }
  50.     /**
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             ActorAssignmentChangeEvent::class => [
  57.                 ['onActorAssignmentChange'0],
  58.             ],
  59.             CategoryAssignmentChangeEvent::class => [
  60.                 ['onCategoryAssignmentChange'0],
  61.             ],
  62.             SingleContentCacheClearedEvent::class => [
  63.                 ['onSingleContentCleared'0]
  64.             ],
  65.             ContentCreatedEvent::class => [
  66.                 ['onContentCreatedEvent'0]
  67.             ],
  68.             ContentDeletedEvent::class => [
  69.                 ['onContentDeletedEvent'0]
  70.             ],
  71.             ContentSubmittedEvent::class => [
  72.                 ['onContentSubmittedEvent'0]
  73.             ],
  74.             ContentBoughtEvent::class => [
  75.                 ['onContentBoughtEvent'0]
  76.             ]
  77.         ];
  78.     }
  79.     /**
  80.      * @param ActorAssignmentChangeEvent $event
  81.      * @throws \Psr\Cache\InvalidArgumentException
  82.      * @throws \ReflectionException
  83.      */
  84.     public function onActorAssignmentChange(ActorAssignmentChangeEvent $event)
  85.     {
  86.         $contentId $event->getContent();
  87.         $this->cacheService->clearContentCache($contentId);
  88.     }
  89.     /**
  90.      * @param CategoryAssignmentChangeEvent $event
  91.      * @throws \Psr\Cache\InvalidArgumentException
  92.      * @throws \ReflectionException
  93.      */
  94.     public function onCategoryAssignmentChange(CategoryAssignmentChangeEvent $event)
  95.     {
  96.         $contentId $event->getContent();
  97.         $this->cacheService->clearContentCache($contentId);
  98.     }
  99.     /**
  100.      * @param SingleContentCacheClearedEvent $event
  101.      * @throws \Psr\Cache\InvalidArgumentException
  102.      * @throws \ReflectionException
  103.      */
  104.     public function onSingleContentCleared(SingleContentCacheClearedEvent $event)
  105.     {
  106.         $contentSpecs $this->contentService->getContentById($event->getContent());
  107.         if(isset($contentSpecs['member'])) {
  108.             $this->contentService->clearMemberContentCache($contentSpecs['member']['id']);
  109.         }
  110.         if(isset($contentSpecs['imageset'])) {
  111.             $this->imagesetService->clearImagesetCache($contentSpecs['imageset']['id']);
  112.         }
  113.         if(isset($contentSpecs['video'])) {
  114.             $this->videoService->clearVideoCache($contentSpecs['video']['id']);
  115.         }
  116.     }
  117.     /**
  118.      * @param ContentCreatedEvent $event
  119.      * @throws \Psr\Cache\InvalidArgumentException
  120.      */
  121.     public function onContentCreatedEvent(ContentCreatedEvent $event)
  122.     {
  123.         $memberId $event->getAuthor();
  124.         $this->contentService->clearMemberContentCache($memberId);
  125.     }
  126.     /**
  127.      * @param ContentDeletedEvent $event
  128.      * @throws \Psr\Cache\InvalidArgumentException
  129.      */
  130.     public function onContentDeletedEvent(ContentDeletedEvent $event)
  131.     {
  132.         $memberId $event->getAuthor();
  133.         $this->contentService->clearMemberContentCache($memberId);
  134.     }
  135.     /**
  136.      * @param ContentSubmittedEvent $event
  137.      * @throws \Psr\Cache\InvalidArgumentException
  138.      */
  139.     public function onContentSubmittedEvent(ContentSubmittedEvent $event)
  140.     {
  141.         $memberId $event->getAuthor();
  142.         $this->contentService->clearMemberContentCache($memberId);
  143.     }
  144.     /**
  145.      * @param ContentBoughtEvent $event
  146.      * @return void
  147.      * @throws \Psr\Cache\InvalidArgumentException
  148.      * @throws \ReflectionException
  149.      */
  150.     public function onContentBoughtEvent(ContentBoughtEvent $event)
  151.     {
  152.         $this->cacheService->clearContentCache($event->getContent());
  153.         $this->cacheService->clearMemberCoinsCache($event->getBuyer());
  154.         $this->cacheService->clearMediaCenterCache($event->getBuyer());
  155.     }
  156. }