Completed
Push — master ( abae3b...04c3fa )
by Thomas
05:59
created

ActionDomainTrait::setModuleId()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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