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

LocalizationDomainTrait::updateApplicationUris()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
namespace keeko\core\domain\base;
3
4
use keeko\core\event\LocalizationEvent;
5
use keeko\core\model\ApplicationUriQuery;
6
use keeko\core\model\LanguageVariantQuery;
7
use keeko\core\model\LocalizationQuery;
8
use keeko\core\model\LocalizationVariantQuery;
9
use keeko\core\model\Localization;
10
use keeko\framework\domain\payload\Created;
11
use keeko\framework\domain\payload\Deleted;
12
use keeko\framework\domain\payload\Found;
13
use keeko\framework\domain\payload\NotDeleted;
14
use keeko\framework\domain\payload\NotFound;
15
use keeko\framework\domain\payload\NotUpdated;
16
use keeko\framework\domain\payload\NotValid;
17
use keeko\framework\domain\payload\PayloadInterface;
18
use keeko\framework\domain\payload\Updated;
19
use keeko\framework\exceptions\ErrorsException;
20
use keeko\framework\service\ServiceContainer;
21
use keeko\framework\utils\NameUtils;
22
use keeko\framework\utils\Parameters;
23
use phootwork\collection\Map;
24
25
/**
26
 */
27
trait LocalizationDomainTrait {
28
29
	/**
30
	 */
31
	protected $pool;
32
33
	/**
34
	 * Adds ApplicationUris to Localization
35
	 * 
36
	 * @param mixed $id
37
	 * @param mixed $data
38
	 * @return PayloadInterface
39
	 */
40
	public function addApplicationUris($id, $data) {
41
		// find
42
		$model = $this->get($id);
43
44
		if ($model === null) {
45
			return new NotFound(['message' => 'Localization not found.']);
46
		}
47
48
		// pass add to internal logic
49
		try {
50
			$this->doAddApplicationUris($model, $data);
51
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
52
			return new NotValid(['errors' => $e->getErrors()]);
53
		}
54
55
		// save and dispatch events
56
		$event = new LocalizationEvent($model);
57
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_ADD, $event);
58
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
59
		$rows = $model->save();
60
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_ADD, $event);
61
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
62
63
		if ($rows > 0) {
64
			return Updated(['model' => $model]);
65
		}
66
67
		return NotUpdated(['model' => $model]);
68
	}
69
70
	/**
71
	 * Adds LanguageVariants to Localization
72
	 * 
73
	 * @param mixed $id
74
	 * @param mixed $data
75
	 * @return PayloadInterface
76
	 */
77
	public function addLanguageVariants($id, $data) {
78
		// find
79
		$model = $this->get($id);
80
81
		if ($model === null) {
82
			return new NotFound(['message' => 'Localization not found.']);
83
		}
84
85
		// pass add to internal logic
86
		try {
87
			$this->doAddLanguageVariants($model, $data);
88
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
89
			return new NotValid(['errors' => $e->getErrors()]);
90
		}
91
92
		// save and dispatch events
93
		$event = new LocalizationEvent($model);
94
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_ADD, $event);
95
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
96
		$rows = $model->save();
97
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_ADD, $event);
98
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
99
100
		if ($rows > 0) {
101
			return Updated(['model' => $model]);
102
		}
103
104
		return NotUpdated(['model' => $model]);
105
	}
106
107
	/**
108
	 * Adds Localizations to Localization
109
	 * 
110
	 * @param mixed $id
111
	 * @param mixed $data
112
	 * @return PayloadInterface
113
	 */
114
	public function addLocalizations($id, $data) {
115
		// find
116
		$model = $this->get($id);
117
118
		if ($model === null) {
119
			return new NotFound(['message' => 'Localization not found.']);
120
		}
121
122
		// pass add to internal logic
123
		try {
124
			$this->doAddLocalizations($model, $data);
125
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
126
			return new NotValid(['errors' => $e->getErrors()]);
127
		}
128
129
		// save and dispatch events
130
		$event = new LocalizationEvent($model);
131
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_ADD, $event);
132
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
133
		$rows = $model->save();
134
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_ADD, $event);
135
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
136
137
		if ($rows > 0) {
138
			return Updated(['model' => $model]);
139
		}
140
141
		return NotUpdated(['model' => $model]);
