vendor/abcpremium/data-object-quality-bundle/src/Event/Subscriber/HandleObjectSave.php line 45

Open in your IDE?
  1. <?php
  2. namespace Abcpremium\DataObjectQualityBundle\Event\Subscriber;
  3. use Abcpremium\DataObjectQualityBundle\Cache\BundleCache;
  4. use Abcpremium\DataObjectQualityBundle\Configuration\BundleConfiguration;
  5. use Abcpremium\DataObjectQualityBundle\Messenger\Message\UpdateDataQualityAsyncMessage;
  6. use Abcpremium\DataObjectQualityBundle\Messenger\Message\UpdateDataQualityMessage;
  7. use Abcpremium\DataObjectQualityBundle\Model\DataQuality\DataQualityService;
  8. use Pimcore\Event\DataObjectEvents;
  9. use Pimcore\Event\Model\DataObjectEvent;
  10. use Pimcore\Model\DataObject\Concrete;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. class HandleObjectSave implements EventSubscriberInterface
  14. {
  15.     private BundleConfiguration $bundleConfiguration;
  16.     private MessageBusInterface $messageBus;
  17.     private BundleCache $cache;
  18.     public function __construct(
  19.         BundleCache $cache,
  20.         BundleConfiguration $bundleConfiguration,
  21.         MessageBusInterface $messageBus
  22.     ) {
  23.         $this->bundleConfiguration $bundleConfiguration;
  24.         $this->messageBus $messageBus;
  25.         $this->cache $cache;
  26.     }
  27.     /**
  28.      * {@inheritDoc}
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             DataObjectEvents::POST_UPDATE => 'setDataQualityOfObject',
  34.         ];
  35.     }
  36.     public function setDataQualityOfObject(DataObjectEvent $dataObjectEvent): void
  37.     {
  38.         if (true === $this->isAutoSave($dataObjectEvent->getArguments())) {
  39.             return;
  40.         }
  41.         $element $dataObjectEvent->getElement();
  42.         if (!$element instanceof Concrete) {
  43.             return;
  44.         }
  45.         $this->cache->deleteResultFromCache($element);
  46.         $dataQualityPropertyName $this->bundleConfiguration->getPropertyNameForDataQuality($element->getClassId());
  47.         if (null === $dataQualityPropertyName) {
  48.             return;
  49.         }
  50.         $this->dispatchUpdateMessage($element$dataQualityPropertyName);
  51.     }
  52.     private function isAutoSave(array $eventArguments): bool
  53.     {
  54.         if (isset($eventArguments['isAutoSave']) && true === $eventArguments['isAutoSave']) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     private function dispatchUpdateMessage(Concrete $elementstring $dataQualityPropertyName): void
  60.     {
  61.         if (true === $this->bundleConfiguration->isCalculationOnSaveDoneAsync()) {
  62.             $updateMessage = new UpdateDataQualityAsyncMessage($element->getId(), $dataQualityPropertyName);
  63.         } else {
  64.             $updateMessage = new UpdateDataQualityMessage($element->getId(), $dataQualityPropertyName);
  65.         }
  66.         $this->messageBus->dispatch($updateMessage);
  67.     }
  68. }