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(); } } }