Completed
Push — master ( e8b3d2...889b57 )
by Thomas
14:29
created

UserDomainTrait::dispatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 17
nc 3
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
		$event = new UserEvent($model);
58
		$this->dispatch(UserEvent::PRE_ACTIVITIES_ADD, $event);
59
		$this->dispatch(UserEvent::PRE_SAVE, $event);
60
		$rows = $model->save();
61
		$this->dispatch(UserEvent::POST_ACTIVITIES_ADD, $event);
62
		$this->dispatch(UserEvent::POST_SAVE, $event);
63
64
		if ($rows > 0) {
65
			return Updated(['model' => $model]);
66
		}
67
68
		return NotUpdated(['model' => $model]);
69
	}
70
71
	/**
72
	 * Adds Groups to User
73
	 * 
74
	 * @param mixed $id
75
	 * @param mixed $data
76
	 * @return PayloadInterface
77
	 */
78
	public function addGroups($id, $data) {
79
		// find
80
		$model = $this->get($id);
81
82
		if ($model === null) {
83
			return new NotFound(['message' => 'User not found.']);
84
		}
85
86
		// pass add to internal logic
87
		try {
88
			$this->doAddGroups($model, $data);
89
		} 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...
90
			return new NotValid(['errors' => $e->getErrors()]);
91
		}
92
93
		// save and dispatch events
94
		$event = new UserEvent($model);
95
		$this->dispatch(UserEvent::PRE_GROUPS_ADD, $event);
96
		$this->dispatch(UserEvent::PRE_SAVE, $event);
97
		$rows = $model->save();
98
		$this->dispatch(UserEvent::POST_GROUPS_ADD, $event);
99
		$this->dispatch(UserEvent::POST_SAVE, $event);
100
101
		if ($rows > 0) {
102
			return Updated(['model' => $model]);
103
		}
104
105
		return NotUpdated(['model' => $model]);
106
	}
107
108
	/**
109
	 * Adds Sessions to User
110
	 * 
111
	 * @param mixed $id
112
	 * @param mixed $data
113
	 * @return PayloadInterface
114
	 */
115
	public function addSessions($id, $data) {
116
		// find
117
		$model = $this->get($id);
118
119
		if ($model === null) {
120
			return new NotFound(['message' => 'User not found.']);
121
		}
122
123
		// pass add to internal logic
124
		try {
125
			$this->doAddSessions($model, $data);
126
		} 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...
127
			return new NotValid(['errors' => $e->getErrors()]);
128
		}
129
130
		// save and dispatch events
131
		$event = new UserEvent($model);
132
		$this->dispatch(UserEvent::PRE_SESSIONS_ADD, $event);
133
		$this->dispatch(UserEvent::PRE_SAVE, $event);
134
		$rows = $model->save();
135
		$this->dispatch(UserEvent::POST_SESSIONS_ADD, $event);
136
		$this->dispatch(UserEvent::POST_SAVE, $event);
137
138
		if ($rows > 0) {
139
			return Updated(['model' => $model]);
140
		}
141
142
		return NotUpdated(['model' => $model]);
143
	}
144
145
	/**
146
	 * Creates a new User with the provided data
147
	 * 
148
	 * @param mixed $data
149
	 * @return PayloadInterface
150
	 */
151
	public function create($data) {
152
		// hydrate
153
		$serializer = User::getSerializer();
154
		$model = $serializer->hydrate(new User(), $data);
155
		$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...
156
157
		// validate
158
		$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...
159
		if ($validator !== null && !$validator->validate($model)) {
160
			return new NotValid([
161
				'errors' => $validator->getValidationFailures()
162
			]);
163
		}
164
165
		// dispatch
166
		$event = new UserEvent($model);
167
		$this->dispatch(UserEvent::PRE_CREATE, $event);
168
		$this->dispatch(UserEvent::PRE_SAVE, $event);
169
		$model->save();
170
		$this->dispatch(UserEvent::POST_CREATE, $event);
171
		$this->dispatch(UserEvent::POST_SAVE, $event);
172
		return new Created(['model' => $model]);
173
	}