142
	}
143
144
	/**
145
	 * Creates a new Localization with the provided data
146
	 * 
147
	 * @param mixed $data
148
	 * @return PayloadInterface
149
	 */
150
	public function create($data) {
151
		// hydrate
152
		$serializer = Localization::getSerializer();
153
		$model = $serializer->hydrate(new Localization(), $data);
154
		$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...
155
156
		// validate
157
		$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...
158
		if ($validator !== null && !$validator->validate($model)) {
159
			return new NotValid([
160
				'errors' => $validator->getValidationFailures()
161
			]);
162
		}
163
164
		// dispatch
165
		$event = new LocalizationEvent($model);
166
		$this->dispatch(LocalizationEvent::PRE_CREATE, $event);
167
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
168
		$model->save();
169
		$this->dispatch(LocalizationEvent::POST_CREATE, $event);
170
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
171
		return new Created(['model' => $model]);
172
	}
173
174
	/**
175
	 * Deletes a Localization with the given id
176
	 * 
177
	 * @param mixed $id
178
	 * @return PayloadInterface
179
	 */
180
	public function delete($id) {
181
		// find
182
		$model = $this->get($id);
183
184
		if ($model === null) {
185
			return new NotFound(['message' => 'Localization not found.']);
186
		}
187
188
		// delete
189
		$event = new LocalizationEvent($model);
190
		$this->dispatch(LocalizationEvent::PRE_DELETE, $event);
191
		$model->delete();
192
193
		if ($model->isDeleted()) {
194
			$this->dispatch(LocalizationEvent::POST_DELETE, $event);
195
			return new Deleted(['model' => $model]);
196
		}
197
198
		return new NotDeleted(['message' => 'Could not delete Localization']);
199
	}
200
201
	/**
202
	 * Returns a paginated result
203
	 * 
204
	 * @param Parameters $params
205
	 * @return PayloadInterface
206
	 */
207
	public function paginate(Parameters $params) {
208
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
209
		$defaultSize = $sysPrefs->getPaginationSize();
210
		$page = $params->getPage('number');
211
		$size = $params->getPage('size', $defaultSize);
212
213
		$query = LocalizationQuery::create();
214
215
		// sorting
216
		$sort = $params->getSort(Localization::getSerializer()->getSortFields());
217
		foreach ($sort as $field => $order) {
218
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
219
			$query->$method($order);
220
		}
221
222
		// filtering
223
		$filter = $params->getFilter();
224
		if (!empty($filter)) {
225
			$this->applyFilter($query, $filter);
226
		}
227
228
		// paginate
229
		$model = $query->paginate($page, $size);
230
231
		// run response
232
		return new Found(['model' => $model]);
233
	}
234
235
	/**
236
	 * Returns one Localization with the given id
237
	 * 
238
	 * @param mixed $id
239
	 * @return PayloadInterface
240
	 */
241
	public function read($id) {
242
		// read
243
		$model = $this->get($id);
244
245
		// check existence
246
		if ($model === null) {
247
			return new NotFound(['message' => 'Localization not found.']);
248
		}
249
250
		return new Found(['model' => $model]);
251
	}
252
253
	/**
254
	 * Removes ApplicationUris from Localization
255
	 * 
256
	 * @param mixed $id
257
	 * @param mixed $data
258
	 * @return PayloadInterface
259
	 */
260
	public function removeApplicationUris($id, $data) {
261
		// find
262
		$model = $this->get($id);
263
264
		if ($model === null) {
265
			return new NotFound(['message' => 'Localization not found.']);
266
		}
267
268
		// pass remove to internal logic
269
		try {
270
			$this->doRemoveApplicationUris($model, $data);
271
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
272
			return new NotValid(['errors' => $e->getErrors()]);
273
		}
274
275
		// save and dispatch events
276
		$event = new LocalizationEvent($model);
277
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_REMOVE, $event);
278
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
279
		$rows = $model->save();
280
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_REMOVE, $event);
281
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
282
283
		if ($rows > 0) {
284
			return Updated(['model' => $model]);
285
		}
286
287
		return NotUpdated(['model' => $model]);
288
	}
