src/ElasticSearch/EventSubscriber/IndexSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\ElasticSearch\EventSubscriber;
  3. use App\ElasticSearch\Service\AuctionIndexService;
  4. use App\ElasticSearch\Service\HorseIndexService;
  5. use App\ElasticSearch\Service\MarketplaceIndexService;
  6. use App\ElasticSearch\Service\NewsIndexService;
  7. use App\ElasticSearch\Service\ProfileIndexService;
  8. use App\ElasticSearch\Service\RaceIndexService;
  9. use App\ElasticSearch\Service\TimelinePostIndexService;
  10. use Pimcore\Event\DataObjectEvents;
  11. use Pimcore\Event\Model\DataObjectEvent;
  12. use Pimcore\Model\DataObject\TimelinePost;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class IndexSubscriber implements EventSubscriberInterface
  16. {
  17.   protected static $dataObjects = ['Auction''Horse''Race''Profile''News''TimelinePost'];
  18.   public function __construct(
  19.     protected AuctionIndexService $auctionIndexService,
  20.     protected HorseIndexService $horseIndexService,
  21.     protected RaceIndexService $raceIndexService,
  22.     protected ProfileIndexService $profileIndexService,
  23.     protected NewsIndexService $newsIndexService,
  24.     protected TimelinePostIndexService $timelinePostIndexService,
  25.     protected MarketplaceIndexService $marketplaceIndexService,
  26.     protected RequestStack $requestStack
  27.   ) {
  28.   }
  29.   public static function getSubscribedEvents()
  30.   {
  31.     return [
  32.       DataObjectEvents::POST_ADD => 'onDataObjectPostAdd',
  33.       DataObjectEvents::POST_UPDATE => 'onDataObjectPostUpdate',
  34.       DataObjectEvents::POST_DELETE => 'onDataObjectPostDelete',
  35.     ];
  36.   }
  37.   public function onDataObjectPostAdd(DataObjectEvent $event)
  38.   {
  39.     if (!$this->requestStack) {
  40.       return;
  41.     }
  42.     $object $event->getObject();
  43.     $className = (new \ReflectionClass($object))->getShortName();
  44.     if (!in_array($className$this::$dataObjects)) {
  45.       return;
  46.     }
  47.     switch ($className) {
  48.       case 'Auction'$this->auctionIndexService->add($object); break;
  49.       case 'Horse'$this->horseIndexService->add($object); break;
  50.       case 'Race'$this->raceIndexService->add($object); break;
  51.       case 'Profile'$this->profileIndexService->add($object); break;
  52.       case 'News'$this->newsIndexService->add($object); break;
  53.       case 'TimelinePost'$this->addTimelinePost($object);
  54.     }
  55.   }
  56.   public function onDataObjectPostUpdate(DataObjectEvent $event)
  57.   {
  58.     if (!$this->requestStack) {
  59.       return;
  60.     }
  61.     $object $event->getObject();
  62.     $className = (new \ReflectionClass($object))->getShortName();
  63.     if (!in_array($className$this::$dataObjects)) {
  64.       return;
  65.     }
  66.     switch ($className) {
  67.       case 'Auction'$this->auctionIndexService->update($object); break;
  68.       case 'Horse'$this->horseIndexService->update($object); break;
  69.       case 'Race'$this->raceIndexService->update($object); break;
  70.       case 'Profile'$this->profileIndexService->update($object); break;
  71.       case 'News'$this->newsIndexService->update($object); break;
  72.       case 'TimelinePost'$this->updateTimelinePost($object);
  73.     }
  74.   }
  75.   public function onDataObjectPostDelete(DataObjectEvent $event)
  76.   {
  77.     if (!$this->requestStack) {
  78.       return;
  79.     }
  80.     $object $event->getObject();
  81.     $className = (new \ReflectionClass($object))->getShortName();
  82.     if (!in_array($className$this::$dataObjects)) {
  83.       return;
  84.     }
  85.     switch ($className) {
  86.       case 'Auction'$this->auctionIndexService->delete($object); break;
  87.       case 'Horse'$this->horseIndexService->delete($object); break;
  88.       case 'Race'$this->raceIndexService->delete($object); break;
  89.       case 'Profile'$this->profileIndexService->delete($object); break;
  90.       case 'News'$this->newsIndexService->delete($object); break;
  91.       case 'TimelinePost'$this->deleteTimelinePost($object);
  92.     }
  93.   }
  94.   private function addTimelinePost(TimelinePost $post)
  95.   {
  96.     if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
  97.       $this->marketplaceIndexService->add($post);
  98.     }
  99.     $this->timelinePostIndexService->add($post);
  100.   }
  101.   private function updateTimelinePost(TimelinePost $post)
  102.   {
  103.     if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
  104.       $this->marketplaceIndexService->update($post);
  105.     }
  106.     $this->timelinePostIndexService->update($post);
  107.   }
  108.   private function deleteTimelinePost(TimelinePost $post)
  109.   {
  110.     if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
  111.       $this->marketplaceIndexService->delete($post);
  112.     }
  113.     $this->timelinePostIndexService->delete($post);
  114.   }
  115. }