Update subscriber to a listener as that's technically what it is
This commit is contained in:
+15
-14
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\SearchBundle\EventSubscriber;
|
||||
|
||||
use Pcm\SearchBundle\Entity\Interface\SearchableInterface;
|
||||
use Pcm\SearchBundle\Interface\SearchableInterface;
|
||||
use Pcm\SearchBundle\Service\SearchService;
|
||||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
|
||||
use Doctrine\ORM\Event\PostPersistEventArgs;
|
||||
@@ -15,26 +15,20 @@ use Doctrine\ORM\Events;
|
||||
#[AsDoctrineListener(event: Events::postPersist)]
|
||||
#[AsDoctrineListener(event: Events::postUpdate)]
|
||||
#[AsDoctrineListener(event: Events::preRemove)]
|
||||
class SearchableSubscriber
|
||||
final class SearchableListener
|
||||
{
|
||||
public function __construct(private SearchService $searchService) {}
|
||||
|
||||
public function postUpdate(PostUpdateEventArgs $args): void
|
||||
public function __construct(private SearchService $searchService)
|
||||
{
|
||||
$entity = $args->getObject();
|
||||
|
||||
if ($entity instanceof SearchableInterface) {
|
||||
$this->searchService->index($entity);
|
||||
}
|
||||
}
|
||||
|
||||
public function postPersist(PostPersistEventArgs $args): void
|
||||
{
|
||||
$entity = $args->getObject();
|
||||
$this->reindex($args->getObject());
|
||||
}
|
||||
|
||||
if ($entity instanceof SearchableInterface) {
|
||||
$this->searchService->index($entity);
|
||||
}
|
||||
public function postUpdate(PostUpdateEventArgs $args): void
|
||||
{
|
||||
$this->reindex($args->getObject());
|
||||
}
|
||||
|
||||
public function preRemove(PreRemoveEventArgs $args): void
|
||||
@@ -45,4 +39,11 @@ class SearchableSubscriber
|
||||
$this->searchService->unIndex($entity);
|
||||
}
|
||||
}
|
||||
|
||||
private function reindex(object $entity): void
|
||||
{
|
||||
if ($entity instanceof SearchableInterface) {
|
||||
$this->searchService->index($entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user