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

LocalizationDomainTrait::setExtLangId()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
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\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 keeko\framework\utils\Parameters;
12
use keeko\framework\utils\NameUtils;
13
use keeko\core\event\LocalizationEvent;
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\LanguageVariantQuery;
21
use keeko\core\model\LocalizationVariantQuery;
22
23
/**
24
 */
25
trait LocalizationDomainTrait {
26
27
	/**
28
	 */
29
	protected $pool;
30
31
	/**
32
	 * Adds LanguageVariant to Localization
33
	 * 
34
	 * @param mixed $id
35
	 * @param mixed $data
36
	 * @return PayloadInterface
37
	 */
38
	public function addLanguageVariant($id, $data) {
39
		// find
40
		$localization = $this->get($id);
41
42
		if ($localization === null) {
43
			return new NotFound(['message' => 'Localization not found.']);
44
		}
45
		 
46
		// update
47
		$errors = [];
48
		foreach ($data as $entry) {
49
			if (!isset($entry['id'])) {
50
				$errors[] = 'Missing id for LanguageVariant';
51
			}
52
			$languageVariant = LanguageVariantQuery::create()->findOneById($entry['id']);
53
			$localization->addLanguageVariant($languageVariant);
54
		}
55
56
		if (count($errors) > 0) {
57
			return new NotValid(['errors' => $errors]);
58
		}
59
60
		$event = new LocalizationEvent($localization);
61
		$dispatcher = $this->getServiceContainer()->getDispatcher();
62
		$dispatcher->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANT_ADD, $event);
63
		$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
64
		$rows = $localization->save();
65
		$dispatcher->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANT_ADD, $event);
66
		$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
67
68
		if ($rows > 0) {
69
			return Updated(['model' => $localization]);
70
		}
71
72
		return NotUpdated(['model' => $localization]);
73
	}
74
75
	/**
76
	 * Creates a new Localization with the provided data
77
	 * 
78
	 * @param mixed $data
79
	 * @return PayloadInterface
80
	 */
81
	public function create($data) {
82
		// hydrate
83
		$serializer = Localization::getSerializer();
84
		$localization = $serializer->hydrate(new Localization(), $data);
85
86
		// dispatch
87
		$event = new LocalizationEvent($localization);
88
		$dispatcher = $this->getServiceContainer()->getDispatcher();
89
		$dispatcher->dispatch(LocalizationEvent::PRE_CREATE, $event);
90
		$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
91
		$localization->save();
92
		$dispatcher->dispatch(LocalizationEvent::POST_CREATE, $event);
93
		$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
94
		return new Created(['model' => $localization]);
95
	}
96
97
	/**
98
	 * Deletes a Localization with the given id
99
	 * 
100
	 * @param mixed $id
101
	 * @return PayloadInterface
102
	 */
103
	public function delete($id) {
104
		// find
105
		$localization = $this->get($id);
106
107
		if ($localization === null) {
108
			return new NotFound(['message' => 'Localization not found.']);
109
		}
110
111
		// delete
112
		$event = new LocalizationEvent($localization);
113
		$dispatcher = $this->getServiceContainer()->getDispatcher();
114
		$dispatcher->dispatch(LocalizationEvent::PRE_DELETE, $event);
115
		$localization->delete();
116
117
		if ($localization->isDeleted()) {
118
			$dispatcher->dispatch(LocalizationEvent::POST_DELETE, $event);
119
			return new Deleted(['model' => $localization]);
120
		}
121
122
		return new NotDeleted(['message' => 'Could not delete Localization']);
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 = LocalizationQuery::create();
138
139
		// sorting
140
		$sort = $params->getSort(Localization::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
		$localization = $query->paginate($page, $size);
154
155
		// run response
156
		return new Found(['model' => $localization]);
157
	}
158
159
	/**
160
	 * Returns one Localization with the given id
161
	 * 
162
	 * @param mixed $id
163
	 * @return PayloadInterface
164
	 */
165
	public function read($id) {
166
		// read
167
		$localization = $this->get($id);
168
169
		// check existence
170
		if ($localization === null) {
171
			return new NotFound(['message' => 'Localization not found.']);
172
		}
173
174
		return new Found(['model' => $localization]);
175
	}
176
177
	/**
178
	 * Removes LanguageVariant from Localization
179
	 * 
180
	 * @param mixed $id
181
	 * @param mixed $data
182
	 * @return PayloadInterface
183
	 */
184
	public function removeLanguageVariant($id, $data) {
185
		// find
186
		$localization = $this->get($id);
187
188
		if ($localization === null) {
189
			return new NotFound(['message' => 'Localization not found.']);
190
		}
191
192
		// remove them
193
		$errors = [];
194
		foreach ($data as $entry) {
195
			if (!isset($entry['id'])) {
196
				$errors[] = 'Missing id for LanguageVariant';
197
			}
198
			$languageVariant = LanguageVariantQuery::create()->findOneById($entry['id']);
199
			$localization->removeLanguageVariant($languageVariant);
200
		}
201
202
		if (count($errors) > 0) {
203
			return new NotValid(['errors' => $errors]);
204
		}
205
206
		$event = new LocalizationEvent($localization);
207
		$dispatcher = $this->getServiceContainer()->getDispatcher();
208
		$dispatcher->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANT_REMOVE, $event);
209
		$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
210
		$rows = $localization->save();
211
		$dispatcher->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANT_REMOVE, $event);