174
175
	/**
176
	 * Deletes a User with the given id
177
	 * 
178
	 * @param mixed $id
179
	 * @return PayloadInterface
180
	 */
181
	public function delete($id) {
182
		// find
183
		$model = $this->get($id);
184
185
		if ($model === null) {
186
			return new NotFound(['message' => 'User not found.']);
187
		}
188
189
		// delete
190
		$event = new UserEvent($model);
191
		$this->dispatch(UserEvent::PRE_DELETE, $event);
192
		$model->delete();
193
194
		if ($model->isDeleted()) {
195
			$this->dispatch(UserEvent::POST_DELETE, $event);
196
			return new Deleted(['model' => $model]);
197
		}
198
199
		return new NotDeleted(['message' => 'Could not delete User']);
200
	}
201
202
	/**
203
	 * Returns a paginated result
204
	 * 
205
	 * @param Parameters $params
206
	 * @return PayloadInterface
207
	 */
208
	public function paginate(Parameters $params) {
209
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
210
		$defaultSize = $sysPrefs->getPaginationSize();
211
		$page = $params->getPage('number');
212
		$size = $params->getPage('size', $defaultSize);
213
214
		$query = UserQuery::create();
215
216
		// sorting
217
		$sort = $params->getSort(User::getSerializer()->getSortFields());
218
		foreach ($sort as $field => $order) {
219
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
220
			$query->$method($order);
221
		}
222
223
		// filtering
224
		$filter = $params->getFilter();
225
		if (!empty($filter)) {
226
			$this->applyFilter($query, $filter);
227
		}
228
229
		// paginate
230
		$model = $query->paginate($page, $size);
231
232
		// run response
233
		return new Found(['model' => $model]);
234
	}
235
236
	/**
237
	 * Returns one User with the given id
238
	 * 
239
	 * @param mixed $id
240
	 * @return PayloadInterface
241
	 */
242
	public function read($id) {
243
		// read
244
		$model = $this->get($id);
245
246
		// check existence
247
		if ($model === null) {
248
			return new NotFound(['message' => 'User not found.']);
249
		}
250
251
		return new Found(['model' => $model]);
252
	}
253
254
	/**
255
	 * Removes Activities from User
256
	 * 
257
	 * @param mixed $id
258
	 * @param mixed $data
259
	 * @return PayloadInterface
260
	 */
261
	public function removeActivities($id, $data) {
262
		// find
263
		$model = $this->get($id);
264
265
		if ($model === null) {
266
			return new NotFound(['message' => 'User not found.']);
267
		}
268
269
		// pass remove to internal logic
270
		try {
271
			$this->doRemoveActivities($model, $data);
272
		} 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...
273
			return new NotValid(['errors' => $e->getErrors()]);
274
		}
275
276
		// save and dispatch events
277
		$event = new UserEvent($model);
278
		$this->dispatch(UserEvent::PRE_ACTIVITIES_REMOVE, $event);
279
		$this->dispatch(UserEvent::PRE_SAVE, $event);
280
		$rows = $model->save();
281
		$this->dispatch(UserEvent::POST_ACTIVITIES_REMOVE, $event);
282
		$this->dispatch(UserEvent::POST_SAVE, $event);
283
284
		if ($rows > 0) {
285
			return Updated(['model' => $model]);
286
		}
287
288
		return NotUpdated(['model' => $model]);
289
	}
290
291
	/**
292
	 * Removes Groups from User
293
	 * 
294
	 * @param mixed $id
295
	 * @param mixed $data
296
	 * @return PayloadInterface
297
	 */
298
	public function removeGroups($id, $data) {
299
		// find
300
		$model = $this->get($id);
301
302
		if ($model === null) {
303
			return new NotFound(['message' => 'User not found.']);
304
		}
305
306
		// pass remove to internal logic
307
		try {
308
			$this->doRemoveGroups($model, $data);
309
		} 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...
310
			return new NotValid(['errors' => $e->getErrors()]);
311
		}
