<?php
namespace App\Subscriber\Content;
use App\Dictionary\ContentStatus;
use App\Dictionary\ContentType;
use App\Event\Content\ContentPublishedEvent;
use App\Event\RedisEventManager;
use App\Lib\Content\ContentServiceAwareInterface;
use App\Service\ActivityFeedService;
use App\Service\Content\ContentService;
use App\Service\Content\PreviewService;
use App\Subscriber\AbstractRedisEventManagerSubscriber;
use Frivol\Common\Service\CacheService;
use Frivol\Common\Service\Notification\ContentPublishNotification;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
class ContentPublishSubscriber extends AbstractRedisEventManagerSubscriber implements EventSubscriberInterface, ContentServiceAwareInterface
{
protected ContentService $service;
protected PreviewService $previewService;
protected ActivityFeedService $activityFeedService;
protected CacheService $cache;
public function __construct(ContentService $service, PreviewService $previewService, RedisEventManager $redisEventManager,
ActivityFeedService $activityFeedService, RouterInterface $router, CacheService $cache)
{
$this->setContentService($service);
$this->setPreviewService($previewService);
$this->setActivityFeedService($activityFeedService);
$this->cache = $cache;
parent::__construct($redisEventManager, $router);
}
public function getActivityFeedService(): ActivityFeedService
{
return $this->activityFeedService;
}
/**
* @return $this
*/
public function setActivityFeedService(ActivityFeedService $activityFeedService): self
{
$this->activityFeedService = $activityFeedService;
return $this;
}
public function setPreviewService(PreviewService $service): self
{
$this->previewService = $service;
return $this;
}
public function getPreviewService(): PreviewService
{
return $this->previewService;
}
public function setContentService(ContentService $service): ContentServiceAwareInterface
{
$this->service = $service;
return $this;
}
public function getContentService(): ContentService
{
return $this->service;
}
public static function getSubscribedEvents(): array
{
return [
ContentPublishedEvent::class => [
['onContentPublishedUpdateStatus', 0],
['onContentPublishedSendNotification', 1],
['onContentPublishedClearCache', 2],
],
];
}
public function onContentPublishedUpdateStatus(ContentPublishedEvent $event)
{
$this->getContentService()->changeContentStatus($event->getContent(), ContentStatus::ACTIVE);
}
public function onContentPublishedSendNotification(ContentPublishedEvent $event)
{
$content = $event->getContent();
$author = $content->getMember();
$previewService = $this->getPreviewService();
$imageIndex = $content->getPreviewImage() ? $content->getPreviewImage() : 1;
$photo = $previewService->getPreviewPhoto($content->getContent(), $imageIndex, true);
$filter = ContentType::VIDEO == $content->getType() ? 'preview_big' : 'previmg_big';
$notification = new ContentPublishNotification();
$notification->setFromMemberId($author->getId());
$notification->setFromUsername($author->getUsername());
$notification->setData([
'type' => $content->isImageset() ? 'Bilderset' : 'Video',
'title' => $content->getContent()->getTitle(),
'slug' => $content->isImageset() ? $content->getImageset()->getSlug() : $content->getVideo()->getSlug(),
'image' => (false === $content->isImageset() && null !== $photo) ? $previewService->getUrlForPhoto($photo, $filter) : null,
'username' => $author->getUsername(),
]);
$this->getRedisEventManager()->publishToAll($author, $notification);
}
public function onContentPublishedClearCache(ContentPublishedEvent $event): void
{
$content = $event->getContent();
if (!$content->isImageset()) {
return;
}
try {
$this->cache->invalidateTags([
'imagesets',
'imageset-slug-'.$content->getSlug(),
'imageset-'.$content->getId(),
]);
} catch (\InvalidArgumentException $iae) {
$this->getPreviewService()->getLogger()->info($iae->getMessage().PHP_EOL.$iae->getTraceAsString());
}
}
}