289
290
	/**
291
	 * Removes LanguageVariants from Localization
292
	 * 
293
	 * @param mixed $id
294
	 * @param mixed $data
295
	 * @return PayloadInterface
296
	 */
297
	public function removeLanguageVariants($id, $data) {
298
		// find
299
		$model = $this->get($id);
300
301
		if ($model === null) {
302
			return new NotFound(['message' => 'Localization not found.']);
303
		}
304
305
		// pass remove to internal logic
306
		try {
307
			$this->doRemoveLanguageVariants($model, $data);
308
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
309
			return new NotValid(['errors' => $e->getErrors()]);
310
		}
311
312
		// save and dispatch events
313
		$event = new LocalizationEvent($model);
314
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_REMOVE, $event);
315
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
316
		$rows = $model->save();
317
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_REMOVE, $event);
318
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
319
320
		if ($rows > 0) {
321
			return Updated(['model' => $model]);
322
		}
323
324
		return NotUpdated(['model' => $model]);
325
	}
326
327
	/**
328
	 * Removes Localizations from Localization
329
	 * 
330
	 * @param mixed $id
331
	 * @param mixed $data
332
	 * @return PayloadInterface
333
	 */
334
	public function removeLocalizations($id, $data) {
335
		// find
336
		$model = $this->get($id);
337
338
		if ($model === null) {
339
			return new NotFound(['message' => 'Localization not found.']);
340
		}
341
342
		// pass remove to internal logic
343
		try {
344
			$this->doRemoveLocalizations($model, $data);
345
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
346
			return new NotValid(['errors' => $e->getErrors()]);
347
		}
348
349
		// save and dispatch events
350
		$event = new LocalizationEvent($model);
351
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_REMOVE, $event);
352
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
353
		$rows = $model->save();
354
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_REMOVE, $event);
355
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
356
357
		if ($rows > 0) {
358
			return Updated(['model' => $model]);
359
		}
360
361
		return NotUpdated(['model' => $model]);
362
	}
363
364
	/**
365
	 * Sets the ExtLang id
366
	 * 
367
	 * @param mixed $id
368
	 * @param mixed $relatedId
369
	 * @return PayloadInterface
370
	 */
371
	public function setExtLangId($id, $relatedId) {
372
		// find
373
		$model = $this->get($id);
374
375
		if ($model === null) {
376
			return new NotFound(['message' => 'Localization not found.']);
377
		}
378
379
		// update
380
		if ($this->doSetExtLangId($model, $relatedId)) {
381
			$event = new LocalizationEvent($model);
382
			$this->dispatch(LocalizationEvent::PRE_EXT_LANG_UPDATE, $event);
383
			$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
384
			$model->save();
385
			$this->dispatch(LocalizationEvent::POST_EXT_LANG_UPDATE, $event);
386
			$this->dispatch(LocalizationEvent::POST_SAVE, $event);
387
388
			return Updated(['model' => $model]);
389
		}
390
391
		return NotUpdated(['model' => $model]);
392
	}
393
394
	/**
395
	 * Sets the Language id
396
	 * 
397
	 * @param mixed $id
398
	 * @param mixed $relatedId
399
	 * @return PayloadInterface
400
	 */
401
	public function setLanguageId($id, $relatedId) {
402
		// find
403
		$model = $this->get($id);
404
405
		if ($model === null) {
406
			return new NotFound(['message' => 'Localization not found.']);
407
		}
408
409
		// update
410
		if ($this->doSetLanguageId($model, $relatedId)) {
411
			$event = new LocalizationEvent($model);
412
			$this->dispatch(LocalizationEvent::PRE_LANGUAGE_UPDATE, $event);
413
			$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
414
			$model->save();
415
			$this->dispatch(LocalizationEvent::POST_LANGUAGE_UPDATE, $event);
416
			$this->dispatch(LocalizationEvent::POST_SAVE, $event);
417
418
			return Updated(['model' => $model]);
419
		}
420
421
		return NotUpdated(['model' => $model]);
422
	}
423
424
	/**
425
	 * Sets the Parent id
426
	 * 
427
	 * @param mixed $id
428
	 * @param mixed $relatedId
429
	 * @return PayloadInterface
430
	 */
