Completed
Push — master ( 94e820...3f0fe0 )
by Thomas
10:39
created

ActionDomainTrait::updateApis()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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