src/Aqarmap/Bundle/NotifierBundle/EventListener/NotifierSubscribeListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\NotifierBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  4. use Aqarmap\Bundle\NotifierBundle\Event\NotifierEvent;
  5. use Aqarmap\Bundle\NotifierBundle\Service\NotifierManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class NotifierSubscribeListener implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityManagerInterface
  12.      */
  13.     protected $em;
  14.     /**
  15.      * @var NotifierManager
  16.      */
  17.     protected $notifierManager;
  18.     public function __construct(EntityManagerInterface $emNotifierManager $notifierManager)
  19.     {
  20.         $this->em $em;
  21.         $this->notifierManager $notifierManager;
  22.     }
  23.     public function onCreateNotifier(NotifierEvent $event): void
  24.     {
  25.         /** @var $locationRepo \Aqarmap\Bundle\ListingBundle\Entity\LocationRepository */
  26.         $locationRepo $this->em->getRepository(Location::class);
  27.         $locations $locationRepo->getPath($event->getNotifier()->getLocation());
  28.         arsort($locations);
  29.         /** @var Location $treeLocation */
  30.         foreach ($locations as $treeLocation) {
  31.             if ($treeLocation->getParent() && $treeLocation != $event->getNotifier()->getLocation()) {
  32.                 $this->notifierManager->addNew(
  33.                     $event->getNotifier()->getUser(),
  34.                     $treeLocation,
  35.                     $event->getNotifier()->getSection(),
  36.                     $event->getNotifier()->getPropertyType(),
  37.                     $event->getNotifier()->getMaxPrice(),
  38.                     $event->getNotifier()->getMinPrice(),
  39.                     false,
  40.                     true
  41.                 )
  42.                     ->markAsPassive()
  43.                     ->commit()
  44.                 ;
  45.             }
  46.         }
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             'aqarmap.notifier.create.parent.locations' => 'onCreateNotifier',
  52.         ];
  53.     }
  54. }