src/Event/Admin/BlogPostSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Event\Admin;
  3. use App\EasyAdmin\Dto\Tag;
  4. use App\Entity\BlogPost;
  5. use App\Entity\BlogTag;
  6. use App\Repository\BlogContentRepository;
  7. use App\Repository\BlogTagRepository;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\String\Slugger\AsciiSlugger;
  12. class BlogPostSubscriber implements EventSubscriberInterface
  13. {
  14.     /** @var BlogTagRepository */
  15.     private $tagRepository;
  16.     /** @var BlogContentRepository */
  17.     private $blogContentRepository;
  18.     public function __construct(
  19.         BlogTagRepository     $tagRepository,
  20.         BlogContentRepository $blogContentRepository
  21.     )
  22.     {
  23.         $this->tagRepository         $tagRepository;
  24.         $this->blogContentRepository $blogContentRepository;
  25.     }
  26.     public function onBeforeEntityPersistedEvent(BeforeEntityPersistedEvent $event)
  27.     {
  28.         $entity $event->getEntityInstance();
  29.         if (!$entity instanceof BlogPost) {
  30.             return;
  31.         }
  32.         $this->prepareTags($entity);
  33.         $this->setContent($entity);
  34.     }
  35.     public function onBeforeEntityUpdatedEvent(BeforeEntityUpdatedEvent $event)
  36.     {
  37.         $entity $event->getEntityInstance();
  38.         if (!$entity instanceof BlogPost) {
  39.             return;
  40.         }
  41.         $this->prepareTags($entity);
  42.         $this->setContent($entity);
  43.     }
  44.     private function setContent(BlogPost $post)
  45.     {
  46.         $contents $post->getBlock();
  47.         foreach ($contents as $block) {
  48.             $this->blogContentRepository->add($block);
  49.             $block->setPost($post);
  50.         }
  51.         foreach ($post->getBlogContents() as $content) {
  52.             if (!$contents->contains($content)) {
  53.                 $post->removeBlogContent($content);
  54.             }
  55.         }
  56.     }
  57.     private function prepareTags(BlogPost $post)
  58.     {
  59.         if (!$post->getRawTagValues()) {
  60.             return;
  61.         }
  62.         $dto   $post->getRawTagValues();
  63.         $names array_map(function (Tag $tag) {
  64.             return mb_strtolower($tag->getTag());
  65.         }, $dto);
  66.         $tags $this->tagRepository
  67.             ->findByNames($names);
  68.         foreach ($tags as $tag) {
  69.             $post->addTag($tag);
  70.             $names array_filter($names, function (string $v) use ($tag) {
  71.                 return $v !== $tag->getName();
  72.             });
  73.         }
  74.         $slugger = new AsciiSlugger();
  75.         if (!empty($names)) {
  76.             foreach ($names as $newTag) {
  77.                 $tag = new BlogTag();
  78.                 $tag->setName($newTag);
  79.                 $tag->setSlug($slugger->slug($newTag));
  80.                 $this->tagRepository->add($tag);
  81.                 $post->addTag($tag);
  82.             }
  83.         }
  84.     }
  85.     public static function getSubscribedEvents(): array
  86.     {
  87.         return [
  88.             BeforeEntityPersistedEvent::class => 'onBeforeEntityPersistedEvent',
  89.             BeforeEntityUpdatedEvent::class   => 'onBeforeEntityUpdatedEvent'
  90.         ];
  91.     }
  92. }