src/EventSubscriber/RegistrationFormSubscriber.php line 66

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\AppEvents;
  4. use App\Event\FormEvent;
  5. use App\Helper\PropertyHelper;
  6. use App\ValueObject\ReviewStatuses;
  7. use DateTime;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12. * Class RegistrationFormListener
  13. */
  14. class RegistrationFormSubscriber implements EventSubscriberInterface
  15. {
  16. /**
  17. * @var EntityManagerInterface
  18. */
  19. private $entityManager;
  20. /**
  21. * @var PropertyHelper
  22. */
  23. private $propertyHelper;
  24. /**
  25. * @var EventDispatcherInterface
  26. */
  27. private $eventDispatcher;
  28. /**
  29. * RegistrationFormListener constructor.
  30. *
  31. * @param EntityManagerInterface $entityManager
  32. * @param PropertyHelper $propertyHelper
  33. * @param EventDispatcherInterface $eventDispatcher
  34. */
  35. public function __construct(
  36. EntityManagerInterface $entityManager,
  37. PropertyHelper $propertyHelper,
  38. EventDispatcherInterface $eventDispatcher
  39. )
  40. {
  41. $this->entityManager = $entityManager;
  42. $this->propertyHelper = $propertyHelper;
  43. $this->eventDispatcher = $eventDispatcher;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function getSubscribedEvents(): array
  49. {
  50. return [
  51. AppEvents::REGISTRATION_FORM_CREATED => 'onRegistrationFormCreated',
  52. ];
  53. }
  54. /**
  55. * @param FormEvent $event
  56. * @throws \Exception
  57. */
  58. public function onRegistrationFormCreated(FormEvent $event): void
  59. {
  60. $form = $event->getForm();
  61. $property = $form->getProperty();
  62. $form->setStatus(ReviewStatuses::APPROVED);
  63. $property->setRegistrationDate(new DateTime());
  64. $nextRenewalDate = $this->propertyHelper->getNextRenewalDate($property);
  65. $property->setNextRenewalDate($nextRenewalDate);
  66. $this->entityManager->flush();
  67. }
  68. }