src/Subscriber/Content/ContentCheckSeoSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Dictionary\ContentStatus;
  4. use App\Entity\Content;
  5. use App\Entity\ContentImageset;
  6. use App\Entity\ContentVideo;
  7. use App\Event\Content\ContentCheckedEvent;
  8. use App\Event\Content\ContentPublishedEvent;
  9. use App\Event\Content\ContentRenamedEvent;
  10. use App\Event\Content\ContentStatusChangedEvent;
  11. use App\Lib\Content\ContentServiceAwareInterface;
  12. use App\Service\Content\ContentService;
  13. use App\Service\Content\SeoService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ContentCheckSeoSubscriber implements EventSubscriberInterfaceContentServiceAwareInterface
  16. {
  17.     protected ContentService $service;
  18.     protected SeoService $seoService;
  19.     public function __construct(ContentService $serviceSeoService $seoService)
  20.     {
  21.         $this->setContentService($service);
  22.         $this->setSeoService($seoService);
  23.     }
  24.     public function setSeoService(SeoService $seoService): self
  25.     {
  26.         $this->seoService $seoService;
  27.         return $this;
  28.     }
  29.     public function getSeoService(): SeoService
  30.     {
  31.         return $this->seoService;
  32.     }
  33.     public function setContentService(ContentService $service): ContentServiceAwareInterface
  34.     {
  35.         $this->service $service;
  36.         return $this;
  37.     }
  38.     public function getContentService(): ContentService
  39.     {
  40.         return $this->service;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             ContentCheckedEvent::class => [
  46.                 ['onCheckedAndAcceptedGenerateSeo'0],
  47.             ],
  48.             ContentRenamedEvent::class => [
  49.                 ['onRenameRegenerateSlug'0],
  50.             ],
  51.             ContentStatusChangedEvent::class => [
  52.                 ['onNewWaitraitingImagesetGenerateSlug'0],
  53.             ],
  54.             ContentPublishedEvent::class => [
  55.                 ['fixMissingSlugOnPublish'1024],
  56.             ],
  57.         ];
  58.     }
  59.     /**
  60.      * @throws \Doctrine\ORM\ORMException
  61.      * @throws \Doctrine\ORM\OptimisticLockException
  62.      */
  63.     public function onCheckedAndAcceptedGenerateSeo(ContentCheckedEvent $event)
  64.     {
  65.         if (!$event->isAccepted()) {
  66.             return;
  67.         }
  68.         $content $event->getContent();
  69.         // seo stuff
  70.         $seoService $this->getSeoService();
  71.         $updated false;
  72.         if (empty($content->getMetaDescription())) {
  73.             $content->setMetaDescription($seoService->generateMetaDescription($content));
  74.             $updated true;
  75.         }
  76.         if (empty($content->getMetaKeywords())) {
  77.             $content->setMetaKeywords(implode(', '$seoService->generateMetaKeywords($content)));
  78.             $updated true;
  79.         }
  80.         if (null !== $content->getContent() && empty($content->getContent()->getSlug())) {
  81.             $content->getContent()->setSlug($seoService->generateSlug($content));
  82.             $updated true;
  83.         }
  84.         if ($updated) {
  85.             $content->setUpdatedAt(new \DateTime());
  86.         }
  87.         $this->service->storeEntity($content);
  88.     }
  89.     /**
  90.      * @throws \Doctrine\ORM\ORMException
  91.      * @throws \Doctrine\ORM\OptimisticLockException
  92.      */
  93.     public function onRenameRegenerateSlug(ContentRenamedEvent $event)
  94.     {
  95.         $entity $event->getEntity();
  96.         if ($entity instanceof ContentVideo || $entity instanceof ContentImageset) {
  97.             $content $entity->getContent();
  98.             $entity->setSlug($this->getSeoService()->generateSlug($content));
  99.             $content->setUpdatedAt(new \DateTime());
  100.             $this->service->storeEntity($content);
  101.         }
  102.     }
  103.     public function onNewWaitraitingImagesetGenerateSlug(ContentStatusChangedEvent $event): void
  104.     {
  105.         if ($event->isVideo() || ContentStatus::WAITRATING !== $event->getNewStatus() || !empty($event->getContent()->getSlug())) {
  106.             return;
  107.         }
  108.         /** @var ContentImageset $contentAbstract */
  109.         $contentAbstract $event->getContent();
  110.         $slug $this->getSeoService()->generateSlug($contentAbstract->getContent());
  111.         $contentAbstract->setSlug($slug);
  112.         $this->service->storeEntity($contentAbstract);
  113.     }
  114.     public function fixMissingSlugOnPublish(ContentPublishedEvent $event)
  115.     {
  116.         if (!$event->getContent()->isImageset() || !$event->getContent()->isSlugEmpty()) {
  117.             return;
  118.         }
  119.         /** @var Content $content */
  120.         $content $event->getContent();
  121.         $slug $this->getSeoService()->generateSlug($content);
  122.         $content->getContent()->setSlug($slug);
  123.         $this->service->storeEntity($content);
  124.     }
  125. }