431
	public function setParentId($id, $relatedId) {
432
		// find
433
		$model = $this->get($id);
434
435
		if ($model === null) {
436
			return new NotFound(['message' => 'Localization not found.']);
437
		}
438
439
		// update
440
		if ($this->doSetParentId($model, $relatedId)) {
441
			$event = new LocalizationEvent($model);
442
			$this->dispatch(LocalizationEvent::PRE_PARENT_UPDATE, $event);
443
			$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
444
			$model->save();
445
			$this->dispatch(LocalizationEvent::POST_PARENT_UPDATE, $event);
446
			$this->dispatch(LocalizationEvent::POST_SAVE, $event);
447
448
			return Updated(['model' => $model]);
449
		}
450
451
		return NotUpdated(['model' => $model]);
452
	}
453
454
	/**
455
	 * Sets the Script id
456
	 * 
457
	 * @param mixed $id
458
	 * @param mixed $relatedId
459
	 * @return PayloadInterface
460
	 */
461
	public function setScriptId($id, $relatedId) {
462
		// find
463
		$model = $this->get($id);
464
465
		if ($model === null) {
466
			return new NotFound(['message' => 'Localization not found.']);
467
		}
468
469
		// update
470
		if ($this->doSetScriptId($model, $relatedId)) {
471
			$event = new LocalizationEvent($model);
472
			$this->dispatch(LocalizationEvent::PRE_SCRIPT_UPDATE, $event);
473
			$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
474
			$model->save();
475
			$this->dispatch(LocalizationEvent::POST_SCRIPT_UPDATE, $event);
476
			$this->dispatch(LocalizationEvent::POST_SAVE, $event);
477
478
			return Updated(['model' => $model]);
479
		}
480
481
		return NotUpdated(['model' => $model]);
482
	}
483
484
	/**
485
	 * Updates a Localization with the given idand the provided data
486
	 * 
487
	 * @param mixed $id
488
	 * @param mixed $data
489
	 * @return PayloadInterface
490
	 */
491
	public function update($id, $data) {
492
		// find
493
		$model = $this->get($id);
494
495
		if ($model === null) {
496
			return new NotFound(['message' => 'Localization not found.']);
497
		}
498
499
		// hydrate
500
		$serializer = Localization::getSerializer();
501
		$model = $serializer->hydrate($model, $data);
502
		$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...
503
504
		// validate
505
		$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...
506
		if ($validator !== null && !$validator->validate($model)) {
507
			return new NotValid([
508
				'errors' => $validator->getValidationFailures()
509
			]);
510
		}
511
512
		// dispatch
513
		$event = new LocalizationEvent($model);
514
		$this->dispatch(LocalizationEvent::PRE_UPDATE, $event);
515
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
516
		$rows = $model->save();
517
		$this->dispatch(LocalizationEvent::POST_UPDATE, $event);
518
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
519
520
		$payload = ['model' => $model];
521
522
		if ($rows === 0) {
523
			return new NotUpdated($payload);
524
		}
525
526
		return new Updated($payload);
527
	}
528
529
	/**
530
	 * Updates ApplicationUris on Localization
531
	 * 
532
	 * @param mixed $id
533
	 * @param mixed $data
534
	 * @return PayloadInterface
535
	 */
536
	public function updateApplicationUris($id, $data) {
537
		// find
538
		$model = $this->get($id);
539
540
		if ($model === null) {
541
			return new NotFound(['message' => 'Localization not found.']);
542
		}
543
544
		// pass update to internal logic
545
		try {
546
			$this->doUpdateApplicationUris($model, $data);
547
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
548
			return new NotValid(['errors' => $e->getErrors()]);
549
		}
550
551
		// save and dispatch events
552
		$event = new LocalizationEvent($model);
553
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_UPDATE, $event);
554
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
555
		$rows = $model->save();
556
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_UPDATE, $event);
557
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
558
559
		if ($rows > 0) {
560
			return Updated(['model' => $model]);
561
		}
562
563
		return NotUpdated(['model' => $model]);
564
	}
565
566
	/**
567
	 * Updates LanguageVariants on Localization
568
	 * 
569
	 * @param mixed $id
570
	 * @param mixed $data
571
	 * @return PayloadInterface
572
	 */
