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 = $entity->getSetting($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; $fieldValue = isset($setting['default']) ? $setting['default'] : null; if ($setting['field'] == 'text') { $text = $fieldValue; } elseif ($setting['field'] == 'date') { $date = $fieldValue; } elseif ($setting['field'] == 'file') { $file = $fieldValue; } $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) { $methodSetValue = 'set' . ucfirst($setting['field']); $entitySetting->$methodSetValue($setting['default']); $this->entityManager->update($entitySetting); } } } } } $this->entityManager->flush(); } } }