src/EventSubscriber/RenewalFormSubscriber.php line 71

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