212
		$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
213
214
		if ($rows > 0) {
215
			return Updated(['model' => $localization]);
216
		}
217
218
		return NotUpdated(['model' => $localization]);
219
	}
220
221
	/**
222
	 * Sets the Language id
223
	 * 
224
	 * @param mixed $id
225
	 * @param mixed $extLangId
226
	 * @return PayloadInterface
227
	 */
228
	public function setExtLangId($id, $extLangId) {
229
		// find
230
		$localization = $this->get($id);
231
232
		if ($localization === null) {
233
			return new NotFound(['message' => 'Localization not found.']);
234
		}
235
236
		// update
237
		if ($localization->getExtLanguageId() !== $extLangId) {
238
			$localization->setExtLanguageId($extLangId);
239
240
			$event = new LocalizationEvent($localization);
241
			$dispatcher = $this->getServiceContainer()->getDispatcher();
242
			$dispatcher->dispatch(LocalizationEvent::PRE_EXT_LANG_UPDATE, $event);
243
			$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
244
			$localization->save();
245
			$dispatcher->dispatch(LocalizationEvent::POST_EXT_LANG_UPDATE, $event);
246
			$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
247
			
248
			return Updated(['model' => $localization]);
249
		}
250
251
		return NotUpdated(['model' => $localization]);
252
	}
253
254
	/**
255
	 * Sets the Localization id
256
	 * 
257
	 * @param mixed $id
258
	 * @param mixed $parentId
259
	 * @return PayloadInterface
260
	 */
261
	public function setParentId($id, $parentId) {
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->getParentId() !== $parentId) {
271
			$localization->setParentId($parentId);
272
273
			$event = new LocalizationEvent($localization);
274
			$dispatcher = $this->getServiceContainer()->getDispatcher();
275
			$dispatcher->dispatch(LocalizationEvent::PRE_PARENT_UPDATE, $event);
276
			$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
277
			$localization->save();
278
			$dispatcher->dispatch(LocalizationEvent::POST_PARENT_UPDATE, $event);
279
			$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
280
			
281
			return Updated(['model' => $localization]);
282
		}
283
284
		return NotUpdated(['model' => $localization]);
285
	}
286
287
	/**
288
	 * Sets the LanguageScript id
289
	 * 
290
	 * @param mixed $id
291
	 * @param mixed $scriptId
292
	 * @return PayloadInterface
293
	 */
294
	public function setScriptId($id, $scriptId) {
295
		// find
296
		$localization = $this->get($id);
297
298
		if ($localization === null) {
299
			return new NotFound(['message' => 'Localization not found.']);
300
		}
301
302
		// update
303
		if ($localization->getScriptId() !== $scriptId) {
304
			$localization->setScriptId($scriptId);
305
306
			$event = new LocalizationEvent($localization);
307
			$dispatcher = $this->getServiceContainer()->getDispatcher();
308
			$dispatcher->dispatch(LocalizationEvent::PRE_SCRIPT_UPDATE, $event);
309
			$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
310
			$localization->save();
311
			$dispatcher->dispatch(LocalizationEvent::POST_SCRIPT_UPDATE, $event);
312
			$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
313
			
314
			return Updated(['model' => $localization]);
315
		}
316
317
		return NotUpdated(['model' => $localization]);
318
	}
