選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SettingBuilder.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace domain\Setting;
  3. use domain\Producer\Producer\Producer;
  4. use domain\_\AbstractBuilder;
  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. }