|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?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.');
- }
- }
- }
|