UserDomainTrait::doUpdateGroups()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 11
nc 6
nop 2
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\event\UserEvent;
5
use keeko\core\model\ActivityQuery;
6
use keeko\core\model\GroupQuery;
7
use keeko\core\model\SessionQuery;
8
use keeko\core\model\User;
9
use keeko\core\model\UserGroupQuery;
10
use keeko\core\model\UserQuery;
11
use keeko\framework\domain\payload\Created;
12
use keeko\framework\domain\payload\Deleted;
13
use keeko\framework\domain\payload\Found;
14
use keeko\framework\domain\payload\NotDeleted;
15
use keeko\framework\domain\payload\NotFound;
16
use keeko\framework\domain\payload\NotUpdated;
17
use keeko\framework\domain\payload\NotValid;
18
use keeko\framework\domain\payload\PayloadInterface;
19
use keeko\framework\domain\payload\Updated;
20
use keeko\framework\exceptions\ErrorsException;
21
use keeko\framework\service\ServiceContainer;
22
use keeko\framework\utils\NameUtils;
23
use keeko\framework\utils\Parameters;
24
use phootwork\collection\Map;
25
26
/**
27
 */
28
trait UserDomainTrait {
29
30
	/**
31
	 */
32
	protected $pool;
33
34
	/**
35
	 * Adds Activities to User
36
	 * 
37
	 * @param mixed $id
38
	 * @param mixed $data
39
	 * @return PayloadInterface
40
	 */
41
	public function addActivities($id, $data) {
42
		// find
43
		$model = $this->get($id);
44
45
		if ($model === null) {
46
			return new NotFound(['message' => 'User not found.']);
47
		}
48
49
		// pass add to internal logic
50
		try {
51
			$this->doAddActivities($model, $data);
52
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
53
			return new NotValid(['errors' => $e->getErrors()]);
54
		}
55
56
		// save and dispatch events
57
		$this->dispatch(UserEvent::PRE_ACTIVITIES_ADD, $model, $data);
58
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
59
		$rows = $model->save();
60
		$this->dispatch(UserEvent::POST_ACTIVITIES_ADD, $model, $data);
61
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
62
63
		if ($rows > 0) {
64
			return Updated(['model' => $model]);
65
		}
66
67
		return NotUpdated(['model' => $model]);
68
	}
69
70
	/**
71
	 * Adds Groups to User
72
	 * 
73
	 * @param mixed $id
74
	 * @param mixed $data
75
	 * @return PayloadInterface
76
	 */
77
	public function addGroups($id, $data) {
78
		// find
79
		$model = $this->get($id);
80
81
		if ($model === null) {
82
			return new NotFound(['message' => 'User not found.']);
83
		}
84
85
		// pass add to internal logic
86
		try {
87
			$this->doAddGroups($model, $data);
88
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
89
			return new NotValid(['errors' => $e->getErrors()]);
90
		}
91
92
		// save and dispatch events
93
		$this->dispatch(UserEvent::PRE_GROUPS_ADD, $model, $data);
94
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
95
		$rows = $model->save();
96
		$this->dispatch(UserEvent::POST_GROUPS_ADD, $model, $data);
97
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
98
99
		if ($rows > 0) {
100
			return Updated(['model' => $model]);
101
		}
102
103
		return NotUpdated(['model' => $model]);
104
	}
105
106
	/**
107
	 * Adds Sessions to User
108
	 * 
109
	 * @param mixed $id
110
	 * @param mixed $data
111
	 * @return PayloadInterface
112
	 */
113
	public function addSessions($id, $data) {
114
		// find
115
		$model = $this->get($id);
116
117
		if ($model === null) {
118
			return new NotFound(['message' => 'User not found.']);
119
		}
120
121
		// pass add to internal logic
122
		try {
123
			$this->doAddSessions($model, $data);
124
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
125
			return new NotValid(['errors' => $e->getErrors()]);
126
		}
127
128
		// save and dispatch events
129
		$this->dispatch(UserEvent::PRE_SESSIONS_ADD, $model, $data);
130
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
131
		$rows = $model->save();
132
		$this->dispatch(UserEvent::POST_SESSIONS_ADD, $model, $data);
133
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
134
135
		if ($rows > 0) {
136
			return Updated(['model' => $model]);
137
		}
138
139
		return NotUpdated(['model' => $model]);
140
	}
141
142
	/**
143
	 * Creates a new User with the provided data
144
	 * 
145
	 * @param mixed $data
146
	 * @return PayloadInterface
147
	 */
148
	public function create($data) {
149
		// hydrate
150
		$serializer = User::getSerializer();
151
		$model = $serializer->hydrate(new User(), $data);
152
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
It seems like hydrateRelationships() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
153
154
		// dispatch pre save hooks
155
		$this->dispatch(UserEvent::PRE_CREATE, $model, $data);
156
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
157
158
		// validate
159
		$validator = $this->getValidator();
0 ignored issues
show
Bug introduced by
It seems like getValidator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
160
		if ($validator !== null && !$validator->validate($model)) {
161
			return new NotValid([
162
				'errors' => $validator->getValidationFailures()
163
			]);
164
		}
165
166
		// save and dispatch post save hooks
167
		$model->save();
168
		$this->dispatch(UserEvent::POST_CREATE, $model, $data);
169
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
170
171
		return new Created(['model' => $model]);
172
	}
173
174
	/**
175
	 * Deletes a User with the given id
176
	 * 
177
	 * @param mixed $id
178
	 * @return PayloadInterface
179
	 */
180
	public function delete($id) {
181
		// find
182
		$model = $this->get($id);
183
184
		if ($model === null) {
185
			return new NotFound(['message' => 'User not found.']);
186
		}
187
188
		// delete
189
		$this->dispatch(UserEvent::PRE_DELETE, $model);
190
		$model->delete();
191
192
		if ($model->isDeleted()) {
193
			$this->dispatch(UserEvent::POST_DELETE, $model);
194
			return new Deleted(['model' => $model]);
195
		}
196
197
		return new NotDeleted(['message' => 'Could not delete User']);
198
	}
199
200
	/**
201
	 * Returns a paginated result
202
	 * 
203
	 * @param Parameters $params
204
	 * @return PayloadInterface
205
	 */
206
	public function paginate(Parameters $params) {
207
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
208
		$defaultSize = $sysPrefs->getPaginationSize();
209
		$page = $params->getPage('number');
210
		$size = $params->getPage('size', $defaultSize);
211
212
		$query = UserQuery::create();
213
214
		// sorting
215
		$sort = $params->getSort(User::getSerializer()->getSortFields());
216
		foreach ($sort as $field => $order) {
217
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
218
			$query->$method($order);
219
		}
220
221
		// filtering
222
		$filter = $params->getFilter();
223
		if (!empty($filter)) {
224
			$this->applyFilter($query, $filter);
225
		}
226
227
		// paginate
228
		$model = $query->paginate($page, $size);
229
230
		// run response
231
		return new Found(['model' => $model]);
232
	}
233
234
	/**
235
	 * Returns one User with the given id
236
	 * 
237
	 * @param mixed $id
238
	 * @return PayloadInterface
239
	 */
240
	public function read($id) {
241
		// read
242
		$model = $this->get($id);
243
244
		// check existence
245
		if ($model === null) {
246
			return new NotFound(['message' => 'User not found.']);
247
		}
248
249
		return new Found(['model' => $model]);
250
	}
251
252
	/**
253
	 * Removes Activities from User
254
	 * 
255
	 * @param mixed $id
256
	 * @param mixed $data
257
	 * @return PayloadInterface
258
	 */
259
	public function removeActivities($id, $data) {
260
		// find
261
		$model = $this->get($id);
262
263
		if ($model === null) {
264
			return new NotFound(['message' => 'User not found.']);
265
		}
266
267
		// pass remove to internal logic
268
		try {
269
			$this->doRemoveActivities($model, $data);
270
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
271
			return new NotValid(['errors' => $e->getErrors()]);
272
		}
273
274
		// save and dispatch events
275
		$this->dispatch(UserEvent::PRE_ACTIVITIES_REMOVE, $model, $data);
276
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
277
		$rows = $model->save();
278
		$this->dispatch(UserEvent::POST_ACTIVITIES_REMOVE, $model, $data);
279
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
280
281
		if ($rows > 0) {
282
			return Updated(['model' => $model]);
283
		}
284
285
		return NotUpdated(['model' => $model]);
286
	}
287
288
	/**
289
	 * Removes Groups from User
290
	 * 
291
	 * @param mixed $id
292
	 * @param mixed $data
293
	 * @return PayloadInterface
294
	 */
295
	public function removeGroups($id, $data) {
296
		// find
297
		$model = $this->get($id);
298
299
		if ($model === null) {
300
			return new NotFound(['message' => 'User not found.']);
301
		}
302
303
		// pass remove to internal logic
304
		try {
305
			$this->doRemoveGroups($model, $data);
306
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
307
			return new NotValid(['errors' => $e->getErrors()]);
308
		}
309
310
		// save and dispatch events
311
		$this->dispatch(UserEvent::PRE_GROUPS_REMOVE, $model, $data);
312
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
313
		$rows = $model->save();
314
		$this->dispatch(UserEvent::POST_GROUPS_REMOVE, $model, $data);
315
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
316
317
		if ($rows > 0) {
318
			return Updated(['model' => $model]);
319
		}
320
321
		return NotUpdated(['model' => $model]);
322
	}
323
324
	/**
325
	 * Removes Sessions from User
326
	 * 
327
	 * @param mixed $id
328
	 * @param mixed $data
329
	 * @return PayloadInterface
330
	 */
331
	public function removeSessions($id, $data) {
332
		// find
333
		$model = $this->get($id);
334
335
		if ($model === null) {
336
			return new NotFound(['message' => 'User not found.']);
337
		}
338
339
		// pass remove to internal logic
340
		try {
341
			$this->doRemoveSessions($model, $data);
342
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
343
			return new NotValid(['errors' => $e->getErrors()]);
344
		}
345
346
		// save and dispatch events
347
		$this->dispatch(UserEvent::PRE_SESSIONS_REMOVE, $model, $data);
348
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
349
		$rows = $model->save();
350
		$this->dispatch(UserEvent::POST_SESSIONS_REMOVE, $model, $data);
351
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
352
353
		if ($rows > 0) {
354
			return Updated(['model' => $model]);
355
		}
356
357
		return NotUpdated(['model' => $model]);
358
	}
359
360
	/**
361
	 * Updates a User with the given idand the provided data
362
	 * 
363
	 * @param mixed $id
364
	 * @param mixed $data
365
	 * @return PayloadInterface
366
	 */
367
	public function update($id, $data) {
368
		// find
369
		$model = $this->get($id);
370
371
		if ($model === null) {
372
			return new NotFound(['message' => 'User not found.']);
373
		}
374
375
		// hydrate
376
		$serializer = User::getSerializer();
377
		$model = $serializer->hydrate($model, $data);
378
		$this->hydrateRelationships($model, $data);
0 ignored issues
show
Bug introduced by
It seems like hydrateRelationships() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
379
380
		// dispatch pre save hooks
381
		$this->dispatch(UserEvent::PRE_UPDATE, $model, $data);
382
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
383
384
		// validate
385
		$validator = $this->getValidator();
0 ignored issues
show
Bug introduced by
It seems like getValidator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
386
		if ($validator !== null && !$validator->validate($model)) {
387
			return new NotValid([
388
				'errors' => $validator->getValidationFailures()
389
			]);
390
		}
391
392
		// save and dispath post save hooks
393
		$rows = $model->save();
394
		$this->dispatch(UserEvent::POST_UPDATE, $model, $data);
395
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
396
397
		$payload = ['model' => $model];
398
399
		if ($rows === 0) {
400
			return new NotUpdated($payload);
401
		}
402
403
		return new Updated($payload);
404
	}
405
406
	/**
407
	 * Updates Activities on User
408
	 * 
409
	 * @param mixed $id
410
	 * @param mixed $data
411
	 * @return PayloadInterface
412
	 */
413
	public function updateActivities($id, $data) {
414
		// find
415
		$model = $this->get($id);
416
417
		if ($model === null) {
418
			return new NotFound(['message' => 'User not found.']);
419
		}
420
421
		// pass update to internal logic
422
		try {
423
			$this->doUpdateActivities($model, $data);
424
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
425
			return new NotValid(['errors' => $e->getErrors()]);
426
		}
427
428
		// save and dispatch events
429
		$this->dispatch(UserEvent::PRE_ACTIVITIES_UPDATE, $model, $data);
430
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
431
		$rows = $model->save();
432
		$this->dispatch(UserEvent::POST_ACTIVITIES_UPDATE, $model, $data);
433
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
434
435
		if ($rows > 0) {
436
			return Updated(['model' => $model]);
437
		}
438
439
		return NotUpdated(['model' => $model]);
440
	}
441
442
	/**
443
	 * Updates Groups on User
444
	 * 
445
	 * @param mixed $id
446
	 * @param mixed $data
447
	 * @return PayloadInterface
448
	 */
449
	public function updateGroups($id, $data) {
450
		// find
451
		$model = $this->get($id);
452
453
		if ($model === null) {
454
			return new NotFound(['message' => 'User not found.']);
455
		}
456
457
		// pass update to internal logic
458
		try {
459
			$this->doUpdateGroups($model, $data);
460
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
461
			return new NotValid(['errors' => $e->getErrors()]);
462
		}
463
464
		// save and dispatch events
465
		$this->dispatch(UserEvent::PRE_GROUPS_UPDATE, $model, $data);
466
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
467
		$rows = $model->save();
468
		$this->dispatch(UserEvent::POST_GROUPS_UPDATE, $model, $data);
469
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
470
471
		if ($rows > 0) {
472
			return Updated(['model' => $model]);
473
		}
474
475
		return NotUpdated(['model' => $model]);
476
	}
477
478
	/**
479
	 * Updates Sessions on User
480
	 * 
481
	 * @param mixed $id
482
	 * @param mixed $data
483
	 * @return PayloadInterface
484
	 */
485
	public function updateSessions($id, $data) {
486
		// find
487
		$model = $this->get($id);
488
489
		if ($model === null) {
490
			return new NotFound(['message' => 'User not found.']);
491
		}
492
493
		// pass update to internal logic
494
		try {
495
			$this->doUpdateSessions($model, $data);
496
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
497
			return new NotValid(['errors' => $e->getErrors()]);
498
		}
499
500
		// save and dispatch events
501
		$this->dispatch(UserEvent::PRE_SESSIONS_UPDATE, $model, $data);
502
		$this->dispatch(UserEvent::PRE_SAVE, $model, $data);
503
		$rows = $model->save();
504
		$this->dispatch(UserEvent::POST_SESSIONS_UPDATE, $model, $data);
505
		$this->dispatch(UserEvent::POST_SAVE, $model, $data);
506
507
		if ($rows > 0) {
508
			return Updated(['model' => $model]);
509
		}
510
511
		return NotUpdated(['model' => $model]);
512
	}
513
514
	/**
515
	 * @param mixed $query
516
	 * @param mixed $filter
517
	 * @return void
518
	 */
519
	protected function applyFilter($query, $filter) {
520
		foreach ($filter as $column => $value) {
521
			$pos = strpos($column, '.');
522
			if ($pos !== false) {
523
				$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
524
				$col = substr($column, $pos + 1);
525
				$method = 'use' . $rel . 'Query';
526
				if (method_exists($query, $method)) {
527
					$sub = $query->$method();
528
					$this->applyFilter($sub, [$col => $value]);
529
					$sub->endUse();
530
				}
531
			} else {
532
				$method = 'filterBy' . NameUtils::toStudlyCase($column);
533
				if (method_exists($query, $method)) {
534
					$query->$method($value);
535
				}
536
			}
537
		}
538
	}
539
540
	/**
541
	 * @param string $type
542
	 * @param User $model
543
	 * @param array $data
544
	 */
545
	protected function dispatch($type, User $model, array $data = []) {
546
		$methods = [
547
			UserEvent::PRE_CREATE => 'preCreate',
548
			UserEvent::POST_CREATE => 'postCreate',
549
			UserEvent::PRE_UPDATE => 'preUpdate',
550
			UserEvent::POST_UPDATE => 'postUpdate',
551
			UserEvent::PRE_DELETE => 'preDelete',
552
			UserEvent::POST_DELETE => 'postDelete',
553
			UserEvent::PRE_SAVE => 'preSave',
554
			UserEvent::POST_SAVE => 'postSave'
555
		];
556
557
		if (isset($methods[$type])) {
558
			$method = $methods[$type];
559
			if (method_exists($this, $method)) {
560
				$this->$method($model, $data);
561
			}
562
		}
563
564
		$dispatcher = $this->getServiceContainer()->getDispatcher();
565
		$dispatcher->dispatch($type, new UserEvent($model));
566
	}
567
568
	/**
569
	 * Interal mechanism to add Activities to User
570
	 * 
571
	 * @param User $model
572
	 * @param mixed $data
573
	 */
574
	protected function doAddActivities(User $model, $data) {
575
		$errors = [];
576
		foreach ($data as $entry) {
577
			if (!isset($entry['id'])) {
578
				$errors[] = 'Missing id for Activity';
579
			} else {
580
				$related = ActivityQuery::create()->findOneById($entry['id']);
581
				$model->addActivity($related);
582
			}
583
		}
584
585
		if (count($errors) > 0) {
586
			return new ErrorsException($errors);
587
		}
588
	}
589
590
	/**
591
	 * Interal mechanism to add Groups to User
592
	 * 
593
	 * @param User $model
594
	 * @param mixed $data
595
	 */
596
	protected function doAddGroups(User $model, $data) {
597
		$errors = [];
598
		foreach ($data as $entry) {
599
			if (!isset($entry['id'])) {
600
				$errors[] = 'Missing id for Group';
601
			} else {
602
				$related = GroupQuery::create()->findOneById($entry['id']);
603
				$model->addGroup($related);
604
			}
605
		}
606
607
		if (count($errors) > 0) {
608
			return new ErrorsException($errors);
609
		}
610
	}
611
612
	/**
613
	 * Interal mechanism to add Sessions to User
614
	 * 
615
	 * @param User $model
616
	 * @param mixed $data
617
	 */
618
	protected function doAddSessions(User $model, $data) {
619
		$errors = [];
620
		foreach ($data as $entry) {
621
			if (!isset($entry['id'])) {
622
				$errors[] = 'Missing id for Session';
623
			} else {
624
				$related = SessionQuery::create()->findOneById($entry['id']);
625
				$model->addSession($related);
626
			}
627
		}
628
629
		if (count($errors) > 0) {
630
			return new ErrorsException($errors);
631
		}
632
	}
633
634
	/**
635
	 * Interal mechanism to remove Activities from User
636
	 * 
637
	 * @param User $model
638
	 * @param mixed $data
639
	 */
640
	protected function doRemoveActivities(User $model, $data) {
641
		$errors = [];
642
		foreach ($data as $entry) {
643
			if (!isset($entry['id'])) {
644
				$errors[] = 'Missing id for Activity';
645
			} else {
646
				$related = ActivityQuery::create()->findOneById($entry['id']);
647
				$model->removeActivity($related);
648
			}
649
		}
650
651
		if (count($errors) > 0) {
652
			return new ErrorsException($errors);
653
		}
654
	}
655
656
	/**
657
	 * Interal mechanism to remove Groups from User
658
	 * 
659
	 * @param User $model
660
	 * @param mixed $data
661
	 */
662
	protected function doRemoveGroups(User $model, $data) {
663
		$errors = [];
664
		foreach ($data as $entry) {
665
			if (!isset($entry['id'])) {
666
				$errors[] = 'Missing id for Group';
667
			} else {
668
				$related = GroupQuery::create()->findOneById($entry['id']);
669
				$model->removeGroup($related);
670
			}
671
		}
672
673
		if (count($errors) > 0) {
674
			return new ErrorsException($errors);
675
		}
676
	}
677
678
	/**
679
	 * Interal mechanism to remove Sessions from User
680
	 * 
681
	 * @param User $model
682
	 * @param mixed $data
683
	 */
684
	protected function doRemoveSessions(User $model, $data) {
685
		$errors = [];
686
		foreach ($data as $entry) {
687
			if (!isset($entry['id'])) {
688
				$errors[] = 'Missing id for Session';
689
			} else {
690
				$related = SessionQuery::create()->findOneById($entry['id']);
691
				$model->removeSession($related);
692
			}
693
		}
694
695
		if (count($errors) > 0) {
696
			return new ErrorsException($errors);
697
		}
698
	}
699
700
	/**
701
	 * Internal update mechanism of Activities on User
702
	 * 
703
	 * @param User $model
704
	 * @param mixed $data
705
	 */
706
	protected function doUpdateActivities(User $model, $data) {
707
		// remove all relationships before
708
		ActivityQuery::create()->filterByActor($model)->delete();
709
710
		// add them
711
		$errors = [];
712
		foreach ($data as $entry) {
713
			if (!isset($entry['id'])) {
714
				$errors[] = 'Missing id for Activity';
715
			} else {
716
				$related = ActivityQuery::create()->findOneById($entry['id']);
717
				$model->addActivity($related);
718
			}
719
		}
720
721
		if (count($errors) > 0) {
722
			throw new ErrorsException($errors);
723
		}
724
	}
725
726
	/**
727
	 * Internal update mechanism of Groups on User
728
	 * 
729
	 * @param User $model
730
	 * @param mixed $data
731
	 */
732
	protected function doUpdateGroups(User $model, $data) {
733
		// remove all relationships before
734
		UserGroupQuery::create()->filterByUser($model)->delete();
735
736
		// add them
737
		$errors = [];
738
		foreach ($data as $entry) {
739
			if (!isset($entry['id'])) {
740
				$errors[] = 'Missing id for Group';
741
			} else {
742
				$related = GroupQuery::create()->findOneById($entry['id']);
743
				$model->addGroup($related);
744
			}
745
		}
746
747
		if (count($errors) > 0) {
748
			throw new ErrorsException($errors);
749
		}
750
	}
751
752
	/**
753
	 * Internal update mechanism of Sessions on User
754
	 * 
755
	 * @param User $model
756
	 * @param mixed $data
757
	 */
758
	protected function doUpdateSessions(User $model, $data) {
759
		// remove all relationships before
760
		SessionQuery::create()->filterByUser($model)->delete();
761
762
		// add them
763
		$errors = [];
764
		foreach ($data as $entry) {
765
			if (!isset($entry['id'])) {
766
				$errors[] = 'Missing id for Session';
767
			} else {
768
				$related = SessionQuery::create()->findOneById($entry['id']);
769
				$model->addSession($related);
770
			}
771
		}
772
773
		if (count($errors) > 0) {
774
			throw new ErrorsException($errors);
775
		}
776
	}
777
778
	/**
779
	 * Returns one User with the given id from cache
780
	 * 
781
	 * @param mixed $id
782
	 * @return User|null
783
	 */
784
	protected function get($id) {
785
		if ($this->pool === null) {
786
			$this->pool = new Map();
787
		} else if ($this->pool->has($id)) {
788
			return $this->pool->get($id);
789
		}
790
791
		$model = UserQuery::create()->findOneById($id);
792
		$this->pool->set($id, $model);
793
794
		return $model;
795
	}
796
797
	/**
798
	 * Returns the service container
799
	 * 
800
	 * @return ServiceContainer
801
	 */
802
	abstract protected function getServiceContainer();
803
}
804