Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

77 lines
2.2KB

  1. <?php
  2. namespace common\logic\Setting;
  3. use common\logic\AbstractBuilder;
  4. use yii\base\ErrorException;
  5. class SettingBuilder extends AbstractBuilder
  6. {
  7. protected SettingDefinition $settingDefinition;
  8. protected SettingRepository $settingRepository;
  9. public function loadDependencies(): void
  10. {
  11. $this->settingDefinition = $this->loadService(SettingDefinition::class);
  12. $this->settingRepository = $this->loadService(SettingRepository::class);
  13. }
  14. public function instanciateSetting(
  15. string $name
  16. ): Setting
  17. {
  18. $setting = new Setting();
  19. $setting->name = $name;
  20. $setting->populateProducer($this->getProducerContext(false));
  21. $this->initDefaultValue($setting);
  22. return $setting;
  23. }
  24. public function createSetting(
  25. string $name
  26. ): Setting
  27. {
  28. $setting = $this->settingRepository->findOneSettingByName($name);
  29. if(!$setting) {
  30. $setting = $this->instanciateSetting($name);
  31. $this->create($setting);
  32. }
  33. return $setting;
  34. }
  35. public function initValue(Setting $setting, $value)
  36. {
  37. $settingDetail = $this->settingDefinition->getSettingDetailByName($setting->name);
  38. $type = $settingDetail->getType();
  39. if(in_array($type, ['string', 'text', 'date', 'integer', 'float', 'double', 'boolean'])) {
  40. $setting->$type = $value;
  41. }
  42. else {
  43. throw new ErrorException("Le type de donnée n'est pas reconnu.");
  44. }
  45. }
  46. public function initDefaultValue(Setting $setting)
  47. {
  48. $settingDetail = $this->settingDefinition->getSettingDetailByName($setting->name);
  49. if($settingDetail->getDefaultValue()) {
  50. $this->initValue($setting, $settingDetail->getDefaultValue());
  51. }
  52. }
  53. public function updateValueByName(string $name, $value): void
  54. {
  55. $setting = $this->createSetting($name);
  56. $this->initValue($setting, $value);
  57. $this->update($setting);
  58. }
  59. public function updateDefaultValueByName(string $name)
  60. {
  61. $setting = $this->createSetting($name);
  62. $this->initDefaultValue($setting);
  63. $this->update($setting);
  64. }
  65. }