diff --git a/src/EventSubscriber/ReindexProductEventSubscriber.php b/src/EventSubscriber/ReindexProductEventSubscriber.php index 13d801e4..445b0255 100644 --- a/src/EventSubscriber/ReindexProductEventSubscriber.php +++ b/src/EventSubscriber/ReindexProductEventSubscriber.php @@ -18,6 +18,7 @@ use Doctrine\ORM\Event\PostFlushEventArgs; use Doctrine\ORM\Events; use Doctrine\ORM\UnitOfWork; +use MonsieurBiz\SyliusSearchPlugin\Manager\AutomaticReindexManagerInterface; use MonsieurBiz\SyliusSearchPlugin\Message\ProductReindexFromIds; use MonsieurBiz\SyliusSearchPlugin\Message\ProductReindexFromTaxon; use MonsieurBiz\SyliusSearchPlugin\Message\ProductToDeleteFromIds; @@ -47,11 +48,14 @@ class ReindexProductEventSubscriber implements EventSubscriberInterface, LoggerA private MessageBusInterface $messageBus; + private AutomaticReindexManagerInterface $automaticReindexManager; + private bool $dispatched = false; - public function __construct(MessageBusInterface $messageBus) + public function __construct(MessageBusInterface $messageBus, AutomaticReindexManagerInterface $automaticReindexManager) { $this->messageBus = $messageBus; + $this->automaticReindexManager = $automaticReindexManager; } public function getSubscribedEvents() @@ -59,11 +63,16 @@ public function getSubscribedEvents() return [ Events::onFlush => 'onFlush', Events::postFlush => 'postFlush', + Events::onClear => 'onClear', ]; } public function onFlush(OnFlushEventArgs $eventArgs): void { + if (!$this->automaticReindexManager->shouldBeAutomaticallyReindex()) { + return; + } + $eventArgs->getEntityManager()->getEventManager()->removeEventListener(Events::onFlush, $this); $unitOfWork = $eventArgs->getEntityManager()->getUnitOfWork(); $this->manageUnitOfWork($unitOfWork); @@ -71,6 +80,10 @@ public function onFlush(OnFlushEventArgs $eventArgs): void public function postFlush(PostFlushEventArgs $args): void { + if (!$this->automaticReindexManager->shouldBeAutomaticallyReindex()) { + return; + } + $unitOfWork = $args->getEntityManager()->getUnitOfWork(); $this->manageUnitOfWork($unitOfWork); @@ -90,6 +103,11 @@ public function postFlush(PostFlushEventArgs $args): void } } + public function onClear(): void + { + $this->dispatched = false; + } + private function onFlushEntities(array $entities, string $type = 'insertionsOrUpdate'): void { foreach ($entities as $entity) { diff --git a/src/Manager/AutomaticProductReindexManager.php b/src/Manager/AutomaticProductReindexManager.php new file mode 100644 index 00000000..0c80bf30 --- /dev/null +++ b/src/Manager/AutomaticProductReindexManager.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusSearchPlugin\Manager; + +class AutomaticProductReindexManager implements AutomaticReindexManagerInterface +{ + private bool $shouldAutomaticallyReindex = true; + + public function shouldAutomaticallyReindex(bool $shouldAutomaticallyReindex): void + { + $this->shouldAutomaticallyReindex = $shouldAutomaticallyReindex; + } + + public function shouldBeAutomaticallyReindex(): bool + { + return $this->shouldAutomaticallyReindex; + } +} diff --git a/src/Manager/AutomaticReindexManagerInterface.php b/src/Manager/AutomaticReindexManagerInterface.php new file mode 100644 index 00000000..4d2d9f1e --- /dev/null +++ b/src/Manager/AutomaticReindexManagerInterface.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusSearchPlugin\Manager; + +interface AutomaticReindexManagerInterface +{ + public function shouldAutomaticallyReindex(bool $shouldAutomaticallyReindex): void; + + public function shouldBeAutomaticallyReindex(): bool; +}