3
0

First pass of the search bundle

This commit is contained in:
2022-07-20 09:23:11 +01:00
commit 06c5a90214
13 changed files with 392 additions and 0 deletions
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Pcm\SearchBundle\EventSubscriber;
use Pcm\SearchBundle\Entity\Interface\SearchableInterface;
use Pcm\SearchBundle\Service\SearchService;
use Doctrine\Common\EventSubscriber;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
class SearchableSubscriber implements EventSubscriber
{
public function __construct(private SearchService $searchService) {}
public function getSubscribedEvents()
{
return [
Events::postPersist,
Events::postUpdate,
Events::preRemove
];
}
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof SearchableInterface) {
$this->searchService->index($entity);
}
}
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof SearchableInterface) {
$this->searchService->index($entity);
}
}
public function preRemove(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if ($entity instanceof SearchableInterface) {
$this->searchService->unIndex($entity);
}
}
}