1
|
|
|
<?php namespace Modules\Setting\Repositories\Eloquent; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Config; |
4
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
5
|
|
|
use Modules\Setting\Events\SettingWasCreated; |
6
|
|
|
use Modules\Setting\Events\SettingWasUpdated; |
7
|
|
|
use Modules\Setting\Repositories\SettingRepository; |
8
|
|
|
|
9
|
|
|
class EloquentSettingRepository extends EloquentBaseRepository implements SettingRepository |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Update a resource |
13
|
|
|
* @param $id |
14
|
|
|
* @param $data |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
|
|
public function update($id, $data) |
18
|
|
|
{ |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return all settings, with the setting name as key |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public function all() |
26
|
|
|
{ |
27
|
|
|
$rawSettings = parent::all(); |
28
|
|
|
|
29
|
|
|
$settings = []; |
30
|
|
|
foreach ($rawSettings as $setting) { |
31
|
|
|
$settings[$setting->name] = $setting; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $settings; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create or update the settings |
39
|
|
|
* @param $settings |
40
|
|
|
* @return mixed|void |
41
|
|
|
*/ |
42
|
|
|
public function createOrUpdate($settings) |
43
|
|
|
{ |
44
|
|
|
$this->removeTokenKey($settings); |
45
|
|
|
|
46
|
|
|
foreach ($settings as $settingName => $settingValues) { |
47
|
|
|
if ($setting = $this->findByName($settingName)) { |
48
|
|
|
$this->updateSetting($setting, $settingValues); |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$this->createForName($settingName, $settingValues); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Remove the token from the input array |
57
|
|
|
* @param $settings |
58
|
|
|
*/ |
59
|
|
|
private function removeTokenKey(&$settings) |
60
|
|
|
{ |
61
|
|
|
unset($settings['_token']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Find a setting by its name |
66
|
|
|
* @param $settingName |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function findByName($settingName) |
70
|
|
|
{ |
71
|
|
|
return $this->model->where('name', $settingName)->first(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create a setting with the given name |
76
|
|
|
* @param string $settingName |
77
|
|
|
* @param $settingValues |
78
|
|
|
*/ |
79
|
|
|
private function createForName($settingName, $settingValues) |
80
|
|
|
{ |
81
|
|
|
$setting = new $this->model(); |
82
|
|
|
$setting->name = $settingName; |
83
|
|
|
|
84
|
|
|
if ($this->isTranslatableSetting($settingName)) { |
85
|
|
|
$setting->isTranslatable = true; |
86
|
|
|
$this->setTranslatedAttributes($settingValues, $setting); |
87
|
|
|
event(new SettingWasCreated($settingName, true, $settingValues)); |
88
|
|
|
} else { |
89
|
|
|
$setting->isTranslatable = false; |
90
|
|
|
$setting->plainValue = $this->getSettingPlainValue($settingValues); |
91
|
|
|
event(new SettingWasCreated($settingName, false, $settingValues)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $setting->save(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Update the given setting |
99
|
|
|
* @param object setting |
100
|
|
|
* @param $settingValues |
101
|
|
|
*/ |
102
|
|
|
private function updateSetting($setting, $settingValues) |
103
|
|
|
{ |
104
|
|
|
$name = $setting->name; |
105
|
|
|
|
106
|
|
|
if ($this->isTranslatableSetting($name)) { |
107
|
|
|
$this->setTranslatedAttributes($settingValues, $setting); |
108
|
|
|
event(new SettingWasUpdated($name, true, $settingValues)); |
109
|
|
|
} else { |
110
|
|
|
$oldValues = $setting->plainValue; |
111
|
|
|
$setting->plainValue = $this->getSettingPlainValue($settingValues); |
112
|
|
|
event(new SettingWasUpdated($name, true, $settingValues, $oldValues)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $setting->save(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $settingValues |
120
|
|
|
* @param $setting |
121
|
|
|
*/ |
122
|
|
|
private function setTranslatedAttributes($settingValues, $setting) |
123
|
|
|
{ |
124
|
|
|
foreach ($settingValues as $lang => $value) { |
125
|
|
|
$setting->translateOrNew($lang)->value = $value; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return all modules that have settings |
131
|
|
|
* with its settings |
132
|
|
|
* @param array|string $modules |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function moduleSettings($modules) |
136
|
|
|
{ |
137
|
|
|
if (is_string($modules)) { |
138
|
|
|
return config('asgard.' . strtolower($modules) . ".settings"); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$modulesWithSettings = []; |
142
|
|
|
foreach ($modules as $module) { |
143
|
|
|
if ($moduleSettings = config('asgard.' . strtolower($module->getName()) . ".settings")) { |
144
|
|
|
$modulesWithSettings[$module->getName()] = $moduleSettings; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $modulesWithSettings; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the saved module settings |
153
|
|
|
* @param $module |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function savedModuleSettings($module) |
157
|
|
|
{ |
158
|
|
|
$moduleSettings = []; |
159
|
|
|
foreach ($this->findByModule($module) as $setting) { |
160
|
|
|
$moduleSettings[$setting->name] = $setting; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $moduleSettings; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Find settings by module name |
168
|
|
|
* @param string $module Module name |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
|
|
public function findByModule($module) |
172
|
|
|
{ |
173
|
|
|
return $this->model->where('name', 'LIKE', $module . '::%')->get(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Find the given setting name for the given module |
178
|
|
|
* @param string $settingName |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public function get($settingName) |
182
|
|
|
{ |
183
|
|
|
return $this->model->where('name', 'LIKE', "{$settingName}")->first(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Return the translatable module settings |
188
|
|
|
* @param $module |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function translatableModuleSettings($module) |
192
|
|
|
{ |
193
|
|
|
return array_filter($this->moduleSettings($module), function ($setting) { |
194
|
|
|
return isset($setting['translatable']) && $setting['translatable'] === true; |
195
|
|
|
}); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the non translatable module settings |
200
|
|
|
* @param $module |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function plainModuleSettings($module) |
204
|
|
|
{ |
205
|
|
|
return array_filter($this->moduleSettings($module), function ($setting) { |
206
|
|
|
return !isset($setting['translatable']) || $setting['translatable'] === false; |
207
|
|
|
}); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Return a setting name using dot notation: asgard.{module}.settings.{settingName} |
212
|
|
|
* @param string $settingName |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
private function getConfigSettingName($settingName) |
216
|
|
|
{ |
217
|
|
|
list($module, $setting) = explode('::', $settingName); |
218
|
|
|
|
219
|
|
|
return "asgard.{$module}.settings.{$setting}"; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Check if the given setting name is translatable |
224
|
|
|
* @param string $settingName |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
private function isTranslatableSetting($settingName) |
228
|
|
|
{ |
229
|
|
|
$configSettingName = $this->getConfigSettingName($settingName); |
230
|
|
|
|
231
|
|
|
$setting = config("$configSettingName"); |
232
|
|
|
|
233
|
|
|
return isset($setting['translatable']) && $setting['translatable'] === true; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Return the setting value(s). If values are ann array, json_encode them |
238
|
|
|
* @param string|array $settingValues |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
private function getSettingPlainValue($settingValues) |
242
|
|
|
{ |
243
|
|
|
if (is_array($settingValues)) { |
244
|
|
|
return json_encode($settingValues); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $settingValues; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.