Completed
Push — master ( 061e94...8714ce )
by Thomas
06:16
created

ActionDomainTrait::removeGroup()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.439
cc 6
eloc 16
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 Tobscure\JsonApi\Parameters;
12
use keeko\framework\utils\NameUtils;
13
use keeko\framework\domain\payload\Created;
14
use keeko\framework\domain\payload\Updated;
15
use keeko\framework\domain\payload\NotUpdated;
16
use keeko\framework\domain\payload\NotValid;
17
use keeko\framework\domain\payload\Deleted;
18
use keeko\framework\domain\payload\NotDeleted;
19
use keeko\core\model\GroupQuery;
20
use keeko\core\model\GroupActionQuery;
21
22
/**
23
 */
24
trait ActionDomainTrait {
25
26
	/**
27
	 * Adds Group to Action
28
	 * 
29
	 * @param mixed $id
30
	 * @param mixed $data
31
	 * @return PayloadInterface
32
	 */
33
	public function addGroup($id, $data) {
34
		// find
35
		$action = $this->get($id);
36
37
		if ($action === null) {
38
			return new NotFound(['message' => 'Action not found.']);
39
		}
40
		 
41
		// update
42
		$errors = [];
43
		foreach ($data as $entry) {
44
			if (!isset($entry['id'])) {
45
				$errors[] = 'Missing id for Group';
46
			}
47
			$group = GroupQuery::create()->findOneById($entry['id']);
48
			$action->addGroup($group);
49
		}
50
51
		if (count($errors) > 0) {
52
			return new NotValid(['errors' => $errors]);
53
		}
54
55
		$rows = $action->save();
56
57
		if ($rows > 0) {
58
			return Updated(['model' => $action]);
59
		}
60
61
		return NotUpdated(['model' => $action]);
62
	}
63
64
	/**
65
	 * Creates a new Action with the provided data
66
	 * 
67
	 * @param mixed $data
68
	 * @return PayloadInterface
69
	 */
70
	public function create($data) {
71
		// hydrate
72
		$serializer = Action::getSerializer();
73
		$action = $serializer->hydrate(new Action(), $data);
74
75
		// validate
76
		if (!$action->validate()) {
77
			return new NotValid([
78
				'errors' => $action->getValidationFailures()
79
			]);
80
		}
81
82
		$action->save();
83
		return new Created(['model' => $action]);
84
	}
85
86
	/**
87
	 * Deletes a Action with the given id
88
	 * 
89
	 * @param mixed $id
90
	 * @return PayloadInterface
91
	 */
92
	public function delete($id) {
93
		// find
94
		$action = $this->get($id);
95
96
		if ($action === null) {
97
			return new NotFound(['message' => 'Action not found.']);
98
		}
99
100
		// delete
101
		$action->delete();
102
103
		if ($action->isDeleted()) {
104
			return new Deleted(['model' => $action]);
105
		}
106
107
		return new NotDeleted(['message' => 'Could not delete Action']);
108
	}
109
110
	/**
111
	 * Returns a paginated result
112
	 * 
113
	 * @param Parameters $params
114
	 * @return PayloadInterface
115
	 */
116
	public function paginate(Parameters $params) {
117
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
118
		$defaultSize = $sysPrefs->getPaginationSize();
119
		$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...
120
		$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...
121
122
		$query = ActionQuery::create();
123
124
		// sorting
125
		$sort = $params->getSort(Action::getSerializer()->getSortFields());
126
		foreach ($sort as $field => $order) {
127
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
128
			$query->$method($order);
129
		}
130
131
		// filtering
132
		$filter = $params->getFilter();
133
		if (!empty($filter)) {
134
			$this->applyFilter($query, $filter);
135
		}
136
137
		// paginate
138
		$action = $query->paginate($page, $size);
139
140
		// run response
141
		return new Found(['model' => $action]);
142
	}
143
144
	/**
145
	 * Returns one Action with the given id
146
	 * 
147
	 * @param mixed $id
148
	 * @return PayloadInterface
149
	 */
150
	public function read($id) {
151
		// read
152
		$action = $this->get($id);
153
154
		// check existence
155
		if ($action === null) {
156
			return new NotFound(['message' => 'Action not found.']);
157
		}
158
159
		return new Found(['model' => $action]);
160
	}
161
162
	/**
163
	 * Removes Group from Action
164
	 * 
165
	 * @param mixed $id
166
	 * @param mixed $data
167
	 * @return PayloadInterface
168
	 */
169
	public function removeGroup($id, $data) {
170
		// find
171
		$action = $this->get($id);
172
173
		if ($action === null) {
174
			return new NotFound(['message' => 'Action not found.']);
175
		}
176
177
		// remove them
178
		$errors = [];
179
		foreach ($data as $entry) {
180
			if (!isset($entry['id'])) {
181
				$errors[] = 'Missing id for Group';
182
			}
183
			$group = GroupQuery::create()->findOneById($entry['id']);
184
			$action->removeGroup($group);
185
		}
186
187
		if (count($errors) > 0) {
188
			return new NotValid(['errors' => $errors]);
189
		}
190
191
		$rows = $action->save();
192
193
		if ($rows > 0) {
194
			return Updated(['model' => $action]);
195
		}
196
197
		return NotUpdated(['model' => $action]);
198
	}
199
200
	/**
201
	 * Sets the Module id
202
	 * 
203
	 * @param mixed $id
204
	 * @param mixed $moduleId
205
	 * @return PayloadInterface
206
	 */
207
	public function setModuleId($id, $moduleId) {
208
		// find
209
		$action = $this->get($id);
210
211
		if ($action === null) {
212
			return new NotFound(['message' => 'Action not found.']);
213
		}
214
215
		// update
216
		if ($action->getModuleId() !== $moduleId) {
217
			$action->setModuleId($moduleId);
218
			$action->save();
219
			return Updated(['model' => $action]);
220
		}
221
222
		return NotUpdated(['model' => $action]);
223
	}
224
225
	/**
226
	 * Updates a Action with the given idand the provided data
227
	 * 
228
	 * @param mixed $id
229
	 * @param mixed $data
230
	 * @return PayloadInterface
231
	 */
232
	public function update($id, $data) {
233
		// find
234
		$action = $this->get($id);
235
236
		if ($action === null) {
237
			return new NotFound(['message' => 'Action not found.']);
238
		}
239
240
		// hydrate
241
		$serializer = Action::getSerializer();
242
		$action = $serializer->hydrate($action, $data);
243
244
		// validate
245
		if (!$action->validate()) {
246
			return new NotValid([
247
				'errors' => $action->getValidationFailures()
248
			]);
249
		}
250
251
		$rows = $action->save();
252
		$payload = ['model' => $action];
253
254
		if ($rows === 0) {
255
			return new NotUpdated($payload);
256
		}
257
258
		return new Updated($payload);
259
	}
260
261
	/**
262
	 * Updates Group on Action
263
	 * 
264
	 * @param mixed $id
265
	 * @param mixed $data
266
	 * @return PayloadInterface
267
	 */
268
	public function updateGroup($id, $data) {
269
		// find
270
		$action = $this->get($id);
271
272
		if ($action === null) {
273
			return new NotFound(['message' => 'Action not found.']);
274
		}
275
276
		// remove all relationships before
277
		GroupActionQuery::create()->filterByAction($action)->delete();
278
279
		// add them
280
		$errors = [];
281
		foreach ($data as $entry) {
282
			if (!isset($entry['id'])) {
283
				$errors[] = 'Missing id for Group';
284
			}
285
			$group = GroupQuery::create()->findOneById($entry['id']);
286
			$action->addGroup($group);
287
		}
288
289
		if (count($errors) > 0) {
290
			return new NotValid(['errors' => $errors]);
291
		}
292
293
		$rows = $action->save();
294
295
		if ($rows > 0) {
296
			return Updated(['model' => $action]);
297
		}
298
299
		return NotUpdated(['model' => $action]);
300
	}
301
302
	/**
303
	 * Implement this functionality at keeko\core\domain\ActionDomain
304
	 * 
305
	 * @param ActionQuery $query
306
	 * @param mixed $filter
307
	 */
308
	abstract protected function applyFilter(ActionQuery $query, $filter);
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...
309
310
	/**
311
	 * Returns one Action with the given id from cache
312
	 * 
313
	 * @param mixed $id
314
	 * @return Action|null
315
	 */
316
	protected function get($id) {
317
		if ($this->pool === null) {
318
			$this->pool = new Map();
0 ignored issues
show
Bug introduced by
The property pool does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
319
		} else if ($this->pool->has($id)) {
320
			return $this->pool->get($id);
321
		}
322
323
		$action = ActionQuery::create()->findOneById($id);
324
		$this->pool->set($id, $action);
325
326
		return $action;
327
	}
328
329
	/**
330
	 * Returns the service container
331
	 * 
332
	 * @return ServiceContainer
333
	 */
334
	abstract protected function getServiceContainer();
335
}
336