vendor/shopware/core/Content/Rule/DataAbstractionLayer/RuleIndexerSubscriber.php line 54

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\CartRuleLoader;
  5. use Shopware\Core\Content\Rule\RuleEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  9. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  10. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  11. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  12. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. /**
  15.  * @internal
  16.  */
  17. #[Package('business-ops')]
  18. class RuleIndexerSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(
  24.         private readonly Connection $connection,
  25.         private readonly CartRuleLoader $cartRuleLoader
  26.     ) {
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             PluginPostInstallEvent::class => 'refreshPlugin',
  32.             PluginPostActivateEvent::class => 'refreshPlugin',
  33.             PluginPostUpdateEvent::class => 'refreshPlugin',
  34.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  35.             PluginPostUninstallEvent::class => 'refreshPlugin',
  36.             RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
  37.         ];
  38.     }
  39.     public function refreshPlugin(): void
  40.     {
  41.         // Delete the payload and invalid flag of all rules
  42.         $update = new RetryableQuery(
  43.             $this->connection,
  44.             $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
  45.         );
  46.         $update->execute();
  47.     }
  48.     public function onRuleWritten(): void
  49.     {
  50.         $this->cartRuleLoader->invalidate();
  51.     }
  52. }