Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

84 lines
2.3KB

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