312
313
		// save and dispatch events
314
		$event = new UserEvent($model);
315
		$this->dispatch(UserEvent::PRE_GROUPS_REMOVE, $event);
316
		$this->dispatch(UserEvent::PRE_SAVE, $event);
317
		$rows = $model->save();
318
		$this->dispatch(UserEvent::POST_GROUPS_REMOVE, $event);
319
		$this->dispatch(UserEvent::POST_SAVE, $event);
320
321
		if ($rows > 0) {
322
			return Updated(['model' => $model]);
323
		}
324
325
		return NotUpdated(['model' => $model]);
326
	}
327
328
	/**
329
	 * Removes Sessions from User
330
	 * 
331
	 * @param mixed $id
332
	 * @param mixed $data
333
	 * @return PayloadInterface
334
	 */
335
	public function removeSessions($id, $data) {
336
		// find
337
		$model = $this->get($id);
338
339
		if ($model === null) {
340
			return new NotFound(['message' => 'User not found.']);
341
		}
342
343
		// pass remove to internal logic
344
		try {
345
			$this->doRemoveSessions($model, $data);
346
		} 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...
347
			return new NotValid(['errors' => $e->getErrors()]);
348
		}
349
350
		// save and dispatch events
351
		$event = new UserEvent($model);
352
		$this->dispatch(UserEvent::PRE_SESSIONS_REMOVE, $event);
353
		$this->dispatch(UserEvent::PRE_SAVE, $event);
354
		$rows = $model->save();
355
		$this->dispatch(UserEvent::POST_SESSIONS_REMOVE, $event);
356
		$this->dispatch(UserEvent::POST_SAVE, $event);
357
358
		if ($rows > 0) {
359
			return Updated(['model' => $model]);
360
		}
361
362
		return NotUpdated(['model' => $model]);
363
	}
364
365
	/**
366
	 * Updates a User with the given idand the provided data
367
	 * 
368
	 * @param mixed $id
369
	 * @param mixed $data
370
	 * @return PayloadInterface
371
	 */
372
	public function update($id, $data) {
373
		// find
374
		$model = $this->get($id);
375
376
		if ($model === null) {
377
			return new NotFound(['message' => 'User not found.']);
378
		}
379
380
		// hydrate
381
		$serializer = User::getSerializer();
382
		$model = $serializer->hydrate($model, $data);
383
		$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...
384
385
		// validate
386
		$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...
387
		if ($validator !== null && !$validator->validate($model)) {
388
			return new NotValid([
389
				'errors' => $validator->getValidationFailures()
390
			]);
391
		}
392
393
		// dispatch
394
		$event = new UserEvent($model);
395
		$this->dispatch(UserEvent::PRE_UPDATE, $event);
396
		$this->dispatch(UserEvent::PRE_SAVE, $event);
397
		$rows = $model->save();
398
		$this->dispatch(UserEvent::POST_UPDATE, $event);
399
		$this->dispatch(UserEvent::POST_SAVE, $event);
400
401
		$payload = ['model' => $model];
402
403
		if ($rows === 0) {
404
			return new NotUpdated($payload);
405
		}
406
407
		return new Updated($payload);
408
	}
409
410
	/**
411
	 * Updates Activities on User
412
	 * 
413
	 * @param mixed $id
414
	 * @param mixed $data
415
	 * @return PayloadInterface
416
	 */
417
	public function updateActivities($id, $data) {
418
		// find
419
		$model = $this->get($id);
420
421
		if ($model === null) {
422
			return new NotFound(['message' => 'User not found.']);
423
		}
424
425
		// pass update to internal logic
426
		try {
427
			$this->doUpdateActivities($model, $data);
428
		} 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...
429
			return new NotValid(['errors' => $e->getErrors()]);
430
		}
