vendor/pimcore/data-hub/src/EventListener/DataChangeListener.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace Pimcore\Bundle\DataHubBundle\EventListener;
  15. use Pimcore\Bundle\DataHubBundle\Configuration;
  16. use Pimcore\Bundle\DataHubBundle\WorkspaceHelper;
  17. use Pimcore\Event\AssetEvents;
  18. use Pimcore\Event\DataObjectEvents;
  19. use Pimcore\Event\DocumentEvents;
  20. use Pimcore\Event\Model\AssetEvent;
  21. use Pimcore\Event\Model\DataObjectEvent;
  22. use Pimcore\Event\Model\DocumentEvent;
  23. use Pimcore\Model\Element\ValidationException;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. class DataChangeListener implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             DataObjectEvents::POST_UPDATE => 'onObjectUpdate',
  34.             DataObjectEvents::POST_DELETE => 'onObjectDelete',
  35.             DocumentEvents::POST_UPDATE   => 'onDocumentUpdate',
  36.             DocumentEvents::POST_DELETE   => 'onDocumentDelete',
  37.             AssetEvents::POST_UPDATE      => 'onAssetUpdate',
  38.             AssetEvents::POST_DELETE      => 'onAssetDelete',
  39.         ];
  40.     }
  41.     /**
  42.      * @param DataObjectEvent $e
  43.      *
  44.      * @throws ValidationException
  45.      */
  46.     public function onObjectUpdate(DataObjectEvent $e)
  47.     {
  48.         if (!$e->hasArgument('oldPath')) {
  49.             return;
  50.         }
  51.         $object $e->getObject();
  52.         $oldPath $e->getArgument('oldPath');
  53.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_OBJECTWorkspaceHelper::MODIFY_TYPE_REPLACE$oldPath$object->getRealFullPath());
  54.     }
  55.     /**
  56.      * @param DataObjectEvent $e
  57.      *
  58.      * @throws ValidationException
  59.      */
  60.     public function onObjectDelete(DataObjectEvent $e)
  61.     {
  62.         $object $e->getObject();
  63.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_OBJECTWorkspaceHelper::MODIFY_TYPE_DELETE$object->getRealFullPath(), null);
  64.     }
  65.     /**
  66.      * @param DocumentEvent $e
  67.      *
  68.      * @throws ValidationException
  69.      */
  70.     public function onDocumentUpdate(DocumentEvent $e)
  71.     {
  72.         if (!$e->hasArgument('oldPath')) {
  73.             return;
  74.         }
  75.         $document $e->getDocument();
  76.         $oldPath $e->getArgument('oldPath');
  77.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_DOCUMENTWorkspaceHelper::MODIFY_TYPE_REPLACE$oldPath$document->getRealFullPath());
  78.     }
  79.     /**
  80.      * @param DocumentEvent $e
  81.      *
  82.      * @throws ValidationException
  83.      */
  84.     public function onDocumentDelete(DocumentEvent $e)
  85.     {
  86.         $object $e->getDocument();
  87.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_DOCUMENTWorkspaceHelper::MODIFY_TYPE_DELETE$object->getRealFullPath(), null);
  88.     }
  89.     /**
  90.      * @param AssetEvent $e
  91.      *
  92.      * @throws ValidationException
  93.      */
  94.     public function onAssetUpdate(AssetEvent $e)
  95.     {
  96.         if (!$e->hasArgument('oldPath')) {
  97.             return;
  98.         }
  99.         $asset $e->getAsset();
  100.         $oldPath $e->getArgument('oldPath');
  101.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_ASSETWorkspaceHelper::MODIFY_TYPE_REPLACE$oldPath$asset->getRealFullPath());
  102.     }
  103.     /**
  104.      * @param AssetEvent $e
  105.      *
  106.      * @throws ValidationException
  107.      */
  108.     public function onAssetDelete(AssetEvent $e)
  109.     {
  110.         $asset $e->getAsset();
  111.         $this->checkConfiguration(WorkspaceHelper::MODIFY_SPACE_ASSETWorkspaceHelper::MODIFY_TYPE_DELETE$asset->getRealFullPath(), null);
  112.     }
  113.     /**
  114.      * @param string      $dataType
  115.      * @param string      $modificationType
  116.      * @param string      $searchValue
  117.      * @param string|null $replaceValue
  118.      *
  119.      * @throws ValidationException
  120.      */
  121.     protected function checkConfiguration($dataType$modificationType$searchValue$replaceValue)
  122.     {
  123.         $configList Configuration::getList();
  124.         if (!is_array($configList)) {
  125.             return;
  126.         }
  127.         /** @var Configuration $configurationEntity */
  128.         foreach ($configList as $configurationEntity) {
  129.             try {
  130.                 $entity WorkspaceHelper::modifyWorkspaceRowByType($configurationEntity$dataType$modificationType$searchValue$replaceValue);
  131.             } catch (\Throwable $e) {
  132.                 throw new ValidationException(sprintf('Could not modify workspace row: %s'$e->getMessage()), 0$e);
  133.             }
  134.             if (!$entity instanceof Configuration) {
  135.                 continue;
  136.             }
  137.             try {
  138.                 $entity->save();
  139.             } catch (\Throwable $e) {
  140.                 throw new ValidationException(sprintf('Could not save configuration: %s'$e->getMessage()), 0$e);
  141.             }
  142.         }
  143.     }
  144. }