src/EventListener/BlogPostEventListener.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Services\ElasticModelFactory;
  4. use App\Services\ElasticsearchService;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Model\DataObject\BlogPost;
  7. class BlogPostEventListener
  8. {
  9.     /**
  10.      * @param DataObjectEvent $event
  11.      * @return void
  12.      */
  13.     public function elasticsearchReindex(DataObjectEvent $event)
  14.     {
  15.         $object $event->getObject();
  16.         if (!$object instanceof BlogPost) return;
  17.         $elasticService = new ElasticsearchService();
  18.         $elasticFactory = new ElasticModelFactory();
  19.         $elasticModel $elasticFactory->make($object);
  20.         if (!$object->getPublished()) {
  21.             $elasticService->delete($elasticModel);
  22.         } else {
  23.             $elasticService->update($elasticModel);
  24.         }
  25.     }
  26.     /**
  27.      * @param DataObjectEvent $event
  28.      * @return void
  29.      */
  30.     public function elasticsearchDelete(DataObjectEvent $event)
  31.     {
  32.         $object $event->getObject();
  33.         if (!$object instanceof BlogPost) return;
  34.         $elasticService = new ElasticsearchService();
  35.         $elasticFactory = new ElasticModelFactory();
  36.         $elasticModel $elasticFactory->make($object);
  37.         $elasticService->delete($elasticModel);
  38.     }
  39. }