<?php
namespace AWCatalogBundle\EventListener;
use AWCatalogBundle\Entity\CatalogSettings;
use AWCmsBundle\Entity\Site;
use AWCmsBundle\Event\SiteEvent;
use AWCmsBundle\Manager\ModuleManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SiteListener implements EventSubscriberInterface
{
private $em;
private $moduleManager;
public function __construct(EntityManagerInterface $em, ModuleManager $moduleManager)
{
$this->em = $em;
$this->moduleManager = $moduleManager;
}
public static function getSubscribedEvents()
{
return array(SiteEvent::SAVED => 'onSiteUpdate');
}
public function onSiteUpdate(SiteEvent $event){
$site = $event->getSite();
if($this->moduleManager->hasSiteModuleForBundle($site,'AWCatalogBundle')){
$catalogSettings = $this->em->getRepository(CatalogSettings::class)->findOneBy(['site' => $site]);
if($catalogSettings === null){
$catalogSettings = new CatalogSettings();
$catalogSettings->setSite($site);
$this->em->persist($catalogSettings);
$this->em->flush();
}
}
}
}