<?php
namespace App\EventSubscriber;
use App\Event\AppEvents;
use App\Event\FormEvent;
use App\Helper\PropertyHelper;
use App\ValueObject\ReviewStatuses;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class RegistrationFormListener
*/
class RegistrationFormSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var PropertyHelper
*/
private $propertyHelper;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* RegistrationFormListener constructor.
*
* @param EntityManagerInterface $entityManager
* @param PropertyHelper $propertyHelper
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
EntityManagerInterface $entityManager,
PropertyHelper $propertyHelper,
EventDispatcherInterface $eventDispatcher
)
{
$this->entityManager = $entityManager;
$this->propertyHelper = $propertyHelper;
$this->eventDispatcher = $eventDispatcher;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
AppEvents::REGISTRATION_FORM_CREATED => 'onRegistrationFormCreated',
];
}
/**
* @param FormEvent $event
* @throws \Exception
*/
public function onRegistrationFormCreated(FormEvent $event)
{
$form = $event->getForm();
$property = $form->getProperty();
$form->setStatus(ReviewStatuses::APPROVED);
$property->setRegistrationDate(new DateTime());
$nextRenewalDate = $this->propertyHelper->getNextRenewalDate($property);
$property->setNextRenewalDate($nextRenewalDate);
$this->entityManager->flush();
}
}