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

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.         //do not update index when auto save or only saving version
  101.         if (
  102.             ($event->hasArgument('isAutoSave') && $event->getArgument('isAutoSave')) ||
  103.             ($event->hasArgument('saveVersionOnly') && $event->getArgument('saveVersionOnly'))
  104.         ) {
  105.             return;
  106.         }
  107.         $inheritanceBackup AbstractObject::getGetInheritedValues();
  108.         AbstractObject::setGetInheritedValues(true);
  109.         $dataObject $event->getObject();
  110.         if ($dataObject instanceof AbstractObject) {
  111.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  112.         }
  113.         AbstractObject::setGetInheritedValues($inheritanceBackup);
  114.     }
  115.     /**
  116.      * @param DataObjectEvent $event
  117.      */
  118.     public function deleteDataObject(DataObjectEvent $event)
  119.     {
  120.         $dataObject $event->getObject();
  121.         if ($dataObject instanceof AbstractObject) {
  122.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  123.         }
  124.     }
  125.     /**
  126.      * @param ClassDefinitionEvent $event
  127.      */
  128.     public function addDataObjectMapping(ClassDefinitionEvent $event)
  129.     {
  130.         $classDefinition $event->getClassDefinition();
  131.         $this->dataObjectIndexService->updateMapping($classDefinitiontrue);
  132.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  133.     }
  134.     /**
  135.      * @param ClassDefinitionEvent $event
  136.      */
  137.     public function updateDataObjectMapping(ClassDefinitionEvent $event)
  138.     {
  139.         $classDefinition $event->getClassDefinition();
  140.         $this->dataObjectIndexService->updateMapping($classDefinition);
  141.         $this->indexQueueService->updateDataObjects($classDefinition);
  142.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  143.     }
  144.     /**
  145.      * @param ClassDefinitionEvent $event
  146.      */
  147.     public function deleteDataObjectIndex(ClassDefinitionEvent $event)
  148.     {
  149.         $classDefinition $event->getClassDefinition();
  150.         try {
  151.             $this->dataObjectIndexService->deleteIndex($classDefinition);
  152.         } catch (\Exception $e) {
  153.             Logger::err($e);
  154.         }
  155.         $this->dataObjectIndexService->removeClassDefinitionFromAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  156.     }
  157.     /**
  158.      * @param AssetEvent $event
  159.      */
  160.     public function updateAsset(AssetEvent $event)
  161.     {
  162.         $asset $event->getAsset();
  163.         if ($asset instanceof Asset) {
  164.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  165.         }
  166.     }
  167.     /**
  168.      * @param AssetEvent $event
  169.      */
  170.     public function deleteAsset(AssetEvent $event)
  171.     {
  172.         $asset $event->getAsset();
  173.         if ($asset instanceof Asset) {
  174.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  175.         }
  176.     }
  177.     /**
  178.      * @param ConfigurationEvent $event
  179.      */
  180.     public function updateAssetMapping(ConfigurationEvent $event)
  181.     {
  182.         $this->assetIndexService->updateMapping();
  183.         $this->indexQueueService->updateAssets();
  184.     }
  185.     public function deleteTag(TagEvent $event)
  186.     {
  187.         $this->indexQueueService->updateByTag($event->getTag());
  188.     }
  189.     public function updateTagAssignment(TagEvent $event)
  190.     {
  191.         $element Service::getElementById($event->getArgument('elementType'), $event->getArgument('elementId'));
  192.         //only update when element is object or asset
  193.         if ($element instanceof AbstractObject || $element instanceof Asset) {
  194.             $this->indexQueueService->updateIndexQueue($elementDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  195.         }
  196.     }
  197. }