573
	public function updateLanguageVariants($id, $data) {
574
		// find
575
		$model = $this->get($id);
576
577
		if ($model === null) {
578
			return new NotFound(['message' => 'Localization not found.']);
579
		}
580
581
		// pass update to internal logic
582
		try {
583
			$this->doUpdateLanguageVariants($model, $data);
584
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
585
			return new NotValid(['errors' => $e->getErrors()]);
586
		}
587
588
		// save and dispatch events
589
		$event = new LocalizationEvent($model);
590
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_UPDATE, $event);
591
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
592
		$rows = $model->save();
593
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_UPDATE, $event);
594
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
595
596
		if ($rows > 0) {
597
			return Updated(['model' => $model]);
598
		}
599
600
		return NotUpdated(['model' => $model]);
601
	}
602
603
	/**
604
	 * Updates Localizations on Localization
605
	 * 
606
	 * @param mixed $id
607
	 * @param mixed $data
608
	 * @return PayloadInterface
609
	 */
610
	public function updateLocalizations($id, $data) {
611
		// find
612
		$model = $this->get($id);
613
614
		if ($model === null) {
615
			return new NotFound(['message' => 'Localization not found.']);
616
		}
617
618
		// pass update to internal logic
619
		try {
620
			$this->doUpdateLocalizations($model, $data);
621
		} catch (ErrorsException $e) {
0 ignored issues
show
Bug introduced by
The class keeko\framework\exceptions\ErrorsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
622
			return new NotValid(['errors' => $e->getErrors()]);
623
		}
624
625
		// save and dispatch events
626
		$event = new LocalizationEvent($model);
627
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_UPDATE, $event);
628
		$this->dispatch(LocalizationEvent::PRE_SAVE, $event);
629
		$rows = $model->save();
630
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_UPDATE, $event);
631
		$this->dispatch(LocalizationEvent::POST_SAVE, $event);
632
633
		if ($rows > 0) {
634
			return Updated(['model' => $model]);
635
		}
636
637
		return NotUpdated(['model' => $model]);
638
	}
639
640
	/**
641
	 * @param mixed $query
642
	 * @param mixed $filter
643
	 * @return void
644
	 */
645
	protected function applyFilter($query, $filter) {
646
		foreach ($filter as $column => $value) {
647
			$pos = strpos($column, '.');
648
			if ($pos !== false) {
649
				$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
650
				$col = substr($column, $pos + 1);
651
				$method = 'use' . $rel . 'Query';
652
				if (method_exists($query, $method)) {
653
					$sub = $query->$method();
654
					$this->applyFilter($sub, [$col => $value]);
655
					$sub->endUse();
656
				}
657
			} else {
658
				$method = 'filterBy' . NameUtils::toStudlyCase($column);
659
				if (method_exists($query, $method)) {
660
					$query->$method($value);
661
				}
662
			}
663
		}
664
	}
665
666
	/**
667
	 * @param string $type
668
	 * @param LocalizationEvent $event
669
	 */
670
	protected function dispatch($type, LocalizationEvent $event) {
671
		$model = $event->getLocalization();
672
		$methods = [
673
			LocalizationEvent::PRE_CREATE => 'preCreate',
674
			LocalizationEvent::POST_CREATE => 'postCreate',
675
			LocalizationEvent::PRE_UPDATE => 'preUpdate',
676
			LocalizationEvent::POST_UPDATE => 'postUpdate',
677
			LocalizationEvent::PRE_DELETE => 'preDelete',
678
			LocalizationEvent::POST_DELETE => 'postDelete',
679
			LocalizationEvent::PRE_SAVE => 'preSave',
680
			LocalizationEvent::POST_SAVE => 'postSave'
681
		];
682
683
		if (isset($methods[$type])) {
684
			$method = $methods[$type];
685
			if (method_exists($this, $method)) {
686
				$this->$method($model);
687
			}
688
		}
689
690
		$dispatcher = $this->getServiceContainer()->getDispatcher();
691
		$dispatcher->dispatch($type, $event);
692
	}
693
694
	/**
695
	 * Interal mechanism to add ApplicationUris to Localization
696
	 * 
697
	 * @param Localization $model
698
	 * @param mixed $data
699
	 */
