src/EventSubscriber/PropertySubscriber.php line 46

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\PropertyReview;
  4. use App\Entity\PropertySupportTicket;
  5. use App\Entity\SupportTicket;
  6. use App\Event\AppEvents;
  7. use App\Event\PropertyEvent;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11. * Class PropertyListener
  12. */
  13. class PropertySubscriber implements EventSubscriberInterface
  14. {
  15. /**
  16. * @var EntityManagerInterface
  17. */
  18. private $entityManager;
  19. /**
  20. * ReviewListener constructor.
  21. *
  22. * @param EntityManagerInterface $entityManager
  23. */
  24. public function __construct(EntityManagerInterface $entityManager)
  25. {
  26. $this->entityManager = $entityManager;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function getSubscribedEvents(): array
  32. {
  33. return [
  34. AppEvents::PROPERTY_DELETED => ['onPropertyDeleted'],
  35. ];
  36. }
  37. /**
  38. * @param PropertyEvent $event
  39. */
  40. public function onPropertyDeleted(PropertyEvent $event): void
  41. {
  42. $property = $event->getProperty();
  43. $id = $property->getId();
  44. //Delete Reviews for the property.
  45. $this->entityManager->getRepository(PropertyReview::class)->deleteByProperty($id);
  46. //Delete Property Dispute and Support.
  47. $ticketIds = $this->entityManager->getRepository(PropertySupportTicket::class)->getTicketsByProperty($id);
  48. if (count($ticketIds)) {
  49. $this->entityManager->getRepository(SupportTicket::class)->deleteInIds($ticketIds);
  50. }
  51. }
  52. }