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

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