|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
-
- namespace Lc\SovBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
- use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
- use Lc\SovBundle\Factory\Site\SiteFactory;
- use Lc\SovBundle\Repository\Setting\SiteSettingRepositoryInterface;
- use Lc\SovBundle\Repository\Site\SiteRepositoryInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SiteSettingEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $siteSettingDefinition;
- protected $siteSettingFactory;
- protected $siteSettingRepository;
- protected $siteRepository;
- protected $siteFactory;
-
- public function __construct(
- EntityManagerInterface $em,
- SiteSettingRepositoryInterface $siteSettingRepository,
- SiteRepositoryInterface $siteRepository,
- SiteSettingDefinitionInterface $siteSettingDefinition,
- SiteSettingFactory $siteSettingFactory,
- SiteFactory $siteFactory
- ) {
- $this->em = $em;
- $this->siteSettingDefinition = $siteSettingDefinition;
- $this->siteSettingFactory = $siteSettingFactory;
- $this->siteSettingRepository = $siteSettingRepository;
- $this->siteRepository = $siteRepository;
- $this->siteFactory = $siteFactory;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $site = $this->getSiteDefault();
- if (!$site) {
- $site = $this->siteFactory->create(
- [
- 'devAlias' => 'default'
- ]
- );
- $this->em->persist($site);
- $this->em->flush($site);
- }
-
-
- $settings = $this->siteSettingDefinition->getSettings();
- $factory = $this->siteSettingFactory;
-
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $site->getSetting($settingName);
-
- if (!$entitySetting) {
- $entitySetting = $factory->create(
- [
- 'site' => $site,
- 'name' => $setting['name'],
- $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
- ]
- );
-
- $this->em->persist($entitySetting);
- } else {
- if ($entitySetting->getValue() === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($setting['default']);
- $this->em->update($entitySetting);
- }
- }
- }
- }
-
-
- $this->em->flush();
- }
-
- public function getSiteDefault()
- {
- return $this->siteRepository->findOneByDevAlias('default');
- }
-
- }
|