<?php
namespace Aqarmap\Bundle\NotifierBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Entity\Location;
use Aqarmap\Bundle\NotifierBundle\Event\NotifierEvent;
use Aqarmap\Bundle\NotifierBundle\Service\NotifierManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NotifierSubscribeListener implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var NotifierManager
*/
protected $notifierManager;
public function __construct(EntityManagerInterface $em, NotifierManager $notifierManager)
{
$this->em = $em;
$this->notifierManager = $notifierManager;
}
public function onCreateNotifier(NotifierEvent $event): void
{
/** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
$locationRepo = $this->em->getRepository(Location::class);
$locations = $locationRepo->getPath($event->getNotifier()->getLocation());
arsort($locations);
/** @var Location $treeLocation */
foreach ($locations as $treeLocation) {
if ($treeLocation->getParent() && $treeLocation != $event->getNotifier()->getLocation()) {
$this->notifierManager->addNew(
$event->getNotifier()->getUser(),
$treeLocation,
$event->getNotifier()->getSection(),
$event->getNotifier()->getPropertyType(),
$event->getNotifier()->getMaxPrice(),
$event->getNotifier()->getMinPrice(),
false,
true
)
->markAsPassive()
->commit()
;
}
}
}
public static function getSubscribedEvents()
{
return [
'aqarmap.notifier.create.parent.locations' => 'onCreateNotifier',
];
}
}