Skip to content

Commit

Permalink
Merge pull request #138 from delyriand/feature/add-automatic-reindex-…
Browse files Browse the repository at this point in the history
…manager

Feature/add automatic reindex manager
  • Loading branch information
delyriand committed Sep 20, 2022
2 parents 7228e97 + f4a7ff3 commit fc74737
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/EventSubscriber/ReindexProductEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,30 +48,42 @@ 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()
{
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);
}

public function postFlush(PostFlushEventArgs $args): void
{
if (!$this->automaticReindexManager->shouldBeAutomaticallyReindex()) {
return;
}

$unitOfWork = $args->getEntityManager()->getUnitOfWork();
$this->manageUnitOfWork($unitOfWork);

Expand All @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions src/Manager/AutomaticProductReindexManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* 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;
}
}
21 changes: 21 additions & 0 deletions src/Manager/AutomaticReindexManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* 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;
}

0 comments on commit fc74737

Please sign in to comment.