Completed
Push — master ( 061e94...8714ce )
by Thomas
06:16
created

LocalizationDomainTrait::removeLanguageVariant()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
394
		} else if ($this->pool->has($id)) {
395
			return $this->pool->get($id);
396
		}
397
398
		$localization = LocalizationQuery::create()->findOneById($id);
399
		$this->pool->set($id, $localization);
400
401
		return $localization;
402
	}
403
404
	/**
405
	 * Returns the service container
406
	 * 
407
	 * @return ServiceContainer
408
	 */
409
	abstract protected function getServiceContainer();
410
}
411