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

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