|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?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\SiteSettingRepository;
- use Lc\SovBundle\Repository\Site\SiteRepository;
- use Lc\SovBundle\Repository\Site\SiteStore;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SiteSettingEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $entityManager;
- protected SiteSettingDefinitionInterface $siteSettingDefinition;
- protected SiteSettingFactory $siteSettingFactory;
- protected SiteSettingRepository $siteSettingRepository;
- protected SiteStore $siteStore;
- protected SiteFactory $siteFactory;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- SiteSettingRepository $siteSettingRepository,
- SiteStore $siteStore,
- SiteSettingDefinitionInterface $siteSettingDefinition,
- SiteSettingFactory $siteSettingFactory,
- SiteFactory $siteFactory,
- SettingSolver $settingSolver
- ) {
- $this->entityManager = $entityManager;
- $this->siteSettingDefinition = $siteSettingDefinition;
- $this->siteSettingFactory = $siteSettingFactory;
- $this->siteSettingRepository = $siteSettingRepository;
- $this->siteStore = $siteStore;
- $this->siteFactory = $siteFactory;
- $this->settingSolver = $settingSolver;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $site = $this->getSiteDefault();
- if (!$site) {
- $site = $this->siteFactory->create('default');
- $this->entityManager->persist($site);
- $this->entityManager->flush($site);
- }
-
- $settings = $this->siteSettingDefinition->getSettings();
- $factory = $this->siteSettingFactory;
-
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $this->settingSolver->getSetting($site, $settingName);
-
- if (!$entitySetting) {
-
- $text = null;
- $date = null;
- $file = null;
-
- $fieldValue = isset($setting['default']) ? $setting['default'] : null;
-
- if($setting['field'] == 'text') {
- $text = $fieldValue;
- }
- if($setting['field'] == 'date') {
- $date = $fieldValue;
- }
- if($setting['field'] == 'file') {
- $file = $fieldValue;
- }
-
- $entitySetting = $factory->create($site, $setting['name'], $text, $date, $file);
- $this->entityManager->create($entitySetting);
-
- } else {
- $methodGetValue = 'get' . ucfirst($setting['field']);
- if ($entitySetting->$methodGetValue() === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
-
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($setting['default']);
- $this->entityManager->update($entitySetting);
- }
- }
- }
- }
-
- $this->entityManager->flush();
- }
-
- public function getSiteDefault()
- {
- return $this->siteStore->getOneByDevAlias('default');
- }
- }
|