<?php declare(strict_types=1);
namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
use Shopware\Core\Content\Rule\RuleEntity;
use Shopware\Core\Content\Rule\RuleEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RulePayloadSubscriber implements EventSubscriberInterface
{
/**
* @var RulePayloadUpdater
*/
private $updater;
public function __construct(RulePayloadUpdater $updater)
{
$this->updater = $updater;
}
public static function getSubscribedEvents(): array
{
return [
RuleEvents::RULE_LOADED_EVENT => 'unserialize',
];
}
public function unserialize(EntityLoadedEvent $event): void
{
$this->indexIfNeeded($event);
/** @var RuleEntity $entity */
foreach ($event->getEntities() as $entity) {
$payload = $entity->getPayload();
if ($payload === null || !\is_string($payload)) {
continue;
}
$entity->setPayload(unserialize($payload));
}
}
private function indexIfNeeded(EntityLoadedEvent $event): void
{
$rules = [];
/** @var RuleEntity $rule */
foreach ($event->getEntities() as $rule) {
if ($rule->getPayload() === null && !$rule->isInvalid()) {
$rules[$rule->getId()] = $rule;
}
}
if (!\count($rules)) {
return;
}
$updated = $this->updater->update(array_keys($rules));
foreach ($updated as $id => $entity) {
$rules[$id]->assign($entity);
}
}
}