|
|
@@ -0,0 +1,68 @@
|
|
|
+<?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
|
|
|
+ */
|
|
|
+ public static function getSubscribedEvents()
|
|
|
+ {
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Evénement se passant après l'update d'un index ES
|
|
|
+ * @param TransformEvent $event
|
|
|
+ */
|
|
|
+ public function doPostTransform(TransformEvent $event)
|
|
|
+ {
|
|
|
+ $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($document, EducationNotation $educationNotation){
|
|
|
+ $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
|
|
|
+ $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
|
|
|
+ }
|
|
|
+}
|