-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from delyriand/fix/reindex-events
Fix the reindex events, use the sylius resource event to reindex product
- Loading branch information
Showing
4 changed files
with
176 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' Search plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <[email protected]> | ||
* | ||
* 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\EventListener; | ||
|
||
use MonsieurBiz\SyliusSearchPlugin\Message\ProductReindexFromIds; | ||
use MonsieurBiz\SyliusSearchPlugin\Message\ProductToDeleteFromIds; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ProductEventListener | ||
{ | ||
private array $productIdsToDelete = []; | ||
|
||
private MessageBusInterface $messageBus; | ||
|
||
public function __construct(MessageBusInterface $messageBus) | ||
{ | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function dispatchProductReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $event->getSubject(); | ||
Assert::isInstanceOf($product, ProductInterface::class); | ||
|
||
$productReindexFromIdsMessage = new ProductReindexFromIds(); | ||
$productReindexFromIdsMessage->addProductId($product->getId()); | ||
|
||
$this->messageBus->dispatch($productReindexFromIdsMessage); | ||
} | ||
|
||
public function saveProductIdToDispatchReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $event->getSubject(); | ||
Assert::isInstanceOf($product, ProductInterface::class); | ||
|
||
$this->productIdsToDelete[] = $product->getId(); | ||
} | ||
|
||
public function dispatchDeleteProductReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductInterface $product */ | ||
$product = $event->getSubject(); | ||
Assert::isInstanceOf($product, ProductInterface::class); | ||
|
||
if (empty($this->productIdsToDelete)) { | ||
return; | ||
} | ||
|
||
$productReindexFromIdsMessage = new ProductToDeleteFromIds(); | ||
foreach ($this->productIdsToDelete as $productIdToDelete) { | ||
$productReindexFromIdsMessage->addProductId($productIdToDelete); | ||
} | ||
|
||
$this->productIdsToDelete = []; | ||
$this->messageBus->dispatch($productReindexFromIdsMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Monsieur Biz' Search plugin for Sylius. | ||
* | ||
* (c) Monsieur Biz <[email protected]> | ||
* | ||
* 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\EventListener; | ||
|
||
use MonsieurBiz\SyliusSearchPlugin\Message\ProductReindexFromIds; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ProductVariantEventListener | ||
{ | ||
private array $productIdsToReindex = []; | ||
|
||
private MessageBusInterface $messageBus; | ||
|
||
public function __construct(MessageBusInterface $messageBus) | ||
{ | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function dispatchProductVariantReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductVariantInterface $variant */ | ||
$variant = $event->getSubject(); | ||
Assert::isInstanceOf($variant, ProductVariantInterface::class); | ||
|
||
if (null === $product = $variant->getProduct()) { | ||
return; | ||
} | ||
|
||
$productReindexFromIdsMessage = new ProductReindexFromIds(); | ||
$productReindexFromIdsMessage->addProductId($product->getId()); | ||
|
||
$this->messageBus->dispatch($productReindexFromIdsMessage); | ||
} | ||
|
||
public function saveProductIdToDispatchReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductVariantInterface $variant */ | ||
$variant = $event->getSubject(); | ||
Assert::isInstanceOf($variant, ProductVariantInterface::class); | ||
|
||
if (null === $product = $variant->getProduct()) { | ||
return; | ||
} | ||
|
||
$this->productIdsToReindex[] = $product->getId(); | ||
} | ||
|
||
public function dispatchProductReindexMessage(GenericEvent $event): void | ||
{ | ||
/** @var ProductVariantInterface $variant */ | ||
$variant = $event->getSubject(); | ||
Assert::isInstanceOf($variant, ProductVariantInterface::class); | ||
|
||
if (empty($this->productIdsToReindex)) { | ||
return; | ||
} | ||
|
||
$productReindexFromIdsMessage = new ProductReindexFromIds(); | ||
foreach ($this->productIdsToReindex as $productIdToReindex) { | ||
$productReindexFromIdsMessage->addProductId($productIdToReindex); | ||
} | ||
|
||
$this->productIdsToReindex = []; | ||
// @TODO For the moment, this message doesn't work. We send product id but the product is deleted … Refactor the code to use document ids! | ||
// $this->messageBus->dispatch($productReindexFromIdsMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters