src/EventListener/ElasticIndexerSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Admin\Trait\GeneralTrait;
  4. use App\Entity\Person;
  5. use App\Entity\Place;
  6. use App\Repository\PersonRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use FOS\ElasticaBundle\Event\PreTransformEvent;
  9. use FOS\ElasticaBundle\Event\PostTransformEvent;
  10. use FOS\ElasticaBundle\Event\PostIndexPopulateEvent;
  11. use FOS\ElasticaBundle\Event\PreIndexPopulateEvent;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Elastica\Document;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. use Symfony\Component\Security\Http\SecurityEvents;
  16. class ElasticIndexerSubscriber implements EventSubscriberInterface
  17. {
  18.     use GeneralTrait;
  19.     /**
  20.      * @var PersonRepository|null
  21.      */
  22.     protected ?PersonRepository $personRepository;
  23.     /**
  24.      * @var EntityManagerInterface|null
  25.      */
  26.     private ?EntityManagerInterface $em;
  27.     public function __construct(
  28.         EntityManagerInterface $em,
  29.         PersonRepository $personRepository
  30.     ) {
  31.         $this->em $em;
  32.         $this->personRepository $personRepository;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             PreTransformEvent::class => 'preTransform',
  38.             PostTransformEvent::class => 'postTransform',
  39.             PreIndexPopulateEvent::class => 'preIndex',
  40.         ];
  41.     }
  42.     public function preIndex(PreIndexPopulateEvent $event)
  43.     {
  44.         // dd($event);
  45.     }
  46.     public function preTransform(PreTransformEvent $event)
  47.     {
  48.         // dd($event);
  49.     }
  50.     public function postTransform(PostTransformEvent $event)
  51.     {
  52.         /*
  53.         $document = $event->getDocument();
  54.         if ($document instanceof Document) {
  55.             $reponame = lcfirst($document->getIndex()) . 'Repository';
  56.             $settings = [
  57.                 'id'         => $document->getId(),
  58.                 'index'      => $document->getIndex(),
  59.                 'entity'     => '\\App\Entity\\' . $this->underscoredToUpperCamelCase($document->getIndex()),
  60.                 'repository' => $reponame,
  61.             ];
  62.             $repository = $this->$reponame; // the repository has to be constructed in the calling class
  63.             $instance = $repository->findOneById($settings['id']);
  64.             if ($instance instanceof Person) {
  65.                 $placeofbirth = $instance->getPlaceofbirth();
  66.                 if ($placeofbirth instanceof Place) {
  67.                     $placeofbirth = $this->renderPlace($placeofbirth);
  68.                     // dd($placeofbirth);
  69.                 }
  70.                 $document->set('placeofbirth', $placeofbirth);
  71.             }
  72.         }
  73.         */
  74.     }
  75.     public function renderPlace(Place $place): array
  76.     {
  77.         return [
  78.             'id'   => $place->getId(),
  79.             'iri'  => '/api/places/' $place->getId(),
  80.             'name' => $place->getName(),
  81.         ];
  82.     }
  83. }