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

  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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Contracts\EventDispatcher\Event;
  7. class LocationSubscriber implements EventSubscriberInterface
  8. {
  9. public function __construct(private readonly LocationManagerInterface $locationManager)
  10. {
  11. }
  12. /**
  13. * @throws \Doctrine\ORM\ORMException
  14. * @throws \Doctrine\ORM\OptimisticLockException
  15. */
  16. public function onUpdate(Event $event): void
  17. {
  18. $location = $event->getLocation();
  19. $this
  20. ->locationManager
  21. ->updateFullPath(
  22. $location,
  23. json_encode($this->locationManager->getFullPathWithId($location), true)
  24. );
  25. $this->locationManager->updateDisabledChildren($location);
  26. }
  27. public function updateChildrenSlugs(LocationSlugUpdateEvent $event)
  28. {
  29. $slugs = $event->getSlugs();
  30. if (!isset($slugs['oldSlug'], $slugs['newSlug'])
  31. || $slugs['oldSlug'] == $slugs['newSlug']) {
  32. return true;
  33. }
  34. return $this->locationManager->updateChildrenSlug($slugs['oldSlug'], $slugs['newSlug']);
  35. }
  36. public static function getSubscribedEvents(): array
  37. {
  38. return [
  39. 'location.got.touched' => 'onUpdate',
  40. 'location.slug.touched' => 'updateChildrenSlugs',
  41. ];
  42. }
  43. }