ElasticaPostTransformSubscriber.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * @package App\EventSubscriber
  13. */
  14. final class ElasticaPostTransformSubscriber implements EventSubscriberInterface
  15. {
  16. public function __construct(
  17. private EducationNotationUtils $educationNotationUtils
  18. )
  19. {
  20. }
  21. /**
  22. * ne se déclenche qu'après le post validate d'api platform
  23. * @return string[]
  24. */
  25. public static function getSubscribedEvents(): array
  26. {
  27. return [
  28. PreTransformEvent::class => 'doPostTransform',
  29. PostTransformEvent::class => 'doPreTransform'
  30. ];
  31. }
  32. /**
  33. * Evénement se passant avant l'update d'un index ES
  34. * @param TransformEvent $event
  35. */
  36. public function doPreTransform(TransformEvent $event): void
  37. {
  38. }
  39. /**
  40. * Evénement se passant après l'update d'un index ES
  41. * @param TransformEvent $event
  42. */
  43. public function doPostTransform(TransformEvent $event): void
  44. {
  45. $document = $event->getDocument();
  46. $object = $event->getObject();
  47. if ($object instanceof EducationNotation) {
  48. $this->educationNotationTransform($document, $object);
  49. }
  50. }
  51. /**
  52. * Recalcul et met à jour les note d'une évaluation suivant la note maximal configurée par la structure
  53. * @param $document
  54. * @param $object
  55. */
  56. private function educationNotationTransform(mixed $document, EducationNotation $educationNotation): void{
  57. $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
  58. $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
  59. }
  60. }