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

ModuleDomainTrait::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\model\Module;
5
use keeko\core\model\ModuleQuery;
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\ModuleEvent;
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\ActionQuery;
21
22
/**
23
 */
24
trait ModuleDomainTrait {
25
26
	/**
27
	 */
28
	protected $pool;
29
30
	/**
31
	 * Adds Actions to Module
32
	 * 
33
	 * @param mixed $id
34
	 * @param mixed $data
35
	 * @return PayloadInterface
36
	 */
37
	public function addActions($id, $data) {
38
		// find
39
		$module = $this->get($id);
40
41
		if ($module === null) {
42
			return new NotFound(['message' => 'Module not found.']);
43
		}
44
		 
45
		// update
46
		$errors = [];
47
		foreach ($data as $entry) {
48
			if (!isset($entry['id'])) {
49
				$errors[] = 'Missing id for Action';
50
			}
51
			$action = ActionQuery::create()->findOneById($entry['id']);
52
			$module->addAction($action);
53
		}
54
55
		if (count($errors) > 0) {
56
			return new NotValid(['errors' => $errors]);
57
		}
58
59
		// save and dispatch events
60
		$event = new ModuleEvent($module);
61
		$dispatcher = $this->getServiceContainer()->getDispatcher();
62
		$dispatcher->dispatch(ModuleEvent::PRE_ACTIONS_ADD, $event);
63
		$dispatcher->dispatch(ModuleEvent::PRE_SAVE, $event);
64
		$rows = $module->save();
65
		$dispatcher->dispatch(ModuleEvent::POST_ACTIONS_ADD, $event);
66
		$dispatcher->dispatch(ModuleEvent::POST_SAVE, $event);
67
68
		if ($rows > 0) {
69
			return Updated(['model' => $module]);
70
		}
71
72
		return NotUpdated(['model' => $module]);
73
	}
74
75
	/**
76
	 * Creates a new Module with the provided data
77
	 * 
78
	 * @param mixed $data
79
	 * @return PayloadInterface
80
	 */
81
	public function create($data) {
82
		// hydrate
83
		$serializer = Module::getSerializer();
84
		$module = $serializer->hydrate(new Module(), $data);
85
86
		// validate
87
		$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...
88
		if ($validator !== null && !$validator->validate($module)) {
89
			return new NotValid([
90
				'errors' => $validator->getValidationFailures()
91
			]);
92
		}
93
94
		// dispatch
95
		$event = new ModuleEvent($module);
96
		$dispatcher = $this->getServiceContainer()->getDispatcher();
97
		$dispatcher->dispatch(ModuleEvent::PRE_CREATE, $event);
98
		$dispatcher->dispatch(ModuleEvent::PRE_SAVE, $event);
99
		$module->save();
100
		$dispatcher->dispatch(ModuleEvent::POST_CREATE, $event);
101
		$dispatcher->dispatch(ModuleEvent::POST_SAVE, $event);
102
		return new Created(['model' => $module]);
103
	}
104
105
	/**
106
	 * Deletes a Module with the given id
107
	 * 
108
	 * @param mixed $id
109
	 * @return PayloadInterface
110
	 */
111
	public function delete($id) {
112
		// find
113
		$module = $this->get($id);
114
115
		if ($module === null) {
116
			return new NotFound(['message' => 'Module not found.']);
117
		}
118
119
		// delete
120
		$event = new ModuleEvent($module);
121
		$dispatcher = $this->getServiceContainer()->getDispatcher();
122
		$dispatcher->dispatch(ModuleEvent::PRE_DELETE, $event);
123
		$module->delete();
124
125
		if ($module->isDeleted()) {
126
			$dispatcher->dispatch(ModuleEvent::POST_DELETE, $event);
127
			return new Deleted(['model' => $module]);
128
		}
129
130
		return new NotDeleted(['message' => 'Could not delete Module']);
131
	}
132
133
	/**
134
	 * Returns a paginated result
135
	 * 
136
	 * @param Parameters $params
137
	 * @return PayloadInterface
138
	 */
139
	public function paginate(Parameters $params) {
140
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
141
		$defaultSize = $sysPrefs->getPaginationSize();
142
		$page = $params->getPage('number');
143
		$size = $params->getPage('size', $defaultSize);
144
145
		$query = ModuleQuery::create();
146
147
		// sorting
148
		$sort = $params->getSort(Module::getSerializer()->getSortFields());
149
		foreach ($sort as $field => $order) {
150
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
151
			$query->$method($order);
152
		}
153
154
		// filtering
155
		$filter = $params->getFilter();
156
		if (!empty($filter)) {
157
			$this->applyFilter($query, $filter);
158
		}
159
160
		// paginate
161
		$module = $query->paginate($page, $size);
162
163
		// run response
164
		return new Found(['model' => $module]);
165
	}
166
167
	/**
168
	 * Returns one Module with the given id
169
	 * 
170
	 * @param mixed $id
171
	 * @return PayloadInterface
172
	 */
173
	public function read($id) {
174
		// read
175
		$module = $this->get($id);
176
177
		// check existence
178
		if ($module === null) {
179
			return new NotFound(['message' => 'Module not found.']);
180
		}
181
182
		return new Found(['model' => $module]);
183
	}
184
185
	/**
186
	 * Removes Actions from Module
187
	 * 
188
	 * @param mixed $id
189
	 * @param mixed $data
190
	 * @return PayloadInterface
191
	 */
192
	public function removeActions($id, $data) {
193
		// find
194
		$module = $this->get($id);
195
196
		if ($module === null) {
197
			return new NotFound(['message' => 'Module not found.']);
198
		}
199
200
		// remove them
201
		$errors = [];
202
		foreach ($data as $entry) {
203
			if (!isset($entry['id'])) {
204
				$errors[] = 'Missing id for Action';
205
			}
206
			$action = ActionQuery::create()->findOneById($entry['id']);
207
			$module->removeAction($action);
208
		}
209
210
		if (count($errors) > 0) {
211
			return new NotValid(['errors' => $errors]);
212
		}
213
214
		// save and dispatch events
215
		$event = new ModuleEvent($module);
216
		$dispatcher = $this->getServiceContainer()->getDispatcher();
217
		$dispatcher->dispatch(ModuleEvent::PRE_ACTIONS_REMOVE, $event);
218
		$dispatcher->dispatch(ModuleEvent::PRE_SAVE, $event);
219
		$rows = $module->save();
220
		$dispatcher->dispatch(ModuleEvent::POST_ACTIONS_REMOVE, $event);
221
		$dispatcher->dispatch(ModuleEvent::POST_SAVE, $event);
222
223
		if ($rows > 0) {
224
			return Updated(['model' => $module]);
225
		}
226
227
		return NotUpdated(['model' => $module]);
228
	}
229
230
	/**
231
	 * Updates a Module with the given idand the provided data
232
	 * 
233
	 * @param mixed $id
234
	 * @param mixed $data
235
	 * @return PayloadInterface
236
	 */
237
	public function update($id, $data) {
238
		// find
239
		$module = $this->get($id);
240
241
		if ($module === null) {
242
			return new NotFound(['message' => 'Module not found.']);
243
		}
244
245
		// hydrate
246
		$serializer = Module::getSerializer();
247
		$module = $serializer->hydrate($module, $data);
248
249
		// validate
250
		$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...
251
		if ($validator !== null && !$validator->validate($module)) {
252
			return new NotValid([
253
				'errors' => $validator->getValidationFailures()
254
			]);
255
		}
256
257
		// dispatch
258
		$event = new ModuleEvent($module);
259
		$dispatcher = $this->getServiceContainer()->getDispatcher();
260
		$dispatcher->dispatch(ModuleEvent::PRE_UPDATE, $event);
261
		$dispatcher->dispatch(ModuleEvent::PRE_SAVE, $event);
262
		$rows = $module->save();
263
		$dispatcher->dispatch(ModuleEvent::POST_UPDATE, $event);
264
		$dispatcher->dispatch(ModuleEvent::POST_SAVE, $event);
265
266
		$payload = ['model' => $module];
267
268
		if ($rows === 0) {
269
			return new NotUpdated($payload);
270
		}
271
272
		return new Updated($payload);
273
	}
274
275
	/**
276
	 * Updates Actions on Module
277
	 * 
278
	 * @param mixed $id
279
	 * @param mixed $data
280
	 * @return PayloadInterface
281
	 */
282
	public function updateActions($id, $data) {
283
		// find
284
		$module = $this->get($id);
285
286
		if ($module === null) {
287
			return new NotFound(['message' => 'Module not found.']);
288
		}
289
290
		// remove all relationships before
291
		ActionQuery::create()->filterByModule($module)->delete();
292
293
		// add them
294
		$errors = [];
295
		foreach ($data as $entry) {
296
			if (!isset($entry['id'])) {
297
				$errors[] = 'Missing id for Action';
298
			}
299
			$action = ActionQuery::create()->findOneById($entry['id']);
300
			$module->addAction($action);
301
		}
302
303
		if (count($errors) > 0) {
304
			return new NotValid(['errors' => $errors]);
305
		}
306
307
		// save and dispatch events
308
		$event = new ModuleEvent($module);
309
		$dispatcher = $this->getServiceContainer()->getDispatcher();
310
		$dispatcher->dispatch(ModuleEvent::PRE_ACTIONS_UPDATE, $event);
311
		$dispatcher->dispatch(ModuleEvent::PRE_SAVE, $event);
312
		$rows = $module->save();
313
		$dispatcher->dispatch(ModuleEvent::POST_ACTIONS_UPDATE, $event);
314
		$dispatcher->dispatch(ModuleEvent::POST_SAVE, $event);
315
316
		if ($rows > 0) {
317
			return Updated(['model' => $module]);
318
		}
319
320
		return NotUpdated(['model' => $module]);
321
	}
322
323
	/**
324
	 * Implement this functionality at keeko\core\domain\ModuleDomain
325
	 * 
326
	 * @param ModuleQuery $query
327
	 * @param mixed $filter
328
	 * @return void
329
	 */
330
	abstract protected function applyFilter(ModuleQuery $query, $filter);
331
332
	/**
333
	 * Returns one Module with the given id from cache
334
	 * 
335
	 * @param mixed $id
336
	 * @return Module|null
337
	 */
338
	protected function get($id) {
339
		if ($this->pool === null) {
340
			$this->pool = new Map();
341
		} else if ($this->pool->has($id)) {
342
			return $this->pool->get($id);
343
		}
344
345
		$module = ModuleQuery::create()->findOneById($id);
346
		$this->pool->set($id, $module);
347
348
		return $module;
349
	}
350
351
	/**
352
	 * Returns the service container
353
	 * 
354
	 * @return ServiceContainer
355
	 */
356
	abstract protected function getServiceContainer();
357
}
358