700
	protected function doAddApplicationUris(Localization $model, $data) {
701
		$errors = [];
702
		foreach ($data as $entry) {
703
			if (!isset($entry['id'])) {
704
				$errors[] = 'Missing id for ApplicationUri';
705
			} else {
706
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
707
				$model->addApplicationUri($related);
708
			}
709
		}
710
711
		if (count($errors) > 0) {
712
			return new ErrorsException($errors);
713
		}
714
	}
715
716
	/**
717
	 * Interal mechanism to add LanguageVariants to Localization
718
	 * 
719
	 * @param Localization $model
720
	 * @param mixed $data
721
	 */
722
	protected function doAddLanguageVariants(Localization $model, $data) {
723
		$errors = [];
724
		foreach ($data as $entry) {
725
			if (!isset($entry['id'])) {
726
				$errors[] = 'Missing id for LanguageVariant';
727
			} else {
728
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
729
				$model->addLanguageVariant($related);
730
			}
731
		}
732
733
		if (count($errors) > 0) {
734
			return new ErrorsException($errors);
735
		}
736
	}
737
738
	/**
739
	 * Interal mechanism to add Localizations to Localization
740
	 * 
741
	 * @param Localization $model
742
	 * @param mixed $data
743
	 */
744
	protected function doAddLocalizations(Localization $model, $data) {
745
		$errors = [];
746
		foreach ($data as $entry) {
747
			if (!isset($entry['id'])) {
748
				$errors[] = 'Missing id for Localization';
749
			} else {
750
				$related = LocalizationQuery::create()->findOneById($entry['id']);
751
				$model->addLocalization($related);
752
			}
753
		}
754
755
		if (count($errors) > 0) {
756
			return new ErrorsException($errors);
757
		}
758
	}
759
760
	/**
761
	 * Interal mechanism to remove ApplicationUris from Localization
762
	 * 
763
	 * @param Localization $model
764
	 * @param mixed $data
765
	 */
766
	protected function doRemoveApplicationUris(Localization $model, $data) {
767
		$errors = [];
768
		foreach ($data as $entry) {
769
			if (!isset($entry['id'])) {
770
				$errors[] = 'Missing id for ApplicationUri';
771
			} else {
772
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
773
				$model->removeApplicationUri($related);
774
			}
775
		}
776
777
		if (count($errors) > 0) {
778
			return new ErrorsException($errors);
779
		}
780
	}
781
782
	/**
783
	 * Interal mechanism to remove LanguageVariants from Localization
784
	 * 
785
	 * @param Localization $model
786
	 * @param mixed $data
787
	 */
788
	protected function doRemoveLanguageVariants(Localization $model, $data) {
789
		$errors = [];
790
		foreach ($data as $entry) {
791
			if (!isset($entry['id'])) {
792
				$errors[] = 'Missing id for LanguageVariant';
793
			} else {
794
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
795
				$model->removeLanguageVariant($related);
796
			}
797
		}
798
799
		if (count($errors) > 0) {
800
			return new ErrorsException($errors);
801
		}
802
	}
803
804
	/**
805
	 * Interal mechanism to remove Localizations from Localization
806
	 * 
807
	 * @param Localization $model
808
	 * @param mixed $data
809
	 */
810
	protected function doRemoveLocalizations(Localization $model, $data) {
811
		$errors = [];
812
		foreach ($data as $entry) {
813
			if (!isset($entry['id'])) {
814
				$errors[] = 'Missing id for Localization';
815
			} else {
816
				$related = LocalizationQuery::create()->findOneById($entry['id']);
817
				$model->removeLocalization($related);
818
			}
819
		}
820
821
		if (count($errors) > 0) {
822
			return new ErrorsException($errors);
823
		}
824
	}
825
826
	/**
827
	 * Internal mechanism to set the ExtLang id
828
	 * 
829
	 * @param Localization $model
830
	 * @param mixed $relatedId
831
	 */
832
	protected function doSetExtLangId(Localization $model, $relatedId) {
833
		if ($model->getExtLanguageId() !== $relatedId) {
834
			$model->setExtLanguageId($relatedId);
835
836
			return true;
837
		}
838
839
		return false;
840
	}
841
842
	/**
843
	 * Internal mechanism to set the Language id
844
	 * 
845
	 * @param Localization $model
846
	 * @param mixed $relatedId
847
	 */
