|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
- use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
- use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\CaracoleBundle\Repository\Section\SectionStore;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SettingEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $entityManager;
- protected MerchantSettingDefinitionInterface $merchantSettingDefinition;
- protected SectionSettingDefinition $sectionSettingDefinition;
- protected MerchantStore $merchantStore;
- protected SectionStore $sectionStore;
- protected MerchantSettingFactory $merchantSettingFactory;
- protected SectionSettingFactory $sectionSettingFactory;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- MerchantSettingDefinitionInterface $merchantSettingDefinition,
- SectionSettingDefinition $sectionSettingDefinition,
- MerchantStore $merchantStore,
- SectionStore $sectionStore,
- MerchantSettingFactory $merchantSettingFactory,
- SectionSettingFactory $sectionSettingFactory,
- SettingSolver $settingSolver
- ) {
- $this->entityManager = $entityManager;
- $this->merchantStore = $merchantStore;
- $this->sectionStore = $sectionStore;
- $this->merchantSettingDefinition = $merchantSettingDefinition;
- $this->sectionSettingDefinition = $sectionSettingDefinition;
- $this->merchantSettingFactory = $merchantSettingFactory;
- $this->sectionSettingFactory = $sectionSettingFactory;
- $this->settingSolver = $settingSolver;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $merchants = $this->merchantStore->get();
-
- $this->initSettingsGeneric(
- 'merchant',
- $this->merchantSettingDefinition->getSettings(),
- //TODO vérifier que ce soit bien les online que l'on souhaite
- $merchants,
- $this->merchantSettingFactory
- );
-
- foreach($merchants as $merchant) {
- $this->initSettingsGeneric(
- 'section',
- $this->sectionSettingDefinition->getSettings(),
- $merchant->getSections(),
- $this->sectionSettingFactory
- );
- }
- }
-
- public function initSettingsGeneric($type, $settings, $entities, $factory)
- {
- if($entities) {
- foreach ($entities as $entity) {
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $this->settingSolver->getSetting($entity, $settingName);
-
- if (!$entitySetting) {
- // gestion du cas des SectionSetting spécifiques à une section
- $createEntitySetting = true;
- if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
- $createEntitySetting = false;
- }
-
- if ($createEntitySetting) {
- $text = null;
- $date = null;
- $file = null;
-
- $valueDefault = isset($setting['default']) ? $this->settingSolver->getDefaultValue($entity, $setting['default']) : null;
-
- if ($setting['field'] == 'text') {
- $text = $valueDefault;
- } elseif ($setting['field'] == 'date') {
- $date = $valueDefault;
- } elseif ($setting['field'] == 'file') {
- $file = $valueDefault;
- }
-
- $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
-
- $this->entityManager->persist($entitySetting);
- }
- } else {
- if ($this->settingSolver->getValue($entitySetting) === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
-
- $valueDefault = $this->settingSolver->getDefaultValue($entity, $setting['default']);
-
- if($valueDefault) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($valueDefault);
- $this->entityManager->update($entitySetting);
- }
- }
- }
- }
- }
- }
-
- $this->entityManager->flush();
- }
- }
-
- }
|