<?php
namespace App\ElasticSearch\EventSubscriber;
use App\ElasticSearch\Service\AuctionIndexService;
use App\ElasticSearch\Service\HorseIndexService;
use App\ElasticSearch\Service\MarketplaceIndexService;
use App\ElasticSearch\Service\NewsIndexService;
use App\ElasticSearch\Service\ProfileIndexService;
use App\ElasticSearch\Service\RaceIndexService;
use App\ElasticSearch\Service\TimelinePostIndexService;
use Pimcore\Event\DataObjectEvents;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\TimelinePost;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class IndexSubscriber implements EventSubscriberInterface
{
protected static $dataObjects = ['Auction', 'Horse', 'Race', 'Profile', 'News', 'TimelinePost'];
public function __construct(
protected AuctionIndexService $auctionIndexService,
protected HorseIndexService $horseIndexService,
protected RaceIndexService $raceIndexService,
protected ProfileIndexService $profileIndexService,
protected NewsIndexService $newsIndexService,
protected TimelinePostIndexService $timelinePostIndexService,
protected MarketplaceIndexService $marketplaceIndexService,
protected RequestStack $requestStack
) {
}
public static function getSubscribedEvents()
{
return [
DataObjectEvents::POST_ADD => 'onDataObjectPostAdd',
DataObjectEvents::POST_UPDATE => 'onDataObjectPostUpdate',
DataObjectEvents::POST_DELETE => 'onDataObjectPostDelete',
];
}
public function onDataObjectPostAdd(DataObjectEvent $event)
{
if (!$this->requestStack) {
return;
}
$object = $event->getObject();
$className = (new \ReflectionClass($object))->getShortName();
if (!in_array($className, $this::$dataObjects)) {
return;
}
switch ($className) {
case 'Auction': $this->auctionIndexService->add($object); break;
case 'Horse': $this->horseIndexService->add($object); break;
case 'Race': $this->raceIndexService->add($object); break;
case 'Profile': $this->profileIndexService->add($object); break;
case 'News': $this->newsIndexService->add($object); break;
case 'TimelinePost': $this->addTimelinePost($object);
}
}
public function onDataObjectPostUpdate(DataObjectEvent $event)
{
if (!$this->requestStack) {
return;
}
$object = $event->getObject();
$className = (new \ReflectionClass($object))->getShortName();
if (!in_array($className, $this::$dataObjects)) {
return;
}
switch ($className) {
case 'Auction': $this->auctionIndexService->update($object); break;
case 'Horse': $this->horseIndexService->update($object); break;
case 'Race': $this->raceIndexService->update($object); break;
case 'Profile': $this->profileIndexService->update($object); break;
case 'News': $this->newsIndexService->update($object); break;
case 'TimelinePost': $this->updateTimelinePost($object);
}
}
public function onDataObjectPostDelete(DataObjectEvent $event)
{
if (!$this->requestStack) {
return;
}
$object = $event->getObject();
$className = (new \ReflectionClass($object))->getShortName();
if (!in_array($className, $this::$dataObjects)) {
return;
}
switch ($className) {
case 'Auction': $this->auctionIndexService->delete($object); break;
case 'Horse': $this->horseIndexService->delete($object); break;
case 'Race': $this->raceIndexService->delete($object); break;
case 'Profile': $this->profileIndexService->delete($object); break;
case 'News': $this->newsIndexService->delete($object); break;
case 'TimelinePost': $this->deleteTimelinePost($object);
}
}
private function addTimelinePost(TimelinePost $post)
{
if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
$this->marketplaceIndexService->add($post);
}
$this->timelinePostIndexService->add($post);
}
private function updateTimelinePost(TimelinePost $post)
{
if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
$this->marketplaceIndexService->update($post);
}
$this->timelinePostIndexService->update($post);
}
private function deleteTimelinePost(TimelinePost $post)
{
if ($post->getPostType() && $post->getPostType()->getServiceTimelinePost()) {
$this->marketplaceIndexService->delete($post);
}
$this->timelinePostIndexService->delete($post);
}
}