Completed
Push — master ( 944dfb...9c93d5 )
by Thomas
10:01 queued 37s
created

LocalizationDomainTrait::setLanguageId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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