431
432
		// save and dispatch events
433
		$event = new UserEvent($model);
434
		$this->dispatch(UserEvent::PRE_ACTIVITIES_UPDATE, $event);
435
		$this->dispatch(UserEvent::PRE_SAVE, $event);
436
		$rows = $model->save();
437
		$this->dispatch(UserEvent::POST_ACTIVITIES_UPDATE, $event);
438
		$this->dispatch(UserEvent::POST_SAVE, $event);
439
440
		if ($rows > 0) {
441
			return Updated(['model' => $model]);
442
		}
443
444
		return NotUpdated(['model' => $model]);
445
	}
446
447
	/**
448
	 * Updates Groups on User
449
	 * 
450
	 * @param mixed $id
451
	 * @param mixed $data
452
	 * @return PayloadInterface
453
	 */
454
	public function updateGroups($id, $data) {
455
		// find
456
		$model = $this->get($id);
457
458
		if ($model === null) {
459
			return new NotFound(['message' => 'User not found.']);
460
		}
461
462
		// pass update to internal logic
463
		try {
464
			$this->doUpdateGroups($model, $data);
465
		} 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...
466
			return new NotValid(['errors' => $e->getErrors()]);
467
		}
468
469
		// save and dispatch events
470
		$event = new UserEvent($model);
471
		$this->dispatch(UserEvent::PRE_GROUPS_UPDATE, $event);
472
		$this->dispatch(UserEvent::PRE_SAVE, $event);
473
		$rows = $model->save();
474
		$this->dispatch(UserEvent::POST_GROUPS_UPDATE, $event);
475
		$this->dispatch(UserEvent::POST_SAVE, $event);
476
477
		if ($rows > 0) {
478
			return Updated(['model' => $model]);
479
		}
480
481
		return NotUpdated(['model' => $model]);
482
	}
483
484
	/**
485
	 * Updates Sessions on User
486
	 * 
487
	 * @param mixed $id
488
	 * @param mixed $data
489
	 * @return PayloadInterface
490
	 */
491
	public function updateSessions($id, $data) {
492
		// find
493
		$model = $this->get($id);
494
495
		if ($model === null) {
496
			return new NotFound(['message' => 'User not found.']);
497
		}
498
499
		// pass update to internal logic
500
		try {
501
			$this->doUpdateSessions($model, $data);
502
		} 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...
503
			return new NotValid(['errors' => $e->getErrors()]);
504
		}
505
506
		// save and dispatch events
507
		$event = new UserEvent($model);
508
		$this->dispatch(UserEvent::PRE_SESSIONS_UPDATE, $event);
509
		$this->dispatch(UserEvent::PRE_SAVE, $event);
510
		$rows = $model->save();
511
		$this->dispatch(UserEvent::POST_SESSIONS_UPDATE, $event);
512
		$this->dispatch(UserEvent::POST_SAVE, $event);
513
514
		if ($rows > 0) {
515
			return Updated(['model' => $model]);
516
		}
517
518
		return NotUpdated(['model' => $model]);
519
	}
520
521
	/**
522
	 * @param mixed $query
523
	 * @param mixed $filter
524
	 * @return void
525
	 */
526
	protected function applyFilter($query, $filter) {
527
		foreach ($filter as $column => $value) {
528
			$pos = strpos($column, '.');
529
			if ($pos !== false) {
530
				$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
531
				$col = substr($column, $pos + 1);
532
				$method = 'use' . $rel . 'Query';
533
				if (method_exists($query, $method)) {
534
					$sub = $query->$method();
535
					$this->applyFilter($sub, [$col => $value]);
536
					$sub->endUse();
537
				}
538
			} else {
539
				$method = 'filterBy' . NameUtils::toStudlyCase($column);
540
				if (method_exists($query, $method)) {
541
					$query->$method($value);
542
				}
543
			}
544
		}
545
	}
546
547
	/**
548
	 * @param string $type
549
	 * @param UserEvent $event
550
	 */
