<?php
namespace App\EventSubscriber;
use App\Entity\PropertyReview;
use App\Entity\PropertySupportTicket;
use App\Entity\SupportTicket;
use App\Event\AppEvents;
use App\Event\PropertyEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class PropertyListener
*/
class PropertySubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* ReviewListener constructor.
*
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
AppEvents::PROPERTY_DELETED => ['onPropertyDeleted'],
];
}
/**
* @param PropertyEvent $event
*/
public function onPropertyDeleted(PropertyEvent $event)
{
$property = $event->getProperty();
$id = $property->getId();
//Delete Reviews for the property.
$this->entityManager->getRepository(PropertyReview::class)->deleteByProperty($id);
//Delete Property Dispute and Support.
$ticketIds = $this->entityManager->getRepository(PropertySupportTicket::class)->getTicketsByProperty($id);
if (count($ticketIds)) {
$this->entityManager->getRepository(SupportTicket::class)->deleteInIds($ticketIds);
}
}
}