1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\domain\base; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\Group; |
5
|
|
|
use keeko\core\model\GroupQuery; |
6
|
|
|
use keeko\framework\service\ServiceContainer; |
7
|
|
|
use keeko\framework\domain\payload\PayloadInterface; |
8
|
|
|
use phootwork\collection\Map; |
9
|
|
|
use keeko\framework\domain\payload\Found; |
10
|
|
|
use keeko\framework\domain\payload\NotFound; |
11
|
|
|
use Tobscure\JsonApi\Parameters; |
12
|
|
|
use keeko\framework\utils\NameUtils; |
13
|
|
|
use keeko\framework\domain\payload\Created; |
14
|
|
|
use keeko\framework\domain\payload\Updated; |
15
|
|
|
use keeko\framework\domain\payload\NotUpdated; |
16
|
|
|
use keeko\framework\domain\payload\NotValid; |
17
|
|
|
use keeko\framework\domain\payload\Deleted; |
18
|
|
|
use keeko\framework\domain\payload\NotDeleted; |
19
|
|
|
use keeko\core\model\UserQuery; |
20
|
|
|
use keeko\core\model\UserGroupQuery; |
21
|
|
|
use keeko\core\model\ActionQuery; |
22
|
|
|
use keeko\core\model\GroupActionQuery; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
*/ |
26
|
|
|
trait GroupDomainTrait { |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
*/ |
30
|
|
|
protected $pool; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Adds Action to Group |
34
|
|
|
* |
35
|
|
|
* @param mixed $id |
36
|
|
|
* @param mixed $data |
37
|
|
|
* @return PayloadInterface |
38
|
|
|
*/ |
39
|
|
|
public function addAction($id, $data) { |
40
|
|
|
// find |
41
|
|
|
$group = $this->get($id); |
42
|
|
|
|
43
|
|
|
if ($group === null) { |
44
|
|
|
return new NotFound(['message' => 'Group not found.']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// update |
48
|
|
|
$errors = []; |
49
|
|
|
foreach ($data as $entry) { |
50
|
|
|
if (!isset($entry['id'])) { |
51
|
|
|
$errors[] = 'Missing id for Action'; |
52
|
|
|
} |
53
|
|
|
$action = ActionQuery::create()->findOneById($entry['id']); |
54
|
|
|
$group->addAction($action); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (count($errors) > 0) { |
58
|
|
|
return new NotValid(['errors' => $errors]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$rows = $group->save(); |
62
|
|
|
|
63
|
|
|
if ($rows > 0) { |
64
|
|
|
return Updated(['model' => $group]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return NotUpdated(['model' => $group]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds User to Group |
72
|
|
|
* |
73
|
|
|
* @param mixed $id |
74
|
|
|
* @param mixed $data |
75
|
|
|
* @return PayloadInterface |
76
|
|
|
*/ |
77
|
|
|
public function addUser($id, $data) { |
78
|
|
|
// find |
79
|
|
|
$group = $this->get($id); |
80
|
|
|
|
81
|
|
|
if ($group === null) { |
82
|
|
|
return new NotFound(['message' => 'Group not found.']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// update |
86
|
|
|
$errors = []; |
87
|
|
|
foreach ($data as $entry) { |
88
|
|
|
if (!isset($entry['id'])) { |
89
|
|
|
$errors[] = 'Missing id for User'; |
90
|
|
|
} |
91
|
|
|
$user = UserQuery::create()->findOneById($entry['id']); |
92
|
|
|
$group->addUser($user); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (count($errors) > 0) { |
96
|
|
|
return new NotValid(['errors' => $errors]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$rows = $group->save(); |
100
|
|
|
|
101
|
|
|
if ($rows > 0) { |
102
|
|
|
return Updated(['model' => $group]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return NotUpdated(['model' => $group]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Creates a new Group with the provided data |
110
|
|
|
* |
111
|
|
|
* @param mixed $data |
112
|
|
|
* @return PayloadInterface |
113
|
|
|
*/ |
114
|
|
|
public function create($data) { |
115
|
|
|
// hydrate |
116
|
|
|
$serializer = Group::getSerializer(); |
117
|
|
|
$group = $serializer->hydrate(new Group(), $data); |
118
|
|
|
|
119
|
|
|
// validate |
120
|
|
|
if (!$group->validate()) { |
121
|
|
|
return new NotValid([ |
122
|
|
|
'errors' => $group->getValidationFailures() |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$group->save(); |
127
|
|
|
return new Created(['model' => $group]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Deletes a Group with the given id |
132
|
|
|
* |
133
|
|
|
* @param mixed $id |
134
|
|
|
* @return PayloadInterface |
135
|
|
|
*/ |
136
|
|
|
public function delete($id) { |
137
|
|
|
// find |
138
|
|
|
$group = $this->get($id); |
139
|
|
|
|
140
|
|
|
if ($group === null) { |
141
|
|
|
return new NotFound(['message' => 'Group not found.']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// delete |
145
|
|
|
$group->delete(); |
146
|
|
|
|
147
|
|
|
if ($group->isDeleted()) { |
148
|
|
|
return new Deleted(['model' => $group]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new NotDeleted(['message' => 'Could not delete Group']); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a paginated result |
156
|
|
|
* |
157
|
|
|
* @param Parameters $params |
158
|
|
|
* @return PayloadInterface |
159
|
|
|
*/ |
160
|
|
|
public function paginate(Parameters $params) { |
161
|
|
|
$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences(); |
162
|
|
|
$defaultSize = $sysPrefs->getPaginationSize(); |
163
|
|
|
$page = $params->getPage('number'); |
|
|
|
|
164
|
|
|
$size = $params->getPage('size', $defaultSize); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$query = GroupQuery::create(); |
167
|
|
|
|
168
|
|
|
// sorting |
169
|
|
|
$sort = $params->getSort(Group::getSerializer()->getSortFields()); |
170
|
|
|
foreach ($sort as $field => $order) { |
171
|
|
|
$method = 'orderBy' . NameUtils::toStudlyCase($field); |
172
|
|
|
$query->$method($order); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// filtering |
176
|
|
|
$filter = $params->getFilter(); |
177
|
|
|
if (!empty($filter)) { |
178
|
|
|
$this->applyFilter($query, $filter); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// paginate |
182
|
|
|
$group = $query->paginate($page, $size); |
183
|
|
|
|
184
|
|
|
// run response |
185
|
|
|
return new Found(['model' => $group]); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns one Group with the given id |
190
|
|
|
* |
191
|
|
|
* @param mixed $id |
192
|
|
|
* @return PayloadInterface |
193
|
|
|
*/ |
194
|
|
|
public function read($id) { |
195
|
|
|
// read |
196
|
|
|
$group = $this->get($id); |
197
|
|
|
|
198
|
|
|
// check existence |
199
|
|
|
if ($group === null) { |
200
|
|
|
return new NotFound(['message' => 'Group not found.']); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return new Found(['model' => $group]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Removes Action from Group |
208
|
|
|
* |
209
|
|
|
* @param mixed $id |
210
|
|
|
* @param mixed $data |
211
|
|
|
* @return PayloadInterface |
212
|
|
|
*/ |
213
|
|
|
public function removeAction($id, $data) { |
214
|
|
|
// find |
215
|
|
|
$group = $this->get($id); |
216
|
|
|
|
217
|
|
|
if ($group === null) { |
218
|
|
|
return new NotFound(['message' => 'Group not found.']); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// remove them |
222
|
|
|
$errors = []; |
223
|
|
|
foreach ($data as $entry) { |
224
|
|
|
if (!isset($entry['id'])) { |
225
|
|
|
$errors[] = 'Missing id for Action'; |
226
|
|
|
} |
227
|
|
|
$action = ActionQuery::create()->findOneById($entry['id']); |
228
|
|
|
$group->removeAction($action); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (count($errors) > 0) { |
232
|
|
|
return new NotValid(['errors' => $errors]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$rows = $group->save(); |
236
|
|
|
|
237
|
|
|
if ($rows > 0) { |
238
|
|
|
return Updated(['model' => $group]); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return NotUpdated(['model' => $group]); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Removes User from Group |
246
|
|
|
* |
247
|
|
|
* @param mixed $id |
248
|
|
|
* @param mixed $data |
249
|
|
|
* @return PayloadInterface |
250
|
|
|
*/ |
251
|
|
|
public function removeUser($id, $data) { |
252
|
|
|
// find |
253
|
|
|
$group = $this->get($id); |
254
|
|
|
|
255
|
|
|
if ($group === null) { |
256
|
|
|
return new NotFound(['message' => 'Group not found.']); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// remove them |
260
|
|
|
$errors = []; |
261
|
|
|
foreach ($data as $entry) { |
262
|
|
|
if (!isset($entry['id'])) { |
263
|
|
|
$errors[] = 'Missing id for User'; |
264
|
|
|
} |
265
|
|
|
$user = UserQuery::create()->findOneById($entry['id']); |
266
|
|
|
$group->removeUser($user); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if (count($errors) > 0) { |
270
|
|
|
return new NotValid(['errors' => $errors]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$rows = $group->save(); |
274
|
|
|
|
275
|
|
|
if ($rows > 0) { |
276
|
|
|
return Updated(['model' => $group]); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return NotUpdated(['model' => $group]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Updates a Group with the given idand the provided data |
284
|
|
|
* |
285
|
|
|
* @param mixed $id |
286
|
|
|
* @param mixed $data |
287
|
|
|
* @return PayloadInterface |
288
|
|
|
*/ |
289
|
|
|
public function update($id, $data) { |
290
|
|
|
// find |
291
|
|
|
$group = $this->get($id); |
292
|
|
|
|
293
|
|
|
if ($group === null) { |
294
|
|
|
return new NotFound(['message' => 'Group not found.']); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// hydrate |
298
|
|
|
$serializer = Group::getSerializer(); |
299
|
|
|
$group = $serializer->hydrate($group, $data); |
300
|
|
|
|
301
|
|
|
// validate |
302
|
|
|
if (!$group->validate()) { |
303
|
|
|
return new NotValid([ |
304
|
|
|
'errors' => $group->getValidationFailures() |
305
|
|
|
]); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$rows = $group->save(); |
309
|
|
|
$payload = ['model' => $group]; |
310
|
|
|
|
311
|
|
|
if ($rows === 0) { |
312
|
|
|
return new NotUpdated($payload); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return new Updated($payload); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Updates Action on Group |
320
|
|
|
* |
321
|
|
|
* @param mixed $id |
322
|
|
|
* @param mixed $data |
323
|
|
|
* @return PayloadInterface |
324
|
|
|
*/ |
325
|
|
|
public function updateAction($id, $data) { |
326
|
|
|
// find |
327
|
|
|
$group = $this->get($id); |
328
|
|
|
|
329
|
|
|
if ($group === null) { |
330
|
|
|
return new NotFound(['message' => 'Group not found.']); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
// remove all relationships before |
334
|
|
|
GroupActionQuery::create()->filterByGroup($group)->delete(); |
335
|
|
|
|
336
|
|
|
// add them |
337
|
|
|
$errors = []; |
338
|
|
|
foreach ($data as $entry) { |
339
|
|
|
if (!isset($entry['id'])) { |
340
|
|
|
$errors[] = 'Missing id for Action'; |
341
|
|
|
} |
342
|
|
|
$action = ActionQuery::create()->findOneById($entry['id']); |
343
|
|
|
$group->addAction($action); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if (count($errors) > 0) { |
347
|
|
|
return new NotValid(['errors' => $errors]); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$rows = $group->save(); |
351
|
|
|
|
352
|
|
|
if ($rows > 0) { |
353
|
|
|
return Updated(['model' => $group]); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return NotUpdated(['model' => $group]); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Updates User on Group |
361
|
|
|
* |
362
|
|
|
* @param mixed $id |
363
|
|
|
* @param mixed $data |
364
|
|
|
* @return PayloadInterface |
365
|
|
|
*/ |
366
|
|
|
public function updateUser($id, $data) { |
367
|
|
|
// find |
368
|
|
|
$group = $this->get($id); |
369
|
|
|
|
370
|
|
|
if ($group === null) { |
371
|
|
|
return new NotFound(['message' => 'Group not found.']); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
// remove all relationships before |
375
|
|
|
UserGroupQuery::create()->filterByGroup($group)->delete(); |
376
|
|
|
|
377
|
|
|
// add them |
378
|
|
|
$errors = []; |
379
|
|
|
foreach ($data as $entry) { |
380
|
|
|
if (!isset($entry['id'])) { |
381
|
|
|
$errors[] = 'Missing id for User'; |
382
|
|
|
} |
383
|
|
|
$user = UserQuery::create()->findOneById($entry['id']); |
384
|
|
|
$group->addUser($user); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
if (count($errors) > 0) { |
388
|
|
|
return new NotValid(['errors' => $errors]); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$rows = $group->save(); |
392
|
|
|
|
393
|
|
|
if ($rows > 0) { |
394
|
|
|
return Updated(['model' => $group]); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return NotUpdated(['model' => $group]); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Implement this functionality at keeko\core\domain\GroupDomain |
402
|
|
|
* |
403
|
|
|
* @param GroupQuery $query |
404
|
|
|
* @param mixed $filter |
405
|
|
|
*/ |
406
|
|
|
abstract protected function applyFilter(GroupQuery $query, $filter); |
|
|
|
|
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Returns one Group with the given id from cache |
410
|
|
|
* |
411
|
|
|
* @param mixed $id |
412
|
|
|
* @return Group|null |
413
|
|
|
*/ |
414
|
|
|
protected function get($id) { |
415
|
|
|
if ($this->pool === null) { |
416
|
|
|
$this->pool = new Map(); |
417
|
|
|
} else if ($this->pool->has($id)) { |
418
|
|
|
return $this->pool->get($id); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
$group = GroupQuery::create()->findOneById($id); |
422
|
|
|
$this->pool->set($id, $group); |
423
|
|
|
|
424
|
|
|
return $group; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Returns the service container |
429
|
|
|
* |
430
|
|
|
* @return ServiceContainer |
431
|
|
|
*/ |
432
|
|
|
abstract protected function getServiceContainer(); |
433
|
|
|
} |
434
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.