1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\domain\base; |
3
|
|
|
|
4
|
|
|
use keeko\core\event\ApplicationUriEvent; |
5
|
|
|
use keeko\core\model\ApplicationUriQuery; |
6
|
|
|
use keeko\core\model\ApplicationUri; |
7
|
|
|
use keeko\framework\domain\payload\Created; |
8
|
|
|
use keeko\framework\domain\payload\Deleted; |
9
|
|
|
use keeko\framework\domain\payload\Found; |
10
|
|
|
use keeko\framework\domain\payload\NotDeleted; |
11
|
|
|
use keeko\framework\domain\payload\NotFound; |
12
|
|
|
use keeko\framework\domain\payload\NotUpdated; |
13
|
|
|
use keeko\framework\domain\payload\NotValid; |
14
|
|
|
use keeko\framework\domain\payload\PayloadInterface; |
15
|
|
|
use keeko\framework\domain\payload\Updated; |
16
|
|
|
use keeko\framework\service\ServiceContainer; |
17
|
|
|
use keeko\framework\utils\NameUtils; |
18
|
|
|
use keeko\framework\utils\Parameters; |
19
|
|
|
use phootwork\collection\Map; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
*/ |
23
|
|
|
trait ApplicationUriDomainTrait { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
*/ |
27
|
|
|
protected $pool; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a new ApplicationUri with the provided data |
31
|
|
|
* |
32
|
|
|
* @param mixed $data |
33
|
|
|
* @return PayloadInterface |
34
|
|
|
*/ |
35
|
|
|
public function create($data) { |
36
|
|
|
// hydrate |
37
|
|
|
$serializer = ApplicationUri::getSerializer(); |
38
|
|
|
$model = $serializer->hydrate(new ApplicationUri(), $data); |
39
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
40
|
|
|
|
41
|
|
|
// dispatch pre save hooks |
42
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_CREATE, $model, $data); |
43
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_SAVE, $model, $data); |
44
|
|
|
|
45
|
|
|
// validate |
46
|
|
|
$validator = $this->getValidator(); |
|
|
|
|
47
|
|
|
if ($validator !== null && !$validator->validate($model)) { |
48
|
|
|
return new NotValid([ |
49
|
|
|
'errors' => $validator->getValidationFailures() |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// save and dispatch post save hooks |
54
|
|
|
$model->save(); |
55
|
|
|
$this->dispatch(ApplicationUriEvent::POST_CREATE, $model, $data); |
56
|
|
|
$this->dispatch(ApplicationUriEvent::POST_SAVE, $model, $data); |
57
|
|
|
|
58
|
|
|
return new Created(['model' => $model]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Deletes a ApplicationUri with the given id |
63
|
|
|
* |
64
|
|
|
* @param mixed $id |
65
|
|
|
* @return PayloadInterface |
66
|
|
|
*/ |
67
|
|
|
public function delete($id) { |
68
|
|
|
// find |
69
|
|
|
$model = $this->get($id); |
70
|
|
|
|
71
|
|
|
if ($model === null) { |
72
|
|
|
return new NotFound(['message' => 'ApplicationUri not found.']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// delete |
76
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_DELETE, $model); |
77
|
|
|
$model->delete(); |
78
|
|
|
|
79
|
|
|
if ($model->isDeleted()) { |
80
|
|
|
$this->dispatch(ApplicationUriEvent::POST_DELETE, $model); |
81
|
|
|
return new Deleted(['model' => $model]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new NotDeleted(['message' => 'Could not delete ApplicationUri']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns a paginated result |
89
|
|
|
* |
90
|
|
|
* @param Parameters $params |
91
|
|
|
* @return PayloadInterface |
92
|
|
|
*/ |
93
|
|
|
public function paginate(Parameters $params) { |
94
|
|
|
$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences(); |
95
|
|
|
$defaultSize = $sysPrefs->getPaginationSize(); |
96
|
|
|
$page = $params->getPage('number'); |
97
|
|
|
$size = $params->getPage('size', $defaultSize); |
98
|
|
|
|
99
|
|
|
$query = ApplicationUriQuery::create(); |
100
|
|
|
|
101
|
|
|
// sorting |
102
|
|
|
$sort = $params->getSort(ApplicationUri::getSerializer()->getSortFields()); |
103
|
|
|
foreach ($sort as $field => $order) { |
104
|
|
|
$method = 'orderBy' . NameUtils::toStudlyCase($field); |
105
|
|
|
$query->$method($order); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// filtering |
109
|
|
|
$filter = $params->getFilter(); |
110
|
|
|
if (!empty($filter)) { |
111
|
|
|
$this->applyFilter($query, $filter); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// paginate |
115
|
|
|
$model = $query->paginate($page, $size); |
116
|
|
|
|
117
|
|
|
// run response |
118
|
|
|
return new Found(['model' => $model]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns one ApplicationUri with the given id |
123
|
|
|
* |
124
|
|
|
* @param mixed $id |
125
|
|
|
* @return PayloadInterface |
126
|
|
|
*/ |
127
|
|
|
public function read($id) { |
128
|
|
|
// read |
129
|
|
|
$model = $this->get($id); |
130
|
|
|
|
131
|
|
|
// check existence |
132
|
|
|
if ($model === null) { |
133
|
|
|
return new NotFound(['message' => 'ApplicationUri not found.']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new Found(['model' => $model]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets the Application id |
141
|
|
|
* |
142
|
|
|
* @param mixed $id |
143
|
|
|
* @param mixed $relatedId |
144
|
|
|
* @return PayloadInterface |
145
|
|
|
*/ |
146
|
|
|
public function setApplicationId($id, $relatedId) { |
147
|
|
|
// find |
148
|
|
|
$model = $this->get($id); |
149
|
|
|
|
150
|
|
|
if ($model === null) { |
151
|
|
|
return new NotFound(['message' => 'ApplicationUri not found.']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// update |
155
|
|
|
if ($this->doSetApplicationId($model, $relatedId)) { |
156
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_APPLICATION_UPDATE, $model); |
157
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_SAVE, $model); |
158
|
|
|
$model->save(); |
159
|
|
|
$this->dispatch(ApplicationUriEvent::POST_APPLICATION_UPDATE, $model); |
160
|
|
|
$this->dispatch(ApplicationUriEvent::POST_SAVE, $model); |
161
|
|
|
|
162
|
|
|
return Updated(['model' => $model]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return NotUpdated(['model' => $model]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets the Localization id |
170
|
|
|
* |
171
|
|
|
* @param mixed $id |
172
|
|
|
* @param mixed $relatedId |
173
|
|
|
* @return PayloadInterface |
174
|
|
|
*/ |
175
|
|
|
public function setLocalizationId($id, $relatedId) { |
176
|
|
|
// find |
177
|
|
|
$model = $this->get($id); |
178
|
|
|
|
179
|
|
|
if ($model === null) { |
180
|
|
|
return new NotFound(['message' => 'ApplicationUri not found.']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// update |
184
|
|
|
if ($this->doSetLocalizationId($model, $relatedId)) { |
185
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_LOCALIZATION_UPDATE, $model); |
186
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_SAVE, $model); |
187
|
|
|
$model->save(); |
188
|
|
|
$this->dispatch(ApplicationUriEvent::POST_LOCALIZATION_UPDATE, $model); |
189
|
|
|
$this->dispatch(ApplicationUriEvent::POST_SAVE, $model); |
190
|
|
|
|
191
|
|
|
return Updated(['model' => $model]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return NotUpdated(['model' => $model]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Updates a ApplicationUri with the given idand the provided data |
199
|
|
|
* |
200
|
|
|
* @param mixed $id |
201
|
|
|
* @param mixed $data |
202
|
|
|
* @return PayloadInterface |
203
|
|
|
*/ |
204
|
|
|
public function update($id, $data) { |
205
|
|
|
// find |
206
|
|
|
$model = $this->get($id); |
207
|
|
|
|
208
|
|
|
if ($model === null) { |
209
|
|
|
return new NotFound(['message' => 'ApplicationUri not found.']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// hydrate |
213
|
|
|
$serializer = ApplicationUri::getSerializer(); |
214
|
|
|
$model = $serializer->hydrate($model, $data); |
215
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
216
|
|
|
|
217
|
|
|
// dispatch pre save hooks |
218
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_UPDATE, $model, $data); |
219
|
|
|
$this->dispatch(ApplicationUriEvent::PRE_SAVE, $model, $data); |
220
|
|
|
|
221
|
|
|
// validate |
222
|
|
|
$validator = $this->getValidator(); |
|
|
|
|
223
|
|
|
if ($validator !== null && !$validator->validate($model)) { |
224
|
|
|
return new NotValid([ |
225
|
|
|
'errors' => $validator->getValidationFailures() |
226
|
|
|
]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// save and dispath post save hooks |
230
|
|
|
$rows = $model->save(); |
231
|
|
|
$this->dispatch(ApplicationUriEvent::POST_UPDATE, $model, $data); |
232
|
|
|
$this->dispatch(ApplicationUriEvent::POST_SAVE, $model, $data); |
233
|
|
|
|
234
|
|
|
$payload = ['model' => $model]; |
235
|
|
|
|
236
|
|
|
if ($rows === 0) { |
237
|
|
|
return new NotUpdated($payload); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return new Updated($payload); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param mixed $query |
245
|
|
|
* @param mixed $filter |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
protected function applyFilter($query, $filter) { |
249
|
|
|
foreach ($filter as $column => $value) { |
250
|
|
|
$pos = strpos($column, '.'); |
251
|
|
|
if ($pos !== false) { |
252
|
|
|
$rel = NameUtils::toStudlyCase(substr($column, 0, $pos)); |
253
|
|
|
$col = substr($column, $pos + 1); |
254
|
|
|
$method = 'use' . $rel . 'Query'; |
255
|
|
|
if (method_exists($query, $method)) { |
256
|
|
|
$sub = $query->$method(); |
257
|
|
|
$this->applyFilter($sub, [$col => $value]); |
258
|
|
|
$sub->endUse(); |
259
|
|
|
} |
260
|
|
|
} else { |
261
|
|
|
$method = 'filterBy' . NameUtils::toStudlyCase($column); |
262
|
|
|
if (method_exists($query, $method)) { |
263
|
|
|
$query->$method($value); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $type |
271
|
|
|
* @param ApplicationUri $model |
272
|
|
|
* @param array $data |
273
|
|
|
*/ |
274
|
|
|
protected function dispatch($type, ApplicationUri $model, array $data = []) { |
275
|
|
|
$methods = [ |
276
|
|
|
ApplicationUriEvent::PRE_CREATE => 'preCreate', |
277
|
|
|
ApplicationUriEvent::POST_CREATE => 'postCreate', |
278
|
|
|
ApplicationUriEvent::PRE_UPDATE => 'preUpdate', |
279
|
|
|
ApplicationUriEvent::POST_UPDATE => 'postUpdate', |
280
|
|
|
ApplicationUriEvent::PRE_DELETE => 'preDelete', |
281
|
|
|
ApplicationUriEvent::POST_DELETE => 'postDelete', |
282
|
|
|
ApplicationUriEvent::PRE_SAVE => 'preSave', |
283
|
|
|
ApplicationUriEvent::POST_SAVE => 'postSave' |
284
|
|
|
]; |
285
|
|
|
|
286
|
|
|
if (isset($methods[$type])) { |
287
|
|
|
$method = $methods[$type]; |
288
|
|
|
if (method_exists($this, $method)) { |
289
|
|
|
$this->$method($model, $data); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$dispatcher = $this->getServiceContainer()->getDispatcher(); |
294
|
|
|
$dispatcher->dispatch($type, new ApplicationUriEvent($model)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Internal mechanism to set the Application id |
299
|
|
|
* |
300
|
|
|
* @param ApplicationUri $model |
301
|
|
|
* @param mixed $relatedId |
302
|
|
|
*/ |
303
|
|
|
protected function doSetApplicationId(ApplicationUri $model, $relatedId) { |
304
|
|
|
if ($model->getApplicationId() !== $relatedId) { |
305
|
|
|
$model->setApplicationId($relatedId); |
306
|
|
|
|
307
|
|
|
return true; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Internal mechanism to set the Localization id |
315
|
|
|
* |
316
|
|
|
* @param ApplicationUri $model |
317
|
|
|
* @param mixed $relatedId |
318
|
|
|
*/ |
319
|
|
|
protected function doSetLocalizationId(ApplicationUri $model, $relatedId) { |
320
|
|
|
if ($model->getLocalizationId() !== $relatedId) { |
321
|
|
|
$model->setLocalizationId($relatedId); |
322
|
|
|
|
323
|
|
|
return true; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns one ApplicationUri with the given id from cache |
331
|
|
|
* |
332
|
|
|
* @param mixed $id |
333
|
|
|
* @return ApplicationUri|null |
334
|
|
|
*/ |
335
|
|
|
protected function get($id) { |
336
|
|
|
if ($this->pool === null) { |
337
|
|
|
$this->pool = new Map(); |
338
|
|
|
} else if ($this->pool->has($id)) { |
339
|
|
|
return $this->pool->get($id); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$model = ApplicationUriQuery::create()->findOneById($id); |
343
|
|
|
$this->pool->set($id, $model); |
344
|
|
|
|
345
|
|
|
return $model; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Returns the service container |
350
|
|
|
* |
351
|
|
|
* @return ServiceContainer |
352
|
|
|
*/ |
353
|
|
|
abstract protected function getServiceContainer(); |
354
|
|
|
} |
355
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.