em = $em; $this->merchantRepository = $merchantRepository; $this->sectionRepository = $sectionRepository; $this->merchantSettingDefinition = $merchantSettingDefinition; $this->sectionSettingDefinition = $sectionSettingDefinition; $this->merchantSettingFactory = $merchantSettingFactory; $this->sectionSettingFactory = $sectionSettingFactory; } public static function getSubscribedEvents() { return [ KernelEvents::CONTROLLER => ['initSettings'] ]; } public function initSettings() { $this->initSettingsGeneric( 'merchant', $this->merchantSettingDefinition->getSettings(), $this->merchantRepository->findAll(), $this->merchantSettingFactory ); $this->initSettingsGeneric( 'section', $this->sectionSettingDefinition->getSettings(), $this->sectionRepository->findAll(), $this->sectionSettingFactory ); } public function initSettingsGeneric($type, $settings, $entities, $factory) { 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) { if ($type == 'section') { $factory->setSection($entity); } elseif ($type == 'merchant') { $factory->setMerchant($entity); } $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($setting['name'], $text, $date, $file); $this->em->persist($entitySetting); } } else { if ($entitySetting->getValue() === null && isset($setting['default']) && $setting['default'] !== null) { $methodSetValue = 'set' . ucfirst($setting['field']); $entitySetting->$methodSetValue($setting['default']); $this->em->update($entitySetting); } } } } } $this->em->flush(); } }