551
	protected function dispatch($type, UserEvent $event) {
552
		$model = $event->getUser();
553
		$methods = [
554
			UserEvent::PRE_CREATE => 'preCreate',
555
			UserEvent::POST_CREATE => 'postCreate',
556
			UserEvent::PRE_UPDATE => 'preUpdate',
557
			UserEvent::POST_UPDATE => 'postUpdate',
558
			UserEvent::PRE_DELETE => 'preDelete',
559
			UserEvent::POST_DELETE => 'postDelete',
560
			UserEvent::PRE_SAVE => 'preSave',
561
			UserEvent::POST_SAVE => 'postSave'
562
		];
563
564
		if (isset($methods[$type])) {
565
			$method = $methods[$type];
566
			if (method_exists($this, $method)) {
567
				$this->$method($model);
568
			}
569
		}
570
571
		$dispatcher = $this->getServiceContainer()->getDispatcher();
572
		$dispatcher->dispatch($type, $event);
573
	}
574
575
	/**
576
	 * Interal mechanism to add Activities to User
577
	 * 
578
	 * @param User $model
579
	 * @param mixed $data
580
	 */
581
	protected function doAddActivities(User $model, $data) {
582
		$errors = [];
583
		foreach ($data as $entry) {
584
			if (!isset($entry['id'])) {
585
				$errors[] = 'Missing id for Activity';
586
			} else {
587
				$related = ActivityQuery::create()->findOneById($entry['id']);
588
				$model->addActivity($related);
589
			}
590
		}
591
592
		if (count($errors) > 0) {
593
			return new ErrorsException($errors);
594
		}
595
	}
596
597
	/**
598
	 * Interal mechanism to add Groups to User
599
	 * 
600
	 * @param User $model
601
	 * @param mixed $data
602
	 */
603
	protected function doAddGroups(User $model, $data) {
604
		$errors = [];
605
		foreach ($data as $entry) {
606
			if (!isset($entry['id'])) {
607
				$errors[] = 'Missing id for Group';
608
			} else {
609
				$related = GroupQuery::create()->findOneById($entry['id']);
610
				$model->addGroup($related);
611
			}
612
		}
613
614
		if (count($errors) > 0) {
615
			return new ErrorsException($errors);
616
		}
617
	}
618
619
	/**
620
	 * Interal mechanism to add Sessions to User
621
	 * 
622
	 * @param User $model
623
	 * @param mixed $data
624
	 */
625
	protected function doAddSessions(User $model, $data) {
626
		$errors = [];
627
		foreach ($data as $entry) {
628
			if (!isset($entry['id'])) {
629
				$errors[] = 'Missing id for Session';
630
			} else {
631
				$related = SessionQuery::create()->findOneById($entry['id']);
632
				$model->addSession($related);
633
			}
634
		}
635
636
		if (count($errors) > 0) {
637
			return new ErrorsException($errors);
638
		}
639
	}
640
641
	/**
642
	 * Interal mechanism to remove Activities from User
643
	 * 
644
	 * @param User $model
645
	 * @param mixed $data
646
	 */
647
	protected function doRemoveActivities(User $model, $data) {
648
		$errors = [];
649
		foreach ($data as $entry) {
650
			if (!isset($entry['id'])) {
651
				$errors[] = 'Missing id for Activity';
652
			} else {
653
				$related = ActivityQuery::create()->findOneById($entry['id']);
654
				$model->removeActivity($related);
655
			}
656
		}
657
658
		if (count($errors) > 0) {
659
			return new ErrorsException($errors);
660
		}
661
	}
662
663
	/**
664
	 * Interal mechanism to remove Groups from User
665
	 * 
666
	 * @param User $model
667
	 * @param mixed $data
668
	 */
