<?php
namespace App\EventListener;
use App\Admin\Trait\GeneralTrait;
use App\Entity\Person;
use App\Entity\Place;
use App\Repository\PersonRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\ElasticaBundle\Event\PreTransformEvent;
use FOS\ElasticaBundle\Event\PostTransformEvent;
use FOS\ElasticaBundle\Event\PostIndexPopulateEvent;
use FOS\ElasticaBundle\Event\PreIndexPopulateEvent;
use Doctrine\ORM\EntityManagerInterface;
use Elastica\Document;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
class ElasticIndexerSubscriber implements EventSubscriberInterface
{
use GeneralTrait;
/**
* @var PersonRepository|null
*/
protected ?PersonRepository $personRepository;
/**
* @var EntityManagerInterface|null
*/
private ?EntityManagerInterface $em;
public function __construct(
EntityManagerInterface $em,
PersonRepository $personRepository
) {
$this->em = $em;
$this->personRepository = $personRepository;
}
public static function getSubscribedEvents(): array
{
return [
PreTransformEvent::class => 'preTransform',
PostTransformEvent::class => 'postTransform',
PreIndexPopulateEvent::class => 'preIndex',
];
}
public function preIndex(PreIndexPopulateEvent $event)
{
// dd($event);
}
public function preTransform(PreTransformEvent $event)
{
// dd($event);
}
public function postTransform(PostTransformEvent $event)
{
/*
$document = $event->getDocument();
if ($document instanceof Document) {
$reponame = lcfirst($document->getIndex()) . 'Repository';
$settings = [
'id' => $document->getId(),
'index' => $document->getIndex(),
'entity' => '\\App\Entity\\' . $this->underscoredToUpperCamelCase($document->getIndex()),
'repository' => $reponame,
];
$repository = $this->$reponame; // the repository has to be constructed in the calling class
$instance = $repository->findOneById($settings['id']);
if ($instance instanceof Person) {
$placeofbirth = $instance->getPlaceofbirth();
if ($placeofbirth instanceof Place) {
$placeofbirth = $this->renderPlace($placeofbirth);
// dd($placeofbirth);
}
$document->set('placeofbirth', $placeofbirth);
}
}
*/
}
public function renderPlace(Place $place): array
{
return [
'id' => $place->getId(),
'iri' => '/api/places/' . $place->getId(),
'name' => $place->getName(),
];
}
}