From bada6a8fa9e3edbba78dfd15a85d29ee06bb66b7 Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Tue, 20 Sep 2022 16:39:17 +0200 Subject: [PATCH 1/2] feat: add an automatic reindex manager to enable or disable event --- .../ReindexProductEventSubscriber.php | 14 ++++++++- .../AutomaticProductReindexManager.php | 29 +++++++++++++++++++ .../AutomaticReindexManagerInterface.php | 21 ++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Manager/AutomaticProductReindexManager.php create mode 100644 src/Manager/AutomaticReindexManagerInterface.php diff --git a/src/EventSubscriber/ReindexProductEventSubscriber.php b/src/EventSubscriber/ReindexProductEventSubscriber.php index 13d801e4..40380217 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() @@ -64,6 +68,10 @@ public function getSubscribedEvents() 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 +79,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); 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; +} From f4a7ff32899924955effa18453749962d78b316d Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Tue, 20 Sep 2022 16:42:48 +0200 Subject: [PATCH 2/2] fix: reset the dispatch flag in the event subscribe on clear event --- src/EventSubscriber/ReindexProductEventSubscriber.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/ReindexProductEventSubscriber.php b/src/EventSubscriber/ReindexProductEventSubscriber.php index 40380217..445b0255 100644 --- a/src/EventSubscriber/ReindexProductEventSubscriber.php +++ b/src/EventSubscriber/ReindexProductEventSubscriber.php @@ -63,6 +63,7 @@ public function getSubscribedEvents() return [ Events::onFlush => 'onFlush', Events::postFlush => 'postFlush', + Events::onClear => 'onClear', ]; } @@ -82,7 +83,7 @@ public function postFlush(PostFlushEventArgs $args): void if (!$this->automaticReindexManager->shouldBeAutomaticallyReindex()) { return; } - + $unitOfWork = $args->getEntityManager()->getUnitOfWork(); $this->manageUnitOfWork($unitOfWork); @@ -102,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) {