|
- <?php
-
- namespace Lc\SovBundle\Builder\Setting;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\SovBundle\Definition\SiteSettingDefinition;
- use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
- use Lc\SovBundle\Repository\Site\SiteStore;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-
- class SettingBuilder
- {
- protected EntityManagerInterface $entityManager;
- protected SettingSolver $settingSolver;
- protected SiteStore $siteStore;
- protected SiteSettingDefinition $siteSettingDefinition;
- protected SiteSettingFactory $siteSettingFactory;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- SettingSolver $settingSolver,
- SiteStore $siteStore,
- SiteSettingDefinition $siteSettingDefinition,
- SiteSettingFactory $siteSettingFactory
- ) {
- $this->entityManager = $entityManager;
- $this->settingSolver = $settingSolver;
- $this->siteStore = $siteStore;
- $this->siteSettingDefinition = $siteSettingDefinition;
- $this->siteSettingFactory = $siteSettingFactory;
- }
-
- public function initSiteSettings()
- {
- $site = $this->siteStore->getOneByDevAlias('default');
-
- if ($site) {
- $settings = $this->siteSettingDefinition->getSettings();
-
- 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 = $this->siteSettingFactory->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();
- } else {
- throw new NotFoundHttpException('Le site par défaut n\'existe pas.');
- }
- }
- }
|