Completed
Push — master ( e8b3d2...889b57 )
by Thomas
14:29
created

ApplicationUriDomainTrait::setApplicationId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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