319
320
	/**
321
	 * Updates a Localization with the given idand the provided data
322
	 * 
323
	 * @param mixed $id
324
	 * @param mixed $data
325
	 * @return PayloadInterface
326
	 */
327
	public function update($id, $data) {
328
		// find
329
		$localization = $this->get($id);
330
331
		if ($localization === null) {
332
			return new NotFound(['message' => 'Localization not found.']);
333
		}
334
335
		// hydrate
336
		$serializer = Localization::getSerializer();
337
		$localization = $serializer->hydrate($localization, $data);
338
339
		// dispatch
340
		$event = new LocalizationEvent($localization);
341
		$dispatcher = $this->getServiceContainer()->getDispatcher();
342
		$dispatcher->dispatch(LocalizationEvent::PRE_UPDATE, $event);
343
		$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
344
		$rows = $localization->save();
345
		$dispatcher->dispatch(LocalizationEvent::POST_UPDATE, $event);
346
		$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
347
348
		$payload = ['model' => $localization];
349
350
		if ($rows === 0) {
351
			return new NotUpdated($payload);
352
		}
353
354
		return new Updated($payload);
355
	}
356
357
	/**
358
	 * Updates LanguageVariant on Localization
359
	 * 
360
	 * @param mixed $id
361
	 * @param mixed $data
362
	 * @return PayloadInterface
363
	 */
364
	public function updateLanguageVariant($id, $data) {
365
		// find
366
		$localization = $this->get($id);
367
368
		if ($localization === null) {
369
			return new NotFound(['message' => 'Localization not found.']);
370
		}
371
372
		// remove all relationships before
373
		LocalizationVariantQuery::create()->filterByLocalization($localization)->delete();
374
375
		// add them
376
		$errors = [];
377
		foreach ($data as $entry) {
378
			if (!isset($entry['id'])) {
379
				$errors[] = 'Missing id for LanguageVariant';
380
			}
381
			$languageVariant = LanguageVariantQuery::create()->findOneById($entry['id']);
382
			$localization->addLanguageVariant($languageVariant);
383
		}
384
385
		if (count($errors) > 0) {
386
			return new NotValid(['errors' => $errors]);
387
		}
388
389
390
		$event = new LocalizationEvent($localization);
391
		$dispatcher = $this->getServiceContainer()->getDispatcher();
392
		$dispatcher->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANT_UPDATE, $event);
393
		$dispatcher->dispatch(LocalizationEvent::PRE_SAVE, $event);
394
		$rows = $localization->save();
395
		$dispatcher->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANT_UPDATE, $event);
396
		$dispatcher->dispatch(LocalizationEvent::POST_SAVE, $event);
397
398
		if ($rows > 0) {
399
			return Updated(['model' => $localization]);
400
		}
401
402
		return NotUpdated(['model' => $localization]);
403
	}
404
405
	/**
406
	 * Implement this functionality at keeko\core\domain\LocalizationDomain
407
	 * 
408
	 * @param LocalizationQuery $query
409
	 * @param mixed $filter
410
	 * @return void
411
	 */
412
	abstract protected function applyFilter(LocalizationQuery $query, $filter);
413
414
	/**
415
	 * Returns one Localization with the given id from cache
416
	 * 
417
	 * @param mixed $id
418
	 * @return Localization|null
419
	 */
420
	protected function get($id) {
421
		if ($this->pool === null) {
422
			$this->pool = new Map();
423
		} else if ($this->pool->has($id)) {
424
			return $this->pool->get($id);
425
		}
426
427
		$localization = LocalizationQuery::create()->findOneById($id);
428
		$this->pool->set($id, $localization);
429
430
		return $localization;
431
	}
432
433
	/**
434
	 * Returns the service container
435
	 * 
436
	 * @return ServiceContainer
437
	 */
438
	abstract protected function getServiceContainer();
439
}
440