SettingsService.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Services\Settings; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Settings\Settings; |
| 6 | use AmeliaBooking\Domain\Factory\Settings\SettingsFactory; |
| 7 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 8 | |
| 9 | /** |
| 10 | * Class SettingsService |
| 11 | * |
| 12 | * @package AmeliaBooking\Domain\Services\Settings |
| 13 | */ |
| 14 | class SettingsService |
| 15 | { |
| 16 | const NUMBER_OF_DAYS_AVAILABLE_FOR_BOOKING = 365; |
| 17 | |
| 18 | /** @var SettingsStorageInterface */ |
| 19 | private $settingsStorage; |
| 20 | |
| 21 | /** |
| 22 | * SettingsService constructor. |
| 23 | * |
| 24 | * @param SettingsStorageInterface $settingsStorage |
| 25 | */ |
| 26 | public function __construct(SettingsStorageInterface $settingsStorage) |
| 27 | { |
| 28 | $this->settingsStorage = $settingsStorage; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param $settingCategoryKey |
| 33 | * @param $settingKey |
| 34 | * @param null $defaultValue |
| 35 | * |
| 36 | * @return mixed|null |
| 37 | */ |
| 38 | public function getSetting($settingCategoryKey, $settingKey, $defaultValue = null) |
| 39 | { |
| 40 | if (null !== $this->settingsStorage->getSetting($settingCategoryKey, $settingKey)) { |
| 41 | return $this->settingsStorage->getSetting($settingCategoryKey, $settingKey); |
| 42 | } |
| 43 | |
| 44 | return $defaultValue; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param $settingCategoryKey |
| 49 | * |
| 50 | * @return mixed|array |
| 51 | */ |
| 52 | public function getCategorySettings($settingCategoryKey) |
| 53 | { |
| 54 | return $this->settingsStorage->getCategorySettings($settingCategoryKey); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return array of all settings where keys are settings names and values are settings values |
| 59 | * |
| 60 | * @return mixed |
| 61 | */ |
| 62 | public function getAllSettings() |
| 63 | { |
| 64 | return $this->settingsStorage->getAllSettings(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return array of arrays where keys are settings categories names and values are categories settings |
| 69 | * |
| 70 | * @return mixed |
| 71 | */ |
| 72 | public function getAllSettingsCategorized() |
| 73 | { |
| 74 | return $this->settingsStorage->getAllSettingsCategorized(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return mixed |
| 79 | */ |
| 80 | public function getFrontendSettings() |
| 81 | { |
| 82 | return $this->settingsStorage->getFrontendSettings(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param $settingCategoryKey |
| 87 | * @param $settingKey |
| 88 | * @param $settingValue |
| 89 | * |
| 90 | * @return mixed |
| 91 | */ |
| 92 | public function setSetting($settingCategoryKey, $settingKey, $settingValue) |
| 93 | { |
| 94 | return $this->settingsStorage->setSetting($settingCategoryKey, $settingKey, $settingValue); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param $settingCategoryKey |
| 99 | * @param $settingValues |
| 100 | * |
| 101 | * @return mixed |
| 102 | */ |
| 103 | public function setCategorySettings($settingCategoryKey, $settingValues) |
| 104 | { |
| 105 | return $this->settingsStorage->setCategorySettings($settingCategoryKey, $settingValues); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param $settings |
| 110 | * |
| 111 | * @return mixed |
| 112 | */ |
| 113 | public function setAllSettings($settings) |
| 114 | { |
| 115 | return $this->settingsStorage->setAllSettings($settings); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param Json $entitySettingsJson |
| 120 | * |
| 121 | * @return Settings |
| 122 | */ |
| 123 | public function getEntitySettings($entitySettingsJson) |
| 124 | { |
| 125 | return SettingsFactory::create($entitySettingsJson, $this->getAllSettingsCategorized()); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param Json $entitySettingsJson |
| 130 | * |
| 131 | * @return Settings |
| 132 | */ |
| 133 | public function getSavedSettings($entitySettingsJson) |
| 134 | { |
| 135 | $data = $entitySettingsJson ? json_decode($entitySettingsJson->getValue(), true) : []; |
| 136 | |
| 137 | $isOldEntitySettings = !isset($data['activation']['version']); |
| 138 | |
| 139 | if ($isOldEntitySettings && isset($data['general']['minimumTimeRequirementPriorToCanceling'])) { |
| 140 | $data['general']['minimumTimeRequirementPriorToRescheduling'] = |
| 141 | $data['general']['minimumTimeRequirementPriorToCanceling']; |
| 142 | } |
| 143 | |
| 144 | return $data; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param array $entities |
| 149 | * |
| 150 | * @return void |
| 151 | */ |
| 152 | public function setStashEntities($entities) |
| 153 | { |
| 154 | update_option('amelia_stash', json_encode($entities)); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return array |
| 159 | */ |
| 160 | public function getStashEntities() |
| 161 | { |
| 162 | $entitiesStash = get_option('amelia_stash'); |
| 163 | |
| 164 | return $entitiesStash ? json_decode($entitiesStash, true) : []; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param array $settings |
| 169 | * |
| 170 | * @return void |
| 171 | */ |
| 172 | public function fixCustomization(&$settings) |
| 173 | { |
| 174 | if (isset($settings['forms']) && $settings['forms']) { |
| 175 | foreach ($settings['forms'] as $formName => &$form) { |
| 176 | if (isset($form['confirmBookingForm'])) { |
| 177 | foreach ($form['confirmBookingForm'] as $entityName => &$entity) { |
| 178 | if (isset($entity['itemsStatic']['paymentMethodFormField']['switchPaymentMethodViewOptions'])) { |
| 179 | array_splice( |
| 180 | $settings['forms'][$formName]['confirmBookingForm'][$entityName]['itemsStatic']['paymentMethodFormField']['switchPaymentMethodViewOptions'], |
| 181 | 2 |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 |