<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Event\LocationSlugUpdateEvent;
use Aqarmap\Bundle\ListingBundle\Service\Contracts\LocationManagerInterface;
use Aqarmap\Bundle\ListingBundle\Service\LocationManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\Event;
class LocationSubscriber implements EventSubscriberInterface
{
/**
* @var LocationManager
*/
private $locationManager;
public function __construct(LocationManagerInterface $locationManager)
{
$this->locationManager = $locationManager;
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onUpdate(Event $event): void
{
$location = $event->getLocation();
$this
->locationManager
->updateFullPath(
$location,
json_encode($this->locationManager->getFullPathWithId($location), true)
);
$this->locationManager->updateDisabledChildren($location);
}
public function updateChildrenSlugs(LocationSlugUpdateEvent $event)
{
$slugs = $event->getSlugs();
if (!isset($slugs['oldSlug'], $slugs['newSlug'])
|| $slugs['oldSlug'] == $slugs['newSlug']) {
return true;
}
return $this->locationManager->updateChildrenSlug($slugs['oldSlug'], $slugs['newSlug']);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'location.got.touched' => 'onUpdate',
'location.slug.touched' => 'updateChildrenSlugs',
];
}
}