| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- declare(strict_types=1);
- namespace App\EventSubscriber;
- use App\Entity\Education\EducationNotation;
- use App\Service\Education\EducationNotationUtils;
- use FOS\ElasticaBundle\Event\PostTransformEvent;
- use FOS\ElasticaBundle\Event\PreTransformEvent;
- use FOS\ElasticaBundle\Event\TransformEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- /**
- * Class ElasticaPostTransformSubscriber : Permet de réaliser des traitements avant et après un update d'un objet dans ES
- * @package App\EventSubscriber
- */
- final class ElasticaPostTransformSubscriber implements EventSubscriberInterface
- {
- public function __construct(
- private EducationNotationUtils $educationNotationUtils
- )
- {
- }
- /**
- * ne se déclenche qu'après le post validate d'api platform
- * @return string[]
- */
- public static function getSubscribedEvents(): array
- {
- return [
- PreTransformEvent::class => 'doPostTransform',
- PostTransformEvent::class => 'doPreTransform'
- ];
- }
- /**
- * Evénement se passant avant l'update d'un index ES
- * @param TransformEvent $event
- */
- public function doPreTransform(TransformEvent $event): void
- {
- }
- /**
- * Evénement se passant après l'update d'un index ES
- * @param TransformEvent $event
- */
- public function doPostTransform(TransformEvent $event): void
- {
- $document = $event->getDocument();
- $object = $event->getObject();
- if ($object instanceof EducationNotation) {
- $this->educationNotationTransform($document, $object);
- }
- }
- /**
- * Recalcul et met à jour les note d'une évaluation suivant la note maximal configurée par la structure
- * @param $document
- * @param $object
- */
- private function educationNotationTransform(mixed $document, EducationNotation $educationNotation): void{
- $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
- $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
- }
- }
|