src/EventSubscriber/ReviewSubscriber.php line 277

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Organization;
  4. use App\Helper\CustomFieldHelper;
  5. use App\Model\Builder\InspectionFormBuilder;
  6. use App\Model\Builder\RegistrationFormBuilder;
  7. use App\Event\AppEvents;
  8. use App\Event\FormCreatedEvent;
  9. use App\Event\FormEvent;
  10. use App\Event\Municipality\FeeEvent;
  11. use App\Event\Review\FeeEvent as ReviewFeeEvent;
  12. use App\Event\Review\FormEvent as ReviewFormEvent;
  13. use App\Event\Municipality\MunicipalityEvent;
  14. use App\Event\OrganizationEvent;
  15. use App\Event\Review\NotifyReviewEvent;
  16. use App\Event\Review\UserEvent;
  17. use App\Event\ReviewEvent;
  18. use App\Entity\RegistrationForm as EntityRegistrationForm;
  19. use App\Helper\PropertyHelper;
  20. use App\Model\Parser\ReviewDeregistrationForm;
  21. use App\Model\Parser\ReviewInspection;
  22. use App\Model\Parser\ReviewInvoice;
  23. use App\Model\Parser\ReviewMunicipality;
  24. use App\Model\Parser\ReviewMunicipalityFee;
  25. use App\Model\Parser\ReviewOrganization;
  26. use App\Model\Parser\ReviewProperty;
  27. use App\Model\Parser\ReviewRegistrationForm;
  28. use App\Model\Parser\ReviewRenewalForm;
  29. use App\Model\Parser\ReviewUser;
  30. use App\ValueObject\UserRoles;
  31. use Doctrine\ORM\EntityManagerInterface;
  32. use App\Entity\DeregistrationForm;
  33. use App\Entity\DeregistrationFormReview;
  34. use App\Entity\InspectionForm;
  35. use App\Entity\InspectionFormReview;
  36. use App\Entity\InvoiceReview;
  37. use App\Entity\MunicipalityFeeReview;
  38. use App\Entity\MunicipalityReview;
  39. use App\Entity\OrganizationReview;
  40. use App\Entity\PropertyReview;
  41. use App\Entity\RegistrationForm;
  42. use App\Entity\RegistrationFormReview;
  43. use App\Entity\RenewalForm;
  44. use App\Entity\RenewalFormReview;
  45. use App\Entity\User;
  46. use App\Entity\UserReview;
  47. use App\ValueObject\InvoiceTypes;
  48. use App\ValueObject\ReviewStatuses;
  49. use App\ValueObject\UserTypes;
  50. use App\ValueObject\FormTypes;
  51. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  52. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  53. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  54. /**
  55. * Class ReviewListener
  56. */
  57. class ReviewSubscriber implements EventSubscriberInterface
  58. {
  59. const ADMIN_REVIEW = true;
  60. /**
  61. * @var EntityManagerInterface
  62. */
  63. private $em;
  64. /**
  65. * @var CustomFieldHelper
  66. */
  67. protected $customFieldHelper;
  68. /**
  69. * @var TokenStorageInterface
  70. */
  71. private $tokenStorage;
  72. /**
  73. * @var EventDispatcherInterface
  74. */
  75. private $eventDispatcher;
  76. /**
  77. * @var PropertyHelper
  78. */
  79. private $propertyHelper;
  80. /**
  81. * ReviewListener constructor.
  82. *
  83. * @param EntityManagerInterface $em
  84. * @param CustomFieldHelper $customFieldHelper
  85. * @param TokenStorageInterface $tokenStorage
  86. * @param EventDispatcherInterface $eventDispatcher
  87. * @param PropertyHelper $propertyHelper
  88. */
  89. public function __construct(
  90. EntityManagerInterface $em,
  91. CustomFieldHelper $customFieldHelper,
  92. TokenStorageInterface $tokenStorage,
  93. EventDispatcherInterface $eventDispatcher,
  94. PropertyHelper $propertyHelper
  95. ) {
  96. $this->em = $em;
  97. $this->customFieldHelper = $customFieldHelper;
  98. $this->tokenStorage = $tokenStorage;
  99. $this->eventDispatcher = $eventDispatcher;
  100. $this->propertyHelper = $propertyHelper;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public static function getSubscribedEvents(): array
  106. {
  107. return [
  108. AppEvents::REVIEW_USER_CREATED => ['onUserCreated'],
  109. AppEvents::REVIEW_APPROVED => ['onReviewApproved'],
  110. AppEvents::REVIEW_DENIED => ['onReviewDenied'],
  111. AppEvents::REVIEW_INSPECTION_CREATED => ['onInspectionCreated'],
  112. AppEvents::REVIEW_MUNICIPALITY_CREATED => ['onMunicipalityCreated'],
  113. AppEvents::REVIEW_MUNICIPALITY_FEE_CREATED => ['onMunicipalityFeeCreated'],
  114. AppEvents::REVIEW_ORGANIZATION_CREATED => ['onOrganizationCreated'],
  115. AppEvents::REVIEW_REGISTRATION_FORM_CREATED => ['onRegistrationFormCreated'],
  116. AppEvents::REVIEW_RENEWAL_FORM_CREATED => ['onRenewalFormCreated'],
  117. AppEvents::REVIEW_DEREGISTRATION_FORM_CREATED => ['onDeregistrationFormCreated']
  118. ];
  119. }
  120. /**
  121. * @param UserEvent $event
  122. */
  123. public function onUserCreated(UserEvent $event): void
  124. {
  125. $currentUser = $this->getCurrentUser();
  126. $user = $event->getUser();
  127. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  128. $userReview = new UserReview($user, [], ReviewStatuses::PENDING, $currentUser);
  129. $this->em->persist($userReview);
  130. $this->em->flush();
  131. }
  132. }
  133. /**
  134. * @param OrganizationEvent $event
  135. */
  136. public function onOrganizationCreated(OrganizationEvent $event): void
  137. {
  138. $currentUser = $this->getCurrentUser();
  139. $organization = $event->getOrganization();
  140. $organizationReview = new OrganizationReview($organization, [], ReviewStatuses::PENDING, self::ADMIN_REVIEW, $currentUser);
  141. $this->em->persist($organizationReview);
  142. $this->em->flush();
  143. }
  144. /**
  145. * @param ReviewFormEvent $event
  146. */
  147. public function onInspectionCreated(ReviewFormEvent $event): void
  148. {
  149. $currentUser = $this->getCurrentUser();
  150. /** @var InspectionForm $inspection */
  151. $inspection = $event->getForm();
  152. if ($currentUser->hasRole($currentUser, UserRoles::ROLE_ADMIN_MUNICIPAL) || $currentUser->hasRole($currentUser, UserRoles::ROLE_MUNICIPAL)) {
  153. $defaultOrg = $this->em->getRepository(Organization::class)->findOneBy(['name' => 'A -- PLEASE SELECT IF NON-REGISTERING ORGANIZATION']);
  154. $inspection->setRegistrant($defaultOrg);
  155. }
  156. $this->em->persist($inspection);
  157. $this->em->flush();
  158. $inspection->setStatus(ReviewStatuses::PENDING);
  159. $needAdmin = $event->getNeedAdmin();
  160. $inspectionReview = new InspectionFormReview($inspection, [], ReviewStatuses::PENDING, $needAdmin, $currentUser);
  161. $this->em->persist($inspectionReview);
  162. $this->em->persist($inspection);
  163. $this->em->flush();
  164. }
  165. /**
  166. * @param ReviewFormEvent $event
  167. */
  168. public function onRegistrationFormCreated(ReviewFormEvent $event): void
  169. {
  170. $currentUser = $this->getCurrentUser();
  171. /** @var RegistrationForm $form */
  172. $form = $event->getForm();
  173. $form->setStatus(ReviewStatuses::PENDING);
  174. $builder = $event->getBuilder();
  175. $customFields = json_encode($builder->customFields);
  176. $needAdmin = $event->getNeedAdmin();
  177. $formReview = new RegistrationFormReview($form, [], ReviewStatuses::PENDING, $needAdmin, $currentUser, $customFields);
  178. if ($event->getPayByCheck()) {
  179. $this->addPayByCheckToFormReview($formReview);
  180. }
  181. $formReview->setForm($form);
  182. $this->em->persist($form);
  183. $this->em->persist($formReview);
  184. $this->em->flush();
  185. }
  186. /**
  187. * @param ReviewFormEvent $event
  188. */
  189. public function onDeregistrationFormCreated(ReviewFormEvent $event): void
  190. {
  191. $currentUser = $this->getCurrentUser();
  192. /** @var DeregistrationForm $form */
  193. $form = $event->getForm();
  194. $form->setStatus(ReviewStatuses::PENDING);
  195. $builder = $event->getBuilder();
  196. $customFields = json_encode($builder->customFields);
  197. $needAdmin = $event->getNeedAdmin();
  198. $formReview = new DeregistrationFormReview($form, [], ReviewStatuses::PENDING, $needAdmin, $currentUser, $customFields);
  199. $formReview->setForm($form);
  200. $this->em->persist($formReview);
  201. $this->em->persist($form);
  202. $this->em->flush();
  203. }
  204. /**
  205. * @param ReviewFormEvent $event
  206. */
  207. public function onRenewalFormCreated(ReviewFormEvent $event): void
  208. {
  209. $currentUser = $this->getCurrentUser();
  210. /** @var RenewalForm $form */
  211. $form = $event->getForm();
  212. $form->setStatus(ReviewStatuses::PENDING);
  213. $needAdmin = $event->getNeedAdmin();
  214. $builder = $event->getBuilder();
  215. $customFields = json_encode($builder->customFields);
  216. $formReview = new RenewalFormReview($form, [], ReviewStatuses::PENDING, $needAdmin, $currentUser, $customFields);
  217. if ($event->getPayByCheck()) {
  218. $this->addPayByCheckToFormReview($formReview);
  219. }
  220. $formReview->setForm($form);
  221. $this->em->persist($formReview);
  222. $this->em->persist($form);
  223. $this->em->flush();
  224. }
  225. public function addPayByCheckToFormReview($formReview): void
  226. {
  227. $customFields = $formReview->getCustomFields();
  228. $customFieldsData = json_decode($customFields, true);
  229. $customFieldsData['pay_by_check'] = true;
  230. $customFieldsData = json_encode($customFieldsData);
  231. $formReview->setCustomFields($customFieldsData);
  232. }
  233. /**
  234. * @param MunicipalityEvent $event
  235. */
  236. public function onMunicipalityCreated(MunicipalityEvent $event): void
  237. {
  238. $currentUser = $this->getCurrentUser();
  239. $municipality = $event->getMunicipality();
  240. $municipalityReview = new MunicipalityReview($municipality, [], ReviewStatuses::PENDING, $currentUser, self::ADMIN_REVIEW);
  241. $this->em->persist($municipalityReview);
  242. $this->em->flush();
  243. }
  244. /**
  245. * @param ReviewFeeEvent $event
  246. */
  247. public function onMunicipalityFeeCreated(ReviewFeeEvent $event): void
  248. {
  249. $currentUser = $this->getCurrentUser();
  250. $fee = $event->getFee();
  251. $feeReview = new MunicipalityFeeReview($fee, [], ReviewStatuses::PENDING, self::ADMIN_REVIEW, $currentUser);
  252. $this->em->persist($feeReview);
  253. $this->em->flush();
  254. }
  255. /**
  256. * @param ReviewEvent $event
  257. */
  258. public function onReviewApproved(ReviewEvent $event): void
  259. {
  260. $entity = null;
  261. $eventType = null;
  262. $newEntityEvent = null;
  263. $review = $event->getReview();
  264. $usedPayByCheckInRegistrationAndRenewal = false;
  265. if ($review instanceof UserReview) {
  266. $entity = ReviewUser::parse($review, $review->getUser());
  267. if ($review->getType() == UserTypes::REGISTER_PARTY
  268. && $review->getReviewCreator() == null
  269. ) {
  270. //registered user.
  271. $eventType = AppEvents::USER_REGISTRATION_APPROVED;
  272. $newEntityEvent = new UserEvent($entity);
  273. }
  274. }
  275. if ($review instanceof MunicipalityReview) {
  276. $entity = ReviewMunicipality::parse($review, $review->getMunicipality());
  277. if ($review->getMunicipality() == null) {
  278. //Is a new Municipality.
  279. $eventType = AppEvents::MUNICIPALITY_CREATED;
  280. $newEntityEvent = new MunicipalityEvent($entity);
  281. }
  282. }
  283. if ($review instanceof OrganizationReview) {
  284. $entity = ReviewOrganization::parse($review, $review->getOrganization());
  285. }
  286. if ($review instanceof InspectionFormReview) {
  287. $entity = ReviewInspection::parse($review, $review->getForm());
  288. $entity->setStatus(ReviewStatuses::APPROVED);
  289. $eventType = AppEvents::INSPECTION_FORM_CREATED;
  290. $newEntityEvent = new FormEvent($entity);
  291. // if ($review->getForm() !== null) $review->setForm(null);
  292. //
  293. // if ($review->getForm() == null) {
  294. // //Is a new Inspection
  295. // $eventType = AppEvents::INSPECTION_FORM_CREATED;
  296. // $newEntityEvent = new FormEvent($entity);
  297. // }
  298. }
  299. if ($review instanceof PropertyReview) {
  300. $entity = ReviewProperty::parse($review, $review->getProperty());
  301. }
  302. if ($review instanceof MunicipalityFeeReview) {
  303. $entity = ReviewMunicipalityFee::parse($review, $review->getMunicipalityFee());
  304. if ($review->getMunicipalityFee() == null) {
  305. //Is a new MunicipalityFee.
  306. $eventType = AppEvents::MUNICIPALITY_FEE_CREATED;
  307. } else {
  308. $eventType = AppEvents::MUNICIPALITY_FEE_UPDATED;
  309. }
  310. $newEntityEvent = new FeeEvent($entity);
  311. }
  312. if ($review instanceof InvoiceReview) {
  313. $entity = ReviewInvoice::parse($review, $review->getInvoice());
  314. }
  315. if ($review instanceof RegistrationFormReview) {
  316. $entity = ReviewRegistrationForm::parse($review, $review->getForm(), true);
  317. if ($review->getForm() !== null) $review->setForm(null);
  318. $reviewCustomFields = json_decode($review->getCustomFields(), true);
  319. $usedPayByCheckInRegistrationAndRenewal = array_key_exists('pay_by_check', $reviewCustomFields) ? $reviewCustomFields['pay_by_check'] : false;
  320. $property = $entity->getProperty();
  321. $registrationForm = new EntityRegistrationForm($property);
  322. $customFields = $this->customFieldHelper->getCustomFields($property->getMunicipality(), FormTypes::REGISTRATION);
  323. $builder = RegistrationFormBuilder::fromEntity($registrationForm, $customFields);
  324. $builder->customFields = json_decode($review->getCustomFields(), true);
  325. $entity->setStartDate(new \DateTime(PropertyHelper::DELAY_FOR_INVOICE));
  326. if ($this->propertyHelper->getNextRenewalDate($entity->getProperty()))
  327. $entity->setEndDate($this->propertyHelper->getNextRenewalDate($entity->getProperty()));
  328. if ($review->getForm() == null) {
  329. $eventType = AppEvents::REGISTRATION_FORM_CREATED;
  330. $invoiceEvent = new FormCreatedEvent($entity, InvoiceTypes::VPR_REGISTRATION, null, $usedPayByCheckInRegistrationAndRenewal);
  331. } else {
  332. $eventType = AppEvents::REGISTRATION_FORM_UPDATED;
  333. }
  334. $newEntityEvent = new FormEvent($entity);
  335. }
  336. if ($review instanceof RenewalFormReview) {
  337. $entity = ReviewRenewalForm::parse($review, $review->getForm(), true);
  338. $reviewCustomFields = json_decode($review->getCustomFields(), true);
  339. $usedPayByCheckInRegistrationAndRenewal = array_key_exists('pay_by_check', $reviewCustomFields) ? $reviewCustomFields['pay_by_check'] : false;
  340. if ($review->getForm() !== null) $review->setForm(null);
  341. $entity->setStartDate(new \DateTime(PropertyHelper::DELAY_FOR_INVOICE));
  342. $entity->setEndDate($this->propertyHelper->getNextRenewalDate($entity->getProperty()));
  343. if ($review->getForm() == null) {
  344. $eventType = AppEvents::RENEWAL_FORM_CREATED;
  345. $invoiceEvent = new FormCreatedEvent($entity, InvoiceTypes::VPR_RENEWAL, null, $usedPayByCheckInRegistrationAndRenewal);
  346. } else {
  347. $eventType = AppEvents::RENEWAL_FORM_UPDATED;
  348. }
  349. //TODO handle custom fields.
  350. $newEntityEvent = new FormEvent($entity);
  351. }
  352. if ($review instanceof DeregistrationFormReview) {
  353. $entity = ReviewDeregistrationForm::parse($review, $review->getForm(), true);
  354. if ($review->getForm() !== null) $review->setForm(null);
  355. if ($review->getForm() == null) {
  356. $eventType = AppEvents::DEREGISTRATION_FORM_CREATED;
  357. } else {
  358. $eventType = AppEvents::DEREGISTRATION_FORM_UPDATED;
  359. }
  360. //TODO handle custom fields.
  361. $newEntityEvent = new FormEvent($entity);
  362. }
  363. if ($entity) {
  364. $this->em->persist($entity);
  365. $this->em->flush();
  366. if ($review instanceof RegistrationFormReview) {
  367. $this->customFieldHelper->saveFormCustomFields($entity, FormTypes::REGISTRATION, $builder);
  368. }
  369. if ($newEntityEvent) {
  370. $this->eventDispatcher->dispatch($newEntityEvent, $eventType);
  371. }
  372. }
  373. if(isset($invoiceEvent)) {
  374. $this->eventDispatcher->dispatch($invoiceEvent, AppEvents::FORM_CREATED);
  375. //TODO PAYMENT
  376. }
  377. }
  378. /**
  379. * @param ReviewEvent $event
  380. */
  381. public function onReviewDenied(ReviewEvent $event): void
  382. {
  383. $review = $event->getReview();
  384. $reason = $event->getReason();
  385. if (!empty($event->getReview()->getForm())) {
  386. $form = $event->getReview()->getForm();
  387. $form->setStatus(ReviewStatuses::DECLINED);
  388. $this->em->persist($form);
  389. $this->em->flush();
  390. }
  391. // if ($review->notifyUser()) {
  392. $event = new NotifyReviewEvent($review, [], $reason);
  393. $this->eventDispatcher->dispatch($event, AppEvents::REVIEW_NOTIFY_DENIED);
  394. // }
  395. }
  396. /**
  397. * @return User|null
  398. */
  399. protected function getCurrentUser(): ?User
  400. {
  401. $currentUser = null;
  402. if ($this->tokenStorage->getToken()
  403. && $this->tokenStorage->getToken()->getUser() instanceof User
  404. ) {
  405. $currentUser = $this->tokenStorage->getToken()->getUser();
  406. }
  407. return $currentUser;
  408. }
  409. }