src/Subscriber/Content/ContentPublishSubscriber.php line 117

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Dictionary\ContentStatus;
  4. use App\Dictionary\ContentType;
  5. use App\Event\Content\ContentPublishedEvent;
  6. use App\Event\RedisEventManager;
  7. use App\Lib\Content\ContentServiceAwareInterface;
  8. use App\Service\ActivityFeedService;
  9. use App\Service\Content\ContentService;
  10. use App\Service\Content\PreviewService;
  11. use App\Subscriber\AbstractRedisEventManagerSubscriber;
  12. use Frivol\Common\Service\CacheService;
  13. use Frivol\Common\Service\Notification\ContentPublishNotification;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Routing\RouterInterface;
  16. class ContentPublishSubscriber extends AbstractRedisEventManagerSubscriber implements EventSubscriberInterfaceContentServiceAwareInterface
  17. {
  18.     protected ContentService $service;
  19.     protected PreviewService $previewService;
  20.     protected ActivityFeedService $activityFeedService;
  21.     protected CacheService $cache;
  22.     public function __construct(ContentService $servicePreviewService $previewServiceRedisEventManager $redisEventManager,
  23.         ActivityFeedService $activityFeedServiceRouterInterface $routerCacheService $cache)
  24.     {
  25.         $this->setContentService($service);
  26.         $this->setPreviewService($previewService);
  27.         $this->setActivityFeedService($activityFeedService);
  28.         $this->cache $cache;
  29.         parent::__construct($redisEventManager$router);
  30.     }
  31.     public function getActivityFeedService(): ActivityFeedService
  32.     {
  33.         return $this->activityFeedService;
  34.     }
  35.     /**
  36.      * @return $this
  37.      */
  38.     public function setActivityFeedService(ActivityFeedService $activityFeedService): self
  39.     {
  40.         $this->activityFeedService $activityFeedService;
  41.         return $this;
  42.     }
  43.     public function setPreviewService(PreviewService $service): self
  44.     {
  45.         $this->previewService $service;
  46.         return $this;
  47.     }
  48.     public function getPreviewService(): PreviewService
  49.     {
  50.         return $this->previewService;
  51.     }
  52.     public function setContentService(ContentService $service): ContentServiceAwareInterface
  53.     {
  54.         $this->service $service;
  55.         return $this;
  56.     }
  57.     public function getContentService(): ContentService
  58.     {
  59.         return $this->service;
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             ContentPublishedEvent::class => [
  65.                 ['onContentPublishedUpdateStatus'0],
  66.                 ['onContentPublishedSendNotification'1],
  67.                 ['onContentPublishedClearCache'2],
  68.             ],
  69.         ];
  70.     }
  71.     public function onContentPublishedUpdateStatus(ContentPublishedEvent $event)
  72.     {
  73.         $this->getContentService()->changeContentStatus($event->getContent(), ContentStatus::ACTIVE);
  74.     }
  75.     public function onContentPublishedSendNotification(ContentPublishedEvent $event)
  76.     {
  77.         $content $event->getContent();
  78.         $author $content->getMember();
  79.         $previewService $this->getPreviewService();
  80.         $imageIndex $content->getPreviewImage() ? $content->getPreviewImage() : 1;
  81.         $photo $previewService->getPreviewPhoto($content->getContent(), $imageIndextrue);
  82.         $filter ContentType::VIDEO == $content->getType() ? 'preview_big' 'previmg_big';
  83.         $notification = new ContentPublishNotification();
  84.         $notification->setFromMemberId($author->getId());
  85.         $notification->setFromUsername($author->getUsername());
  86.         $notification->setData([
  87.             'type' => $content->isImageset() ? 'Bilderset' 'Video',
  88.             'title' => $content->getContent()->getTitle(),
  89.             'slug' => $content->isImageset() ? $content->getImageset()->getSlug() : $content->getVideo()->getSlug(),
  90.             'image' => (false === $content->isImageset() && null !== $photo) ? $previewService->getUrlForPhoto($photo$filter) : null,
  91.             'username' => $author->getUsername(),
  92.         ]);
  93.         $this->getRedisEventManager()->publishToAll($author$notification);
  94.     }
  95.     public function onContentPublishedClearCache(ContentPublishedEvent $event): void
  96.     {
  97.         $content $event->getContent();
  98.         if (!$content->isImageset()) {
  99.             return;
  100.         }
  101.         try {
  102.             $this->cache->invalidateTags([
  103.                 'imagesets',
  104.                 'imageset-slug-'.$content->getSlug(),
  105.                 'imageset-'.$content->getId(),
  106.             ]);
  107.         } catch (\InvalidArgumentException $iae) {
  108.             $this->getPreviewService()->getLogger()->info($iae->getMessage().PHP_EOL.$iae->getTraceAsString());
  109.         }
  110.     }
  111. }