src/Hitso/Bundle/ContentBundle/EventListener/ArticleSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\ContentBundle\EventListener;
  4. use Faker\Factory;
  5. use Hitso\Bundle\CommonBundle\Doctrine\Event\EntityEvent;
  6. use Hitso\Bundle\ContentBundle\Entity\Article;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Hitso\Bundle\SearchBundle\Manager\SearchManager;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. class ArticleSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SearchManager
  14.      */
  15.     private $searchManager;
  16.     public function __construct(SearchManager $searchManager)
  17.     {
  18.         $this->searchManager $searchManager;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             'article.post_create' => 'onPostCreate',
  24.             'article.post_update' => 'onPostUpdate',
  25.         ];
  26.     }
  27.     public function onPostCreate(EntityEvent $event): void
  28.     {
  29.         $entity $event->getEntity();
  30.         if ($entity instanceof Article) {
  31.             try {
  32.                 $type      $this->searchManager->getType('article');
  33.                 $documents = new ArrayCollection();
  34.                 $document  $type->createDocument($entity);
  35.                 $documents->add($document);
  36.                 $this->searchManager->addDocuments($documents$type->getName());
  37.             } catch (\Exception $e) {
  38.             }
  39.         }
  40.     }
  41.     public function onPostUpdate(EntityEvent $event): void
  42.     {
  43.         $entity $event->getEntity();
  44.         if ($entity instanceof Article) {
  45.             try {
  46.                 $type     $this->searchManager->getType('article');
  47.                 $document $type->createDocument($entity);
  48.                 $this->searchManager->updateDocument($document);
  49.             } catch (\Exception $e) {
  50.             }
  51.         }
  52.     }
  53. }