<?php declare(strict_types=1);
namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Cart\CartRuleLoader;
use Shopware\Core\Content\Rule\Event\RuleIndexerEvent;
use Shopware\Core\Content\Rule\RuleDefinition;
use Shopware\Core\Content\Rule\RuleEvents;
use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class RuleIndexer extends EntityIndexer implements EventSubscriberInterface
{
/**
* @var IteratorFactory
*/
private $iteratorFactory;
/**
* @var Connection
*/
private $connection;
/**
* @var EntityRepositoryInterface
*/
private $repository;
/**
* @var CacheClearer
*/
private $cacheClearer;
/**
* @var RulePayloadUpdater
*/
private $payloadUpdater;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var CartRuleLoader
*/
private $cartRuleLoader;
public function __construct(
Connection $connection,
IteratorFactory $iteratorFactory,
EntityRepositoryInterface $repository,
CacheClearer $cacheClearer,
RulePayloadUpdater $payloadUpdater,
CartRuleLoader $cartRuleLoader,
EventDispatcherInterface $eventDispatcher
) {
$this->iteratorFactory = $iteratorFactory;
$this->repository = $repository;
$this->connection = $connection;
$this->cacheClearer = $cacheClearer;
$this->payloadUpdater = $payloadUpdater;
$this->eventDispatcher = $eventDispatcher;
$this->cartRuleLoader = $cartRuleLoader;
}
public function getName(): string
{
return 'rule.indexer';
}
public static function getSubscribedEvents(): array
{
return [
PluginPostInstallEvent::class => 'refreshPlugin',
PluginPostActivateEvent::class => 'refreshPlugin',
PluginPostUpdateEvent::class => 'refreshPlugin',
PluginPostDeactivateEvent::class => 'refreshPlugin',
PluginPostUninstallEvent::class => 'refreshPlugin',
RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
];
}
public function refreshPlugin(): void
{
// Delete the payload and invalid flag of all rules
$update = new RetryableQuery(
$this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
);
$update->execute();
// invalidates all cached queries to the `rule` table
$this->cacheClearer->invalidateTags(['entity_' . RuleDefinition::ENTITY_NAME]);
}
public function iterate($offset): ?EntityIndexingMessage
{
$iterator = $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
$ids = $iterator->fetch();
if (empty($ids)) {
return null;
}
return new RuleIndexingMessage(array_values($ids), $iterator->getOffset());
}
public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
{
$updates = $event->getPrimaryKeys(RuleDefinition::ENTITY_NAME);
if (empty($updates)) {
return null;
}
$this->handle(new RuleIndexingMessage(array_values($updates), null, $event->getContext()));
return null;
}
public function handle(EntityIndexingMessage $message): void
{
$ids = $message->getData();
$ids = array_unique(array_filter($ids));
if (empty($ids)) {
return;
}
$this->payloadUpdater->update($ids);
$this->eventDispatcher->dispatch(new RuleIndexerEvent($ids, $message->getContext()));
$this->cacheClearer->invalidateIds($ids, RuleDefinition::ENTITY_NAME);
}
public function onRuleWritten(EntityWrittenEvent $event): void
{
$this->cartRuleLoader->reset();
}
}