src/EventSubscriber/InspectionFormSubscriber.php line 44

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\AppEvents;
  4. use App\Event\FormEvent;
  5. use DateTime;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9. * Class InspectionFormListener
  10. */
  11. class InspectionFormSubscriber implements EventSubscriberInterface
  12. {
  13. /**
  14. * @var EntityManagerInterface
  15. */
  16. private $entityManager;
  17. /**
  18. * InspectionFormListener constructor.
  19. *
  20. * @param EntityManagerInterface $entityManager
  21. */
  22. public function __construct(EntityManagerInterface $entityManager)
  23. {
  24. $this->entityManager = $entityManager;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. AppEvents::INSPECTION_FORM_CREATED => 'onInspectionFormCreated',
  33. ];
  34. }
  35. /**
  36. * @param FormEvent $event
  37. */
  38. public function onInspectionFormCreated(FormEvent $event): void
  39. {
  40. $property = $event->getForm()->getProperty();
  41. $property->setInspectionDate(new DateTime());
  42. $this->entityManager->flush();
  43. }
  44. }