Completed
Push — master ( 04c3fa...9e32f0 )
by Thomas
09:51
created

GroupDomainTrait::addUser()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
rs 8.439
cc 6
eloc 22
nc 10
nop 2
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 keeko\framework\utils\Parameters;
12
use keeko\framework\utils\NameUtils;
13
use keeko\core\event\GroupEvent;
14
use keeko\framework\domain\payload\Created;
15
use keeko\framework\domain\payload\NotValid;
16
use keeko\framework\domain\payload\Updated;
17
use keeko\framework\domain\payload\NotUpdated;
18
use keeko\framework\domain\payload\Deleted;
19
use keeko\framework\domain\payload\NotDeleted;
20
use keeko\core\model\UserQuery;
21
use keeko\core\model\UserGroupQuery;
22
use keeko\core\model\ActionQuery;
23
use keeko\core\model\GroupActionQuery;
24
25
/**
26
 */
27
trait GroupDomainTrait {
28
29
	/**
30
	 */
31
	protected $pool;
32
33
	/**
34
	 * Adds Action to Group
35
	 * 
36
	 * @param mixed $id
37
	 * @param mixed $data
38
	 * @return PayloadInterface
39
	 */
40
	public function addAction($id, $data) {
41
		// find
42
		$group = $this->get($id);
43
44
		if ($group === null) {
45
			return new NotFound(['message' => 'Group not found.']);
46
		}
47
		 
48
		// update
49
		$errors = [];
50
		foreach ($data as $entry) {
51
			if (!isset($entry['id'])) {
52
				$errors[] = 'Missing id for Action';
53
			}
54
			$action = ActionQuery::create()->findOneById($entry['id']);
55
			$group->addAction($action);
56
		}
57
58
		if (count($errors) > 0) {
59
			return new NotValid(['errors' => $errors]);
60
		}
61
62
		$event = new GroupEvent($group);
63
		$dispatcher = $this->getServiceContainer()->getDispatcher();
64
		$dispatcher->dispatch(GroupEvent::PRE_ACTION_ADD, $event);
65
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
66
		$rows = $group->save();
67
		$dispatcher->dispatch(GroupEvent::POST_ACTION_ADD, $event);
68
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
69
70
		if ($rows > 0) {
71
			return Updated(['model' => $group]);
72
		}
73
74
		return NotUpdated(['model' => $group]);
75
	}
76
77
	/**
78
	 * Adds User to Group
79
	 * 
80
	 * @param mixed $id
81
	 * @param mixed $data
82
	 * @return PayloadInterface
83
	 */
84
	public function addUser($id, $data) {
85
		// find
86
		$group = $this->get($id);
87
88
		if ($group === null) {
89
			return new NotFound(['message' => 'Group not found.']);
90
		}
91
		 
92
		// update
93
		$errors = [];
94
		foreach ($data as $entry) {
95
			if (!isset($entry['id'])) {
96
				$errors[] = 'Missing id for User';
97
			}
98
			$user = UserQuery::create()->findOneById($entry['id']);
99
			$group->addUser($user);
100
		}
101
102
		if (count($errors) > 0) {
103
			return new NotValid(['errors' => $errors]);
104
		}
105
106
		$event = new GroupEvent($group);
107
		$dispatcher = $this->getServiceContainer()->getDispatcher();
108
		$dispatcher->dispatch(GroupEvent::PRE_USER_ADD, $event);
109
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
110
		$rows = $group->save();
111
		$dispatcher->dispatch(GroupEvent::POST_USER_ADD, $event);
112
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
113
114
		if ($rows > 0) {
115
			return Updated(['model' => $group]);
116
		}
117
118
		return NotUpdated(['model' => $group]);
119
	}
120
121
	/**
122
	 * Creates a new Group with the provided data
123
	 * 
124
	 * @param mixed $data
125
	 * @return PayloadInterface
126
	 */
127
	public function create($data) {
128
		// hydrate
129
		$serializer = Group::getSerializer();
130
		$group = $serializer->hydrate(new Group(), $data);
131
132
		// validate
133
		if (!$group->validate()) {
134
			return new NotValid([
135
				'errors' => $group->getValidationFailures()
136
			]);
137
		}
138
139
		// dispatch
140
		$event = new GroupEvent($group);
141
		$dispatcher = $this->getServiceContainer()->getDispatcher();
142
		$dispatcher->dispatch(GroupEvent::PRE_CREATE, $event);
143
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
144
		$group->save();
145
		$dispatcher->dispatch(GroupEvent::POST_CREATE, $event);
146
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
147
		return new Created(['model' => $group]);
148
	}
149
150
	/**
151
	 * Deletes a Group with the given id
152
	 * 
153
	 * @param mixed $id
154
	 * @return PayloadInterface
155
	 */
156
	public function delete($id) {
157
		// find
158
		$group = $this->get($id);
159
160
		if ($group === null) {
161
			return new NotFound(['message' => 'Group not found.']);
162
		}
163
164
		// delete
165
		$event = new GroupEvent($group);
166
		$dispatcher = $this->getServiceContainer()->getDispatcher();
167
		$dispatcher->dispatch(GroupEvent::PRE_DELETE, $event);
168
		$group->delete();
169
170
		if ($group->isDeleted()) {
171
			$dispatcher->dispatch(GroupEvent::POST_DELETE, $event);
172
			return new Deleted(['model' => $group]);
173
		}
174
175
		return new NotDeleted(['message' => 'Could not delete Group']);
176
	}
177
178
	/**
179
	 * Returns a paginated result
180
	 * 
181
	 * @param Parameters $params
182
	 * @return PayloadInterface
183
	 */
184
	public function paginate(Parameters $params) {
185
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
186
		$defaultSize = $sysPrefs->getPaginationSize();
187
		$page = $params->getPage('number');
188
		$size = $params->getPage('size', $defaultSize);
189
190
		$query = GroupQuery::create();
191
192
		// sorting
193
		$sort = $params->getSort(Group::getSerializer()->getSortFields());
194
		foreach ($sort as $field => $order) {
195
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
196
			$query->$method($order);
197
		}
198
199
		// filtering
200
		$filter = $params->getFilter();
201
		if (!empty($filter)) {
202
			$this->applyFilter($query, $filter);
203
		}
204
205
		// paginate
206
		$group = $query->paginate($page, $size);
207
208
		// run response
209
		return new Found(['model' => $group]);
210
	}
211
212
	/**
213
	 * Returns one Group with the given id
214
	 * 
215
	 * @param mixed $id
216
	 * @return PayloadInterface
217
	 */
218
	public function read($id) {
219
		// read
220
		$group = $this->get($id);
221
222
		// check existence
223
		if ($group === null) {
224
			return new NotFound(['message' => 'Group not found.']);
225
		}
226
227
		return new Found(['model' => $group]);
228
	}
229
230
	/**
231
	 * Removes Action from Group
232
	 * 
233
	 * @param mixed $id
234
	 * @param mixed $data
235
	 * @return PayloadInterface
236
	 */
237
	public function removeAction($id, $data) {
238
		// find
239
		$group = $this->get($id);
240
241
		if ($group === null) {
242
			return new NotFound(['message' => 'Group not found.']);
243
		}
244
245
		// remove them
246
		$errors = [];
247
		foreach ($data as $entry) {
248
			if (!isset($entry['id'])) {
249
				$errors[] = 'Missing id for Action';
250
			}
251
			$action = ActionQuery::create()->findOneById($entry['id']);
252
			$group->removeAction($action);
253
		}
254
255
		if (count($errors) > 0) {
256
			return new NotValid(['errors' => $errors]);
257
		}
258
259
		$event = new GroupEvent($group);
260
		$dispatcher = $this->getServiceContainer()->getDispatcher();
261
		$dispatcher->dispatch(GroupEvent::PRE_ACTION_REMOVE, $event);
262
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
263
		$rows = $group->save();
264
		$dispatcher->dispatch(GroupEvent::POST_ACTION_REMOVE, $event);
265
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
266
267
		if ($rows > 0) {
268
			return Updated(['model' => $group]);
269
		}
270
271
		return NotUpdated(['model' => $group]);
272
	}
273
274
	/**
275
	 * Removes User from Group
276
	 * 
277
	 * @param mixed $id
278
	 * @param mixed $data
279
	 * @return PayloadInterface
280
	 */
281
	public function removeUser($id, $data) {
282
		// find
283
		$group = $this->get($id);
284
285
		if ($group === null) {
286
			return new NotFound(['message' => 'Group not found.']);
287
		}
288
289
		// remove them
290
		$errors = [];
291
		foreach ($data as $entry) {
292
			if (!isset($entry['id'])) {
293
				$errors[] = 'Missing id for User';
294
			}
295
			$user = UserQuery::create()->findOneById($entry['id']);
296
			$group->removeUser($user);
297
		}
298
299
		if (count($errors) > 0) {
300
			return new NotValid(['errors' => $errors]);
301
		}
302
303
		$event = new GroupEvent($group);
304
		$dispatcher = $this->getServiceContainer()->getDispatcher();
305
		$dispatcher->dispatch(GroupEvent::PRE_USER_REMOVE, $event);
306
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
307
		$rows = $group->save();
308
		$dispatcher->dispatch(GroupEvent::POST_USER_REMOVE, $event);
309
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
310
311
		if ($rows > 0) {
312
			return Updated(['model' => $group]);
313
		}
314
315
		return NotUpdated(['model' => $group]);
316
	}
317
318
	/**
319
	 * Updates a Group with the given idand the provided data
320
	 * 
321
	 * @param mixed $id
322
	 * @param mixed $data
323
	 * @return PayloadInterface
324
	 */
325
	public function update($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
		// hydrate
334
		$serializer = Group::getSerializer();
335
		$group = $serializer->hydrate($group, $data);
336
337
		// validate
338
		if (!$group->validate()) {
339
			return new NotValid([
340
				'errors' => $group->getValidationFailures()
341
			]);
342
		}
343
344
345
		// dispatch
346
		$event = new GroupEvent($group);
347
		$dispatcher = $this->getServiceContainer()->getDispatcher();
348
		$dispatcher->dispatch(GroupEvent::PRE_UPDATE, $event);
349
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
350
		$rows = $group->save();
351
		$dispatcher->dispatch(GroupEvent::POST_UPDATE, $event);
352
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
353
354
		$payload = ['model' => $group];
355
356
		if ($rows === 0) {
357
			return new NotUpdated($payload);
358
		}
359
360
		return new Updated($payload);
361
	}
362
363
	/**
364
	 * Updates Action on Group
365
	 * 
366
	 * @param mixed $id
367
	 * @param mixed $data
368
	 * @return PayloadInterface
369
	 */
370
	public function updateAction($id, $data) {
371
		// find
372
		$group = $this->get($id);
373
374
		if ($group === null) {
375
			return new NotFound(['message' => 'Group not found.']);
376
		}
377
378
		// remove all relationships before
379
		GroupActionQuery::create()->filterByGroup($group)->delete();
380
381
		// add them
382
		$errors = [];
383
		foreach ($data as $entry) {
384
			if (!isset($entry['id'])) {
385
				$errors[] = 'Missing id for Action';
386
			}
387
			$action = ActionQuery::create()->findOneById($entry['id']);
388
			$group->addAction($action);
389
		}
390
391
		if (count($errors) > 0) {
392
			return new NotValid(['errors' => $errors]);
393
		}
394
395
396
		$event = new GroupEvent($group);
397
		$dispatcher = $this->getServiceContainer()->getDispatcher();
398
		$dispatcher->dispatch(GroupEvent::PRE_ACTION_UPDATE, $event);
399
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
400
		$rows = $group->save();
401
		$dispatcher->dispatch(GroupEvent::POST_ACTION_UPDATE, $event);
402
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
403
404
		if ($rows > 0) {
405
			return Updated(['model' => $group]);
406
		}
407
408
		return NotUpdated(['model' => $group]);
409
	}
410
411
	/**
412
	 * Updates User on Group
413
	 * 
414
	 * @param mixed $id
415
	 * @param mixed $data
416
	 * @return PayloadInterface
417
	 */
418
	public function updateUser($id, $data) {
419
		// find
420
		$group = $this->get($id);
421
422
		if ($group === null) {
423
			return new NotFound(['message' => 'Group not found.']);
424
		}
425
426
		// remove all relationships before
427
		UserGroupQuery::create()->filterByGroup($group)->delete();
428
429
		// add them
430
		$errors = [];
431
		foreach ($data as $entry) {
432
			if (!isset($entry['id'])) {
433
				$errors[] = 'Missing id for User';
434
			}
435
			$user = UserQuery::create()->findOneById($entry['id']);
436
			$group->addUser($user);
437
		}
438
439
		if (count($errors) > 0) {
440
			return new NotValid(['errors' => $errors]);
441
		}
442
443
444
		$event = new GroupEvent($group);
445
		$dispatcher = $this->getServiceContainer()->getDispatcher();
446
		$dispatcher->dispatch(GroupEvent::PRE_USER_UPDATE, $event);
447
		$dispatcher->dispatch(GroupEvent::PRE_SAVE, $event);
448
		$rows = $group->save();
449
		$dispatcher->dispatch(GroupEvent::POST_USER_UPDATE, $event);
450
		$dispatcher->dispatch(GroupEvent::POST_SAVE, $event);
451
452
		if ($rows > 0) {
453
			return Updated(['model' => $group]);
454
		}
455
456
		return NotUpdated(['model' => $group]);
457
	}
458
459
	/**
460
	 * Implement this functionality at keeko\core\domain\GroupDomain
461
	 * 
462
	 * @param GroupQuery $query
463
	 * @param mixed $filter
464
	 * @return void
465
	 */
466
	abstract protected function applyFilter(GroupQuery $query, $filter);
467
468
	/**
469
	 * Returns one Group with the given id from cache
470
	 * 
471
	 * @param mixed $id
472
	 * @return Group|null
473
	 */
474
	protected function get($id) {
475
		if ($this->pool === null) {
476
			$this->pool = new Map();
477
		} else if ($this->pool->has($id)) {
478
			return $this->pool->get($id);
479
		}
480
481
		$group = GroupQuery::create()->findOneById($id);
482
		$this->pool->set($id, $group);
483
484
		return $group;
485
	}
486
487
	/**
488
	 * Returns the service container
489
	 * 
490
	 * @return ServiceContainer
491
	 */
492
	abstract protected function getServiceContainer();
493
}
494