vendor/pimcore/portal-engine/src/EventSubscriber/IndexUpdateSubscriber.php line 189

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\EventSubscriber;
  12. use Pimcore\AssetMetadataClassDefinitionsBundle\Event\AssetMetadataConfigurationEvents;
  13. use Pimcore\AssetMetadataClassDefinitionsBundle\Event\Model\Asset\ConfigurationEvent;
  14. use Pimcore\Bundle\PortalEngineBundle\Enum\Index\DatabaseConfig;
  15. use Pimcore\Bundle\PortalEngineBundle\Enum\Index\Statistics\ElasticSearchAlias;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\Asset\IndexService as AssetIndexService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\DataObject\IndexService as DataObjectIndexService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\IndexQueueService;
  19. use Pimcore\Event\AssetEvents;
  20. use Pimcore\Event\DataObjectClassDefinitionEvents;
  21. use Pimcore\Event\DataObjectEvents;
  22. use Pimcore\Event\Model\AssetEvent;
  23. use Pimcore\Event\Model\DataObject\ClassDefinitionEvent;
  24. use Pimcore\Event\Model\DataObjectEvent;
  25. use Pimcore\Event\Model\TagEvent;
  26. use Pimcore\Event\TagEvents;
  27. use Pimcore\Logger;
  28. use Pimcore\Model\Asset;
  29. use Pimcore\Model\DataObject\AbstractObject;
  30. use Pimcore\Model\Element\Service;
  31. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  32. /**
  33.  * Class IndexUpdateSubscriber
  34.  *
  35.  * @package Pimcore\Bundle\PortalEngineBundle\EventSubsriber
  36.  */
  37. class IndexUpdateSubscriber implements EventSubscriberInterface
  38. {
  39.     /** @var IndexQueueService */
  40.     protected $indexQueueService;
  41.     /** @var DataObjectIndexService */
  42.     protected $dataObjectIndexService;
  43.     /** @var AssetIndexService */
  44.     protected $assetIndexService;
  45.     /**
  46.      * IndexUpdateSubscriber constructor.
  47.      *
  48.      * @param IndexQueueService $indexQueueService
  49.      * @param DataObjectIndexService $dataObjectIndexService
  50.      * @param AssetIndexService $assetIndexService
  51.      */
  52.     public function __construct(IndexQueueService $indexQueueServiceDataObjectIndexService $dataObjectIndexServiceAssetIndexService $assetIndexService)
  53.     {
  54.         $this->indexQueueService $indexQueueService;
  55.         $this->dataObjectIndexService $dataObjectIndexService;
  56.         $this->assetIndexService $assetIndexService;
  57.     }
  58.     /**
  59.      * Returns an array of event names this subscriber wants to listen to.
  60.      *
  61.      * The array keys are event names and the value can be:
  62.      *
  63.      *  * The method name to call (priority defaults to 0)
  64.      *  * An array composed of the method name to call and the priority
  65.      *  * An array of arrays composed of the method names to call and respective
  66.      *    priorities, or 0 if unset
  67.      *
  68.      * For instance:
  69.      *
  70.      *  * ['eventName' => 'methodName']
  71.      *  * ['eventName' => ['methodName', $priority]]
  72.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  73.      *
  74.      * @return array The event names to listen to
  75.      */
  76.     public static function getSubscribedEvents()
  77.     {
  78.         return [
  79.             DataObjectEvents::POST_UPDATE => 'updateDataObject',
  80.             DataObjectEvents::POST_ADD => 'updateDataObject',
  81.             DataObjectEvents::PRE_DELETE => 'deleteDataObject',
  82.             DataObjectClassDefinitionEvents::POST_UPDATE => 'updateDataObjectMapping',
  83.             DataObjectClassDefinitionEvents::POST_ADD => 'addDataObjectMapping',
  84.             DataObjectClassDefinitionEvents::POST_DELETE => 'deleteDataObjectIndex',
  85.             AssetEvents::POST_UPDATE => 'updateAsset',
  86.             AssetEvents::POST_ADD => 'updateAsset',
  87.             AssetEvents::POST_DELETE => 'deleteAsset',
  88.             AssetMetadataConfigurationEvents::POST_UPDATE => 'updateAssetMapping',
  89.             AssetMetadataConfigurationEvents::POST_DELETE => 'updateAssetMapping',
  90.             TagEvents::PRE_DELETE => 'deleteTag',
  91.             TagEvents::POST_ADD_TO_ELEMENT => 'updateTagAssignment',
  92.             TagEvents::POST_REMOVE_FROM_ELEMENT => 'updateTagAssignment',
  93.         ];
  94.     }
  95.     /**
  96.      * @param DataObjectEvent $event
  97.      */
  98.     public function updateDataObject(DataObjectEvent $event)
  99.     {
  100.         $inheritanceBackup AbstractObject::getGetInheritedValues();
  101.         AbstractObject::setGetInheritedValues(true);
  102.         $dataObject $event->getObject();
  103.         if ($dataObject instanceof AbstractObject) {
  104.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  105.         }
  106.         AbstractObject::setGetInheritedValues($inheritanceBackup);
  107.     }
  108.     /**
  109.      * @param DataObjectEvent $event
  110.      */
  111.     public function deleteDataObject(DataObjectEvent $event)
  112.     {
  113.         $dataObject $event->getObject();
  114.         if ($dataObject instanceof AbstractObject) {
  115.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  116.         }
  117.     }
  118.     /**
  119.      * @param ClassDefinitionEvent $event
  120.      */
  121.     public function addDataObjectMapping(ClassDefinitionEvent $event)
  122.     {
  123.         $classDefinition $event->getClassDefinition();
  124.         $this->dataObjectIndexService->updateMapping($classDefinitiontrue);
  125.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  126.     }
  127.     /**
  128.      * @param ClassDefinitionEvent $event
  129.      */
  130.     public function updateDataObjectMapping(ClassDefinitionEvent $event)
  131.     {
  132.         $classDefinition $event->getClassDefinition();
  133.         $this->dataObjectIndexService->updateMapping($classDefinition);
  134.         $this->indexQueueService->updateDataObjects($classDefinition);
  135.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  136.     }
  137.     /**
  138.      * @param ClassDefinitionEvent $event
  139.      */
  140.     public function deleteDataObjectIndex(ClassDefinitionEvent $event)
  141.     {
  142.         $classDefinition $event->getClassDefinition();
  143.         try {
  144.             $this->dataObjectIndexService->deleteIndex($classDefinition);
  145.         } catch (\Exception $e) {
  146.             Logger::err($e);
  147.         }
  148.         $this->dataObjectIndexService->removeClassDefinitionFromAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  149.     }
  150.     /**
  151.      * @param AssetEvent $event
  152.      */
  153.     public function updateAsset(AssetEvent $event)
  154.     {
  155.         $asset $event->getAsset();
  156.         if ($asset instanceof Asset) {
  157.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  158.         }
  159.     }
  160.     /**
  161.      * @param AssetEvent $event
  162.      */
  163.     public function deleteAsset(AssetEvent $event)
  164.     {
  165.         $asset $event->getAsset();
  166.         if ($asset instanceof Asset) {
  167.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  168.         }
  169.     }
  170.     /**
  171.      * @param ConfigurationEvent $event
  172.      */
  173.     public function updateAssetMapping(ConfigurationEvent $event)
  174.     {
  175.         $this->assetIndexService->updateMapping();
  176.         $this->indexQueueService->updateAssets();
  177.     }
  178.     public function deleteTag(TagEvent $event)
  179.     {
  180.         $this->indexQueueService->updateByTag($event->getTag());
  181.     }
  182.     public function updateTagAssignment(TagEvent $event)
  183.     {
  184.         $element Service::getElementById($event->getArgument('elementType'), $event->getArgument('elementId'));
  185.         //only update when element is object or asset
  186.         if ($element instanceof AbstractObject || $element instanceof Asset) {
  187.             $this->indexQueueService->updateIndexQueue($elementDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  188.         }
  189.     }
  190. }