848
	protected function doSetLanguageId(Localization $model, $relatedId) {
849
		if ($model->getLanguageId() !== $relatedId) {
850
			$model->setLanguageId($relatedId);
851
852
			return true;
853
		}
854
855
		return false;
856
	}
857
858
	/**
859
	 * Internal mechanism to set the Parent id
860
	 * 
861
	 * @param Localization $model
862
	 * @param mixed $relatedId
863
	 */
864
	protected function doSetParentId(Localization $model, $relatedId) {
865
		if ($model->getParentId() !== $relatedId) {
866
			$model->setParentId($relatedId);
867
868
			return true;
869
		}
870
871
		return false;
872
	}
873
874
	/**
875
	 * Internal mechanism to set the Script id
876
	 * 
877
	 * @param Localization $model
878
	 * @param mixed $relatedId
879
	 */
880
	protected function doSetScriptId(Localization $model, $relatedId) {
881
		if ($model->getScriptId() !== $relatedId) {
882
			$model->setScriptId($relatedId);
883
884
			return true;
885
		}
886
887
		return false;
888
	}
889
890
	/**
891
	 * Internal update mechanism of ApplicationUris on Localization
892
	 * 
893
	 * @param Localization $model
894
	 * @param mixed $data
895
	 */
896
	protected function doUpdateApplicationUris(Localization $model, $data) {
897
		// remove all relationships before
898
		ApplicationUriQuery::create()->filterByLocalization($model)->delete();
899
900
		// add them
901
		$errors = [];
902
		foreach ($data as $entry) {
903
			if (!isset($entry['id'])) {
904
				$errors[] = 'Missing id for ApplicationUri';
905
			} else {
906
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
907
				$model->addApplicationUri($related);
908
			}
909
		}
910
911
		if (count($errors) > 0) {
912
			throw new ErrorsException($errors);
913
		}
914
	}
915
916
	/**
917
	 * Internal update mechanism of LanguageVariants on Localization
918
	 * 
919
	 * @param Localization $model
920
	 * @param mixed $data
921
	 */
922
	protected function doUpdateLanguageVariants(Localization $model, $data) {
923
		// remove all relationships before
924
		LocalizationVariantQuery::create()->filterByLocalization($model)->delete();
925
926
		// add them
927
		$errors = [];
928
		foreach ($data as $entry) {
929
			if (!isset($entry['id'])) {
930
				$errors[] = 'Missing id for LanguageVariant';
931
			} else {
932
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
933
				$model->addLanguageVariant($related);
934
			}
935
		}
936
937
		if (count($errors) > 0) {
938
			throw new ErrorsException($errors);
939
		}
940
	}
941
942
	/**
943
	 * Internal update mechanism of Localizations on Localization
944
	 * 
945
	 * @param Localization $model
946
	 * @param mixed $data
947
	 */
948
	protected function doUpdateLocalizations(Localization $model, $data) {
949
		// remove all relationships before
950
		LocalizationQuery::create()->filterByParent($model)->delete();
951
952
		// add them
953
		$errors = [];
954
		foreach ($data as $entry) {
955
			if (!isset($entry['id'])) {
956
				$errors[] = 'Missing id for Localization';
957
			} else {
958
				$related = LocalizationQuery::create()->findOneById($entry['id']);
959
				$model->addLocalization($related);
960
			}
961
		}
962
963
		if (count($errors) > 0) {
964
			throw new ErrorsException($errors);
965
		}
966
	}
967
968
	/**
969
	 * Returns one Localization with the given id from cache
970
	 * 
971
	 * @param mixed $id
972
	 * @return Localization|null
973
	 */
974
	protected function get($id) {
975
		if ($this->pool === null) {
976
			$this->pool = new Map();
977
		} else if ($this->pool->has($id)) {
978
			return $this->pool->get($id);
979
		}
980
981
		$model = LocalizationQuery::create()->findOneById($id);
982
		$this->pool->set($id, $model);
983
984
		return $model;
985
	}
986
987
	/**
988
	 * Returns the service container
989
	 * 
990
	 * @return ServiceContainer
991
	 */
992
	abstract protected function getServiceContainer();
993
}
994