Completed
Push — master ( b30b3b...5895ce )
by Thomas
09:27
created

ActionDomainTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 139
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 2
A delete() 0 18 3
B paginate() 0 27 3
A read() 0 14 2
B update() 0 28 4
applyFilter() 0 1 ?
getServiceContainer() 0 1 ?
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\Found;
8
use keeko\framework\domain\payload\NotFound;
9
use Tobscure\JsonApi\Parameters;
10
use keeko\framework\utils\NameUtils;
11
use keeko\framework\domain\payload\Created;
12
use keeko\framework\domain\payload\Updated;
13
use keeko\framework\domain\payload\NotUpdated;
14
use keeko\framework\domain\payload\NotValid;
15
use keeko\framework\domain\payload\Deleted;
16
use keeko\framework\domain\payload\NotDeleted;
17
18
/**
19
 */
20
trait ActionDomainTrait {
21
22
	/**
23
	 * @param mixed $data
24
	 */
25
	public function create($data) {
26
		// hydrate
27
		$serializer = Action::getSerializer();
28
		$action = $serializer->hydrate(new Action(), $data);
29
30
		// validate
31
		if (!$action->validate()) {
32
			return new NotValid([
33
				'errors' => $action->getValidationFailures()
34
			]);
35
		}
36
37
		$action->save();
38
		return new Created(['model' => $action]);
39
	}
40
41
	/**
42
	 * @param mixed $id
43
	 */
44
	public function delete($id) {
45
		// find
46
		$action = ActionQuery::create()->findOneById($id);
47
48
		if ($action === null) {
49
			return new NotFound(['message' => 'Action not found.']);
50
		}
51
52
		// delete
53
		$action->delete();
54
		$payload = ['model' => $action];
55
56
		if ($action->isDeleted()) {
57
			return new Deleted($payload);
58
		}
59
60
		return new NotDeleted($payload);
61
	}
62
63
	/**
64
	 * @param Parameters $params
65
	 */
66
	public function paginate(Parameters $params) {
67
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
68
		$defaultSize = $sysPrefs->getPaginationSize();
69
		$page = $params->getPage('number');
0 ignored issues
show
Bug introduced by
The method getPage() cannot be called from this context as it is declared protected in class Tobscure\JsonApi\Parameters.

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.

Loading history...
70
		$size = $params->getPage('size', $defaultSize);
0 ignored issues
show
Bug introduced by
The method getPage() cannot be called from this context as it is declared protected in class Tobscure\JsonApi\Parameters.

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.

Loading history...
Unused Code introduced by
The call to Parameters::getPage() has too many arguments starting with $defaultSize.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
72
		$query = ActionQuery::create();
73
74
		// sorting
75
		$sort = $params->getSort(Action::getSerializer()->getSortFields());
76
		foreach ($sort as $field => $order) {
77
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
78
			$query->$method($order);
79
		}
80
81
		// filtering
82
		$filter = $params->getFilter();
83
		if (!empty($filter)) {
84
			$this->applyFilter($query, $filter);
0 ignored issues
show
Unused Code introduced by
The call to ActionDomainTrait::applyFilter() has too many arguments starting with $filter.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
85
		}
86
87
		// paginate
88
		$action = $query->paginate($page, $size);
89
90
		// run response
91
		return new Found(['model' => $action]);
92
	}
93
94
	/**
95
	 * @param mixed $id
96
	 */
97
	public function read($id) {
98
		// read
99
		$action = ActionQuery::create()->findOneById($id);
100
101
		// check existence
102
		if ($action === null) {
103
			$payload = new NotFound(['message' => 'Action not found.']);
104
		} else {
105
			$payload = new Found(['model' => $action]);
106
		}
107
108
		// run response
109
		return $payload;
110
	}
111
112
	/**
113
	 * @param mixed $id
114
	 * @param mixed $data
115
	 */
116
	public function update($id, $data) {
117
		// find
118
		$action = ActionQuery::create()->findOneById($id);
119
120
		if ($action === null) {
121
			return new NotFound(['message' => 'Action not found.']);
122
		}
123
124
		// hydrate
125
		$serializer = Action::getSerializer();
126
		$action = $serializer->hydrate($action, $data);
127
128
		// validate
129
		if (!$action->validate()) {
130
			return new NotValid([
131
				'errors' => $action->getValidationFailures()
132
			]);
133
		}
134
135
		$rows = $action->save();
136
		$payload = ['model' => $action];
137
138
		if ($rows === 0) {
139
			return new NotUpdated($payload);
140
		}
141
142
		return new Updated($payload);
143
	}
144
145
	/**
146
	 * Implement this functionality at keeko\core\domain\ActionDomain
147
	 * 
148
	 * @param ActionQuery $query
149
	 */
150
	abstract protected function applyFilter(ActionQuery $query);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
151
152
	/**
153
	 * Returns the service container
154
	 * 
155
	 * @return ServiceContainer
156
	 */
157
	abstract protected function getServiceContainer();
158
}
159