ElasticaPostTransformSubscriber.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\Education\EducationNotation;
  5. use App\Service\Education\EducationNotationUtils;
  6. use FOS\ElasticaBundle\Event\PostTransformEvent;
  7. use FOS\ElasticaBundle\Event\PreTransformEvent;
  8. use FOS\ElasticaBundle\Event\TransformEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11. * Class ElasticaPostTransformSubscriber : Permet de réaliser des traitements avant et après un update d'un objet dans ES.
  12. */
  13. final class ElasticaPostTransformSubscriber implements EventSubscriberInterface
  14. {
  15. public function __construct(
  16. private EducationNotationUtils $educationNotationUtils,
  17. ) {
  18. }
  19. /**
  20. * ne se déclenche qu'après le post validate d'api platform.
  21. *
  22. * @return string[]
  23. */
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. PreTransformEvent::class => 'doPostTransform',
  28. PostTransformEvent::class => 'doPreTransform',
  29. ];
  30. }
  31. /**
  32. * Evénement se passant avant l'update d'un index ES.
  33. */
  34. public function doPreTransform(TransformEvent $event): void
  35. {
  36. }
  37. /**
  38. * Evénement se passant après l'update d'un index ES.
  39. */
  40. public function doPostTransform(TransformEvent $event): void
  41. {
  42. $document = $event->getDocument();
  43. $object = $event->getObject();
  44. if ($object instanceof EducationNotation) {
  45. $this->educationNotationTransform($document, $object);
  46. }
  47. }
  48. /**
  49. * Recalcul et met à jour les note d'une évaluation suivant la note maximal configurée par la structure.
  50. */
  51. private function educationNotationTransform(mixed $document, EducationNotation $educationNotation): void
  52. {
  53. $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
  54. $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
  55. }
  56. }