src/Aqarmap/Bundle/ListingBundle/EventListener/LocationSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Event\LocationSlugUpdateEvent;
  4. use Aqarmap\Bundle\ListingBundle\Service\Contracts\LocationManagerInterface;
  5. use Aqarmap\Bundle\ListingBundle\Service\LocationManager;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Contracts\EventDispatcher\Event;
  8. class LocationSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var LocationManager
  12.      */
  13.     private $locationManager;
  14.     public function __construct(LocationManagerInterface $locationManager)
  15.     {
  16.         $this->locationManager $locationManager;
  17.     }
  18.     /**
  19.      * @throws \Doctrine\ORM\ORMException
  20.      * @throws \Doctrine\ORM\OptimisticLockException
  21.      */
  22.     public function onUpdate(Event $event): void
  23.     {
  24.         $location $event->getLocation();
  25.         $this
  26.             ->locationManager
  27.             ->updateFullPath(
  28.                 $location,
  29.                 json_encode($this->locationManager->getFullPathWithId($location), true)
  30.             );
  31.         $this->locationManager->updateDisabledChildren($location);
  32.     }
  33.     public function updateChildrenSlugs(LocationSlugUpdateEvent $event)
  34.     {
  35.         $slugs $event->getSlugs();
  36.         if (!isset($slugs['oldSlug'], $slugs['newSlug'])
  37.             || $slugs['oldSlug'] == $slugs['newSlug']) {
  38.             return true;
  39.         }
  40.         return $this->locationManager->updateChildrenSlug($slugs['oldSlug'], $slugs['newSlug']);
  41.     }
  42.     /**
  43.      * @return array
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             'location.got.touched' => 'onUpdate',
  49.             'location.slug.touched' => 'updateChildrenSlugs',
  50.         ];
  51.     }
  52. }