669
	protected function doRemoveGroups(User $model, $data) {
670
		$errors = [];
671
		foreach ($data as $entry) {
672
			if (!isset($entry['id'])) {
673
				$errors[] = 'Missing id for Group';
674
			} else {
675
				$related = GroupQuery::create()->findOneById($entry['id']);
676
				$model->removeGroup($related);
677
			}
678
		}
679
680
		if (count($errors) > 0) {
681
			return new ErrorsException($errors);
682
		}
683
	}
684
685
	/**
686
	 * Interal mechanism to remove Sessions from User
687
	 * 
688
	 * @param User $model
689
	 * @param mixed $data
690
	 */
691
	protected function doRemoveSessions(User $model, $data) {
692
		$errors = [];
693
		foreach ($data as $entry) {
694
			if (!isset($entry['id'])) {
695
				$errors[] = 'Missing id for Session';
696
			} else {
697
				$related = SessionQuery::create()->findOneById($entry['id']);
698
				$model->removeSession($related);
699
			}
700
		}
701
702
		if (count($errors) > 0) {
703
			return new ErrorsException($errors);
704
		}
705
	}
706
707
	/**
708
	 * Internal update mechanism of Activities on User
709
	 * 
710
	 * @param User $model
711
	 * @param mixed $data
712
	 */
713
	protected function doUpdateActivities(User $model, $data) {
714
		// remove all relationships before
715
		ActivityQuery::create()->filterByActor($model)->delete();
716
717
		// add them
718
		$errors = [];
719
		foreach ($data as $entry) {
720
			if (!isset($entry['id'])) {
721
				$errors[] = 'Missing id for Activity';
722
			} else {
723
				$related = ActivityQuery::create()->findOneById($entry['id']);
724
				$model->addActivity($related);
725
			}
726
		}
727
728
		if (count($errors) > 0) {
729
			throw new ErrorsException($errors);
730
		}
731
	}
732
733
	/**
734
	 * Internal update mechanism of Groups on User
735
	 * 
736
	 * @param User $model
737
	 * @param mixed $data
738
	 */
739
	protected function doUpdateGroups(User $model, $data) {
740
		// remove all relationships before
741
		UserGroupQuery::create()->filterByUser($model)->delete();
742
743
		// add them
744
		$errors = [];
745
		foreach ($data as $entry) {
746
			if (!isset($entry['id'])) {
747
				$errors[] = 'Missing id for Group';
748
			} else {
749
				$related = GroupQuery::create()->findOneById($entry['id']);
750
				$model->addGroup($related);
751
			}
752
		}
753
754
		if (count($errors) > 0) {
755
			throw new ErrorsException($errors);
756
		}
757
	}
758
759
	/**
760
	 * Internal update mechanism of Sessions on User
761
	 * 
762
	 * @param User $model
763
	 * @param mixed $data
764
	 */
765
	protected function doUpdateSessions(User $model, $data) {
766
		// remove all relationships before
767
		SessionQuery::create()->filterByUser($model)->delete();
768
769
		// add them
770
		$errors = [];
771
		foreach ($data as $entry) {
772
			if (!isset($entry['id'])) {
773
				$errors[] = 'Missing id for Session';
774
			} else {
775
				$related = SessionQuery::create()->findOneById($entry['id']);
776
				$model->addSession($related);
777
			}
778
		}
779
780
		if (count($errors) > 0) {
781
			throw new ErrorsException($errors);
782
		}
783
	}
784
785
	/**
786
	 * Returns one User with the given id from cache
787
	 * 
788
	 * @param mixed $id
789
	 * @return User|null
790
	 */
791
	protected function get($id) {
792
		if ($this->pool === null) {
793
			$this->pool = new Map();
794
		} else if ($this->pool->has($id)) {
795
			return $this->pool->get($id);
796
		}
797
798
		$model = UserQuery::create()->findOneById($id);
799
		$this->pool->set($id, $model);
800
801
		return $model;
802
	}
803
804
	/**
805
	 * Returns the service container
806
	 * 
807
	 * @return ServiceContainer
808
	 */
809
	abstract protected function getServiceContainer();
810
}
811