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) { $entitySetting = $factory->create( [ $type => $entity, 'name' => $setting['name'], $setting['field'] => isset($setting['default']) ? $setting['default'] : null, ] ); $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(); } }