| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?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.
- */
- 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.
- */
- public function doPreTransform(TransformEvent $event): void
- {
- }
- /**
- * Evénement se passant après l'update d'un index ES.
- */
- 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.
- */
- private function educationNotationTransform(mixed $document, EducationNotation $educationNotation): void
- {
- $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
- $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
- }
- }
|