Completed
Push — master ( 04c3fa...9e32f0 )
by Thomas
09:51
created

UserDomainTrait::getServiceContainer()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\model\User;
5
use keeko\core\model\UserQuery;
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\UserEvent;
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\UserGroupQuery;
22
23
/**
24
 */
25
trait UserDomainTrait {
26
27
	/**
28
	 */
29
	protected $pool;
30
31
	/**
32
	 * Adds Group to User
33
	 * 
34
	 * @param mixed $id
35
	 * @param mixed $data
36
	 * @return PayloadInterface
37
	 */
38
	public function addGroup($id, $data) {
39
		// find
40
		$user = $this->get($id);
41
42
		if ($user === null) {
43
			return new NotFound(['message' => 'User 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
			$user->addGroup($group);
54
		}
55
56
		if (count($errors) > 0) {
57
			return new NotValid(['errors' => $errors]);
58
		}
59
60
		$event = new UserEvent($user);
61
		$dispatcher = $this->getServiceContainer()->getDispatcher();
62
		$dispatcher->dispatch(UserEvent::PRE_GROUP_ADD, $event);
63
		$dispatcher->dispatch(UserEvent::PRE_SAVE, $event);
64
		$rows = $user->save();
65
		$dispatcher->dispatch(UserEvent::POST_GROUP_ADD, $event);
66
		$dispatcher->dispatch(UserEvent::POST_SAVE, $event);
67
68
		if ($rows > 0) {
69
			return Updated(['model' => $user]);
70
		}
71
72
		return NotUpdated(['model' => $user]);
73
	}
74
75
	/**
76
	 * Creates a new User with the provided data
77
	 * 
78
	 * @param mixed $data
79
	 * @return PayloadInterface
80
	 */
81
	public function create($data) {
82
		// hydrate
83
		$serializer = User::getSerializer();
84
		$user = $serializer->hydrate(new User(), $data);
85
86
		// dispatch
87
		$event = new UserEvent($user);
88
		$dispatcher = $this->getServiceContainer()->getDispatcher();
89
		$dispatcher->dispatch(UserEvent::PRE_CREATE, $event);
90
		$dispatcher->dispatch(UserEvent::PRE_SAVE, $event);
91
		$user->save();
92
		$dispatcher->dispatch(UserEvent::POST_CREATE, $event);
93
		$dispatcher->dispatch(UserEvent::POST_SAVE, $event);
94
		return new Created(['model' => $user]);
95
	}
96
97
	/**
98
	 * Deletes a User with the given id
99
	 * 
100
	 * @param mixed $id
101
	 * @return PayloadInterface
102
	 */
103
	public function delete($id) {
104
		// find
105
		$user = $this->get($id);
106
107
		if ($user === null) {
108
			return new NotFound(['message' => 'User not found.']);
109
		}
110
111
		// delete
112
		$event = new UserEvent($user);
113
		$dispatcher = $this->getServiceContainer()->getDispatcher();
114
		$dispatcher->dispatch(UserEvent::PRE_DELETE, $event);
115
		$user->delete();
116
117
		if ($user->isDeleted()) {
118
			$dispatcher->dispatch(UserEvent::POST_DELETE, $event);
119
			return new Deleted(['model' => $user]);
120
		}
121
122
		return new NotDeleted(['message' => 'Could not delete User']);
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 = UserQuery::create();
138
139
		// sorting
140
		$sort = $params->getSort(User::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
		$user = $query->paginate($page, $size);
154
155
		// run response
156
		return new Found(['model' => $user]);
157
	}
158
159
	/**
160
	 * Returns one User with the given id
161
	 * 
162
	 * @param mixed $id
163
	 * @return PayloadInterface
164
	 */
165
	public function read($id) {
166
		// read
167
		$user = $this->get($id);
168
169
		// check existence
170
		if ($user === null) {
171
			return new NotFound(['message' => 'User not found.']);
172
		}
173
174
		return new Found(['model' => $user]);
175
	}
176
177
	/**
178
	 * Removes Group from User
179
	 * 
180
	 * @param mixed $id
181
	 * @param mixed $data
182
	 * @return PayloadInterface
183
	 */
184
	public function removeGroup($id, $data) {
185
		// find
186
		$user = $this->get($id);
187
188
		if ($user === null) {
189
			return new NotFound(['message' => 'User 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
			$user->removeGroup($group);
200
		}
201
202
		if (count($errors) > 0) {
203
			return new NotValid(['errors' => $errors]);
204
		}
205
206
		$event = new UserEvent($user);
207
		$dispatcher = $this->getServiceContainer()->getDispatcher();
208
		$dispatcher->dispatch(UserEvent::PRE_GROUP_REMOVE, $event);
209
		$dispatcher->dispatch(UserEvent::PRE_SAVE, $event);
210
		$rows = $user->save();
211
		$dispatcher->dispatch(UserEvent::POST_GROUP_REMOVE, $event);
212
		$dispatcher->dispatch(UserEvent::POST_SAVE, $event);
213
214
		if ($rows > 0) {
215
			return Updated(['model' => $user]);
216
		}
217
218
		return NotUpdated(['model' => $user]);
219
	}
220
221
	/**
222
	 * Updates a User with the given idand the provided data
223
	 * 
224
	 * @param mixed $id
225
	 * @param mixed $data
226
	 * @return PayloadInterface
227
	 */
228
	public function update($id, $data) {
229
		// find
230
		$user = $this->get($id);
231
232
		if ($user === null) {
233
			return new NotFound(['message' => 'User not found.']);
234
		}
235
236
		// hydrate
237
		$serializer = User::getSerializer();
238
		$user = $serializer->hydrate($user, $data);
239
240
		// dispatch
241
		$event = new UserEvent($user);
242
		$dispatcher = $this->getServiceContainer()->getDispatcher();
243
		$dispatcher->dispatch(UserEvent::PRE_UPDATE, $event);
244
		$dispatcher->dispatch(UserEvent::PRE_SAVE, $event);
245
		$rows = $user->save();
246
		$dispatcher->dispatch(UserEvent::POST_UPDATE, $event);
247
		$dispatcher->dispatch(UserEvent::POST_SAVE, $event);
248
249
		$payload = ['model' => $user];
250
251
		if ($rows === 0) {
252
			return new NotUpdated($payload);
253
		}
254
255
		return new Updated($payload);
256
	}
257
258
	/**
259
	 * Updates Group on User
260
	 * 
261
	 * @param mixed $id
262
	 * @param mixed $data
263
	 * @return PayloadInterface
264
	 */
265
	public function updateGroup($id, $data) {
266
		// find
267
		$user = $this->get($id);
268
269
		if ($user === null) {
270
			return new NotFound(['message' => 'User not found.']);
271
		}
272
273
		// remove all relationships before
274
		UserGroupQuery::create()->filterByUser($user)->delete();
275
276
		// add them
277
		$errors = [];
278
		foreach ($data as $entry) {
279
			if (!isset($entry['id'])) {
280
				$errors[] = 'Missing id for Group';
281
			}
282
			$group = GroupQuery::create()->findOneById($entry['id']);
283
			$user->addGroup($group);
284
		}
285
286
		if (count($errors) > 0) {
287
			return new NotValid(['errors' => $errors]);
288
		}
289
290
291
		$event = new UserEvent($user);
292
		$dispatcher = $this->getServiceContainer()->getDispatcher();
293
		$dispatcher->dispatch(UserEvent::PRE_GROUP_UPDATE, $event);
294
		$dispatcher->dispatch(UserEvent::PRE_SAVE, $event);
295
		$rows = $user->save();
296
		$dispatcher->dispatch(UserEvent::POST_GROUP_UPDATE, $event);
297
		$dispatcher->dispatch(UserEvent::POST_SAVE, $event);
298
299
		if ($rows > 0) {
300
			return Updated(['model' => $user]);
301
		}
302
303
		return NotUpdated(['model' => $user]);
304
	}
305
306
	/**
307
	 * Implement this functionality at keeko\core\domain\UserDomain
308
	 * 
309
	 * @param UserQuery $query
310
	 * @param mixed $filter
311
	 * @return void
312
	 */
313
	abstract protected function applyFilter(UserQuery $query, $filter);
314
315
	/**
316
	 * Returns one User with the given id from cache
317
	 * 
318
	 * @param mixed $id
319
	 * @return User|null
320
	 */
321
	protected function get($id) {
322
		if ($this->pool === null) {
323
			$this->pool = new Map();
324
		} else if ($this->pool->has($id)) {
325
			return $this->pool->get($id);
326
		}
327
328
		$user = UserQuery::create()->findOneById($id);
329
		$this->pool->set($id, $user);
330
331
		return $user;
332
	}
333
334
	/**
335
	 * Returns the service container
336
	 * 
337
	 * @return ServiceContainer
338
	 */
339
	abstract protected function getServiceContainer();
340
}
341