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

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