Skip to content

Commit

Permalink
fix: avoid error if delete a product without ES
Browse files Browse the repository at this point in the history
  • Loading branch information
delyriand committed Dec 20, 2024
1 parent de29806 commit 4f4ce0a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/MessageHandler/ProductToDeleteFromIdsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,44 @@

namespace MonsieurBiz\SyliusSearchPlugin\MessageHandler;

use Exception;
use MonsieurBiz\SyliusSearchPlugin\Index\IndexerInterface;
use MonsieurBiz\SyliusSearchPlugin\Message\ProductToDeleteFromIds;
use MonsieurBiz\SyliusSearchPlugin\Model\Documentable\DocumentableInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Registry\ServiceRegistryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class ProductToDeleteFromIdsHandler implements MessageHandlerInterface
{
private IndexerInterface $indexer;

private ServiceRegistryInterface $documentableRegistry;
private LoggerInterface $logger;

public function __construct(
IndexerInterface $indexer,
ServiceRegistryInterface $documentableRegistry
ServiceRegistryInterface $documentableRegistry,
LoggerInterface $logger
) {
$this->indexer = $indexer;
$this->documentableRegistry = $documentableRegistry;
$this->logger = $logger;
}

public function __invoke(ProductToDeleteFromIds $message): void
{
/** @var DocumentableInterface $documentable */
$documentable = $this->documentableRegistry->get('search.documentable.monsieurbiz_product');

$this->indexer->deleteByDocumentIds(
$documentable,
$message->getProductIds()
);
try {
$this->indexer->deleteByDocumentIds(
$documentable,
$message->getProductIds()
);
} catch (Exception $e) {
$this->logger->error('An error occurred while deleting products from search index', [
'exception' => $e,
]);
}
}
}

0 comments on commit 4f4ce0a

Please sign in to comment.