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

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 Shopware\Core\Framework\Rule\Container\Container;
  7. use Shopware\Core\Framework\Rule\Container\FilterRule;
  8. use Shopware\Core\Framework\Rule\ScriptRule;
  9. use Shopware\Core\Framework\Script\Debugging\ScriptTraces;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class RulePayloadSubscriber implements EventSubscriberInterface
  12. {
  13.     private RulePayloadUpdater $updater;
  14.     private ScriptTraces $traces;
  15.     private string $cacheDir;
  16.     private bool $debug;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(
  21.         RulePayloadUpdater $updater,
  22.         ScriptTraces $traces,
  23.         string $cacheDir,
  24.         bool $debug
  25.     ) {
  26.         $this->updater $updater;
  27.         $this->traces $traces;
  28.         $this->cacheDir $cacheDir;
  29.         $this->debug $debug;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             RuleEvents::RULE_LOADED_EVENT => 'unserialize',
  35.         ];
  36.     }
  37.     public function unserialize(EntityLoadedEvent $event): void
  38.     {
  39.         $this->indexIfNeeded($event);
  40.         /** @var RuleEntity $entity */
  41.         foreach ($event->getEntities() as $entity) {
  42.             $payload $entity->getPayload();
  43.             if ($payload === null || !\is_string($payload)) {
  44.                 continue;
  45.             }
  46.             $payload unserialize($payload);
  47.             $this->enrichConditions([$payload]);
  48.             $entity->setPayload($payload);
  49.         }
  50.     }
  51.     private function indexIfNeeded(EntityLoadedEvent $event): void
  52.     {
  53.         $rules = [];
  54.         /** @var RuleEntity $rule */
  55.         foreach ($event->getEntities() as $rule) {
  56.             if ($rule->getPayload() === null && !$rule->isInvalid()) {
  57.                 $rules[$rule->getId()] = $rule;
  58.             }
  59.         }
  60.         if (!\count($rules)) {
  61.             return;
  62.         }
  63.         $updated $this->updater->update(array_keys($rules));
  64.         foreach ($updated as $id => $entity) {
  65.             $rules[$id]->assign($entity);
  66.         }
  67.     }
  68.     private function enrichConditions(array $conditions): void
  69.     {
  70.         foreach ($conditions as $condition) {
  71.             if ($condition instanceof ScriptRule) {
  72.                 $condition->assign([
  73.                     'traces' => $this->traces,
  74.                     'cacheDir' => $this->cacheDir,
  75.                     'debug' => $this->debug,
  76.                 ]);
  77.                 continue;
  78.             }
  79.             if ($condition instanceof Container || $condition instanceof FilterRule) {
  80.                 $this->enrichConditions($condition->getRules());
  81.             }
  82.         }
  83.     }
  84. }