vendor/shopware/core/Content/Rule/DataAbstractionLayer/RulePayloadSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
  3. use Shopware\Core\Content\Rule\RuleDefinition;
  4. use Shopware\Core\Content\Rule\RuleEntity;
  5. use Shopware\Core\Content\Rule\RuleEvents;
  6. use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class RulePayloadSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var CacheClearer
  13.      */
  14.     private $cacheClearer;
  15.     /**
  16.      * @var RulePayloadUpdater
  17.      */
  18.     private $updater;
  19.     public function __construct(RulePayloadUpdater $updaterCacheClearer $cacheClearer)
  20.     {
  21.         $this->updater $updater;
  22.         $this->cacheClearer $cacheClearer;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             RuleEvents::RULE_LOADED_EVENT => 'unserialize',
  28.         ];
  29.     }
  30.     public function unserialize(EntityLoadedEvent $event): void
  31.     {
  32.         $this->indexIfNeeded($event);
  33.         /** @var RuleEntity $entity */
  34.         foreach ($event->getEntities() as $entity) {
  35.             if (!$entity->getPayload() || !\is_string($entity->getPayload())) {
  36.                 continue;
  37.             }
  38.             $unserialized unserialize($entity->getPayload());
  39.             $entity->setPayload($unserialized);
  40.         }
  41.     }
  42.     private function indexIfNeeded(EntityLoadedEvent $event): void
  43.     {
  44.         $rules = [];
  45.         /** @var RuleEntity $rule */
  46.         foreach ($event->getEntities() as $rule) {
  47.             if ($rule->getPayload() === null && !$rule->isInvalid()) {
  48.                 $rules[$rule->getId()] = $rule;
  49.             }
  50.         }
  51.         if (!\count($rules)) {
  52.             return;
  53.         }
  54.         $updated $this->updater->update(array_keys($rules));
  55.         foreach ($updated as $id => $entity) {
  56.             $rules[$id]->assign($entity);
  57.         }
  58.         $this->cacheClearer->invalidateIds(array_keys($updated), RuleDefinition::ENTITY_NAME);
  59.     }
  60. }