LocalizationDomainTrait::updateLanguageVariants()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 16
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
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_ADD, $model, $data);
57
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
58
		$rows = $model->save();
59
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_ADD, $model, $data);
60
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
61
62
		if ($rows > 0) {
63
			return Updated(['model' => $model]);
64
		}
65
66
		return NotUpdated(['model' => $model]);
67
	}
68
69
	/**
70
	 * Adds LanguageVariants to Localization
71
	 * 
72
	 * @param mixed $id
73
	 * @param mixed $data
74
	 * @return PayloadInterface
75
	 */
76
	public function addLanguageVariants($id, $data) {
77
		// find
78
		$model = $this->get($id);
79
80
		if ($model === null) {
81
			return new NotFound(['message' => 'Localization not found.']);
82
		}
83
84
		// pass add to internal logic
85
		try {
86
			$this->doAddLanguageVariants($model, $data);
87
		} 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...
88
			return new NotValid(['errors' => $e->getErrors()]);
89
		}
90
91
		// save and dispatch events
92
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_ADD, $model, $data);
93
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
94
		$rows = $model->save();
95
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_ADD, $model, $data);
96
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
97
98
		if ($rows > 0) {
99
			return Updated(['model' => $model]);
100
		}
101
102
		return NotUpdated(['model' => $model]);
103
	}
104
105
	/**
106
	 * Adds Localizations to Localization
107
	 * 
108
	 * @param mixed $id
109
	 * @param mixed $data
110
	 * @return PayloadInterface
111
	 */
112
	public function addLocalizations($id, $data) {
113
		// find
114
		$model = $this->get($id);
115
116
		if ($model === null) {
117
			return new NotFound(['message' => 'Localization not found.']);
118
		}
119
120
		// pass add to internal logic
121
		try {
122
			$this->doAddLocalizations($model, $data);
123
		} 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...
124
			return new NotValid(['errors' => $e->getErrors()]);
125
		}
126
127
		// save and dispatch events
128
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_ADD, $model, $data);
129
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
130
		$rows = $model->save();
131
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_ADD, $model, $data);
132
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
133
134
		if ($rows > 0) {
135
			return Updated(['model' => $model]);
136
		}
137
138
		return NotUpdated(['model' => $model]);
139
	}
140
141
	/**
142
	 * Creates a new Localization with the provided data
143
	 * 
144
	 * @param mixed $data
145
	 * @return PayloadInterface
146
	 */
147
	public function create($data) {
148
		// hydrate
149
		$serializer = Localization::getSerializer();
150
		$model = $serializer->hydrate(new Localization(), $data);
151
		$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...
152
153
		// dispatch pre save hooks
154
		$this->dispatch(LocalizationEvent::PRE_CREATE, $model, $data);
155
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
156
157
		// validate
158
		$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...
159
		if ($validator !== null && !$validator->validate($model)) {
160
			return new NotValid([
161
				'errors' => $validator->getValidationFailures()
162
			]);
163
		}
164
165
		// save and dispatch post save hooks
166
		$model->save();
167
		$this->dispatch(LocalizationEvent::POST_CREATE, $model, $data);
168
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
169
170
		return new Created(['model' => $model]);
171
	}
172
173
	/**
174
	 * Deletes a Localization with the given id
175
	 * 
176
	 * @param mixed $id
177
	 * @return PayloadInterface
178
	 */
179
	public function delete($id) {
180
		// find
181
		$model = $this->get($id);
182
183
		if ($model === null) {
184
			return new NotFound(['message' => 'Localization not found.']);
185
		}
186
187
		// delete
188
		$this->dispatch(LocalizationEvent::PRE_DELETE, $model);
189
		$model->delete();
190
191
		if ($model->isDeleted()) {
192
			$this->dispatch(LocalizationEvent::POST_DELETE, $model);
193
			return new Deleted(['model' => $model]);
194
		}
195
196
		return new NotDeleted(['message' => 'Could not delete Localization']);
197
	}
198
199
	/**
200
	 * Returns a paginated result
201
	 * 
202
	 * @param Parameters $params
203
	 * @return PayloadInterface
204
	 */
205
	public function paginate(Parameters $params) {
206
		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
207
		$defaultSize = $sysPrefs->getPaginationSize();
208
		$page = $params->getPage('number');
209
		$size = $params->getPage('size', $defaultSize);
210
211
		$query = LocalizationQuery::create();
212
213
		// sorting
214
		$sort = $params->getSort(Localization::getSerializer()->getSortFields());
215
		foreach ($sort as $field => $order) {
216
			$method = 'orderBy' . NameUtils::toStudlyCase($field);
217
			$query->$method($order);
218
		}
219
220
		// filtering
221
		$filter = $params->getFilter();
222
		if (!empty($filter)) {
223
			$this->applyFilter($query, $filter);
224
		}
225
226
		// paginate
227
		$model = $query->paginate($page, $size);
228
229
		// run response
230
		return new Found(['model' => $model]);
231
	}
232
233
	/**
234
	 * Returns one Localization with the given id
235
	 * 
236
	 * @param mixed $id
237
	 * @return PayloadInterface
238
	 */
239
	public function read($id) {
240
		// read
241
		$model = $this->get($id);
242
243
		// check existence
244
		if ($model === null) {
245
			return new NotFound(['message' => 'Localization not found.']);
246
		}
247
248
		return new Found(['model' => $model]);
249
	}
250
251
	/**
252
	 * Removes ApplicationUris from Localization
253
	 * 
254
	 * @param mixed $id
255
	 * @param mixed $data
256
	 * @return PayloadInterface
257
	 */
258
	public function removeApplicationUris($id, $data) {
259
		// find
260
		$model = $this->get($id);
261
262
		if ($model === null) {
263
			return new NotFound(['message' => 'Localization not found.']);
264
		}
265
266
		// pass remove to internal logic
267
		try {
268
			$this->doRemoveApplicationUris($model, $data);
269
		} 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...
270
			return new NotValid(['errors' => $e->getErrors()]);
271
		}
272
273
		// save and dispatch events
274
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_REMOVE, $model, $data);
275
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
276
		$rows = $model->save();
277
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_REMOVE, $model, $data);
278
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
279
280
		if ($rows > 0) {
281
			return Updated(['model' => $model]);
282
		}
283
284
		return NotUpdated(['model' => $model]);
285
	}
286
287
	/**
288
	 * Removes LanguageVariants from Localization
289
	 * 
290
	 * @param mixed $id
291
	 * @param mixed $data
292
	 * @return PayloadInterface
293
	 */
294
	public function removeLanguageVariants($id, $data) {
295
		// find
296
		$model = $this->get($id);
297
298
		if ($model === null) {
299
			return new NotFound(['message' => 'Localization not found.']);
300
		}
301
302
		// pass remove to internal logic
303
		try {
304
			$this->doRemoveLanguageVariants($model, $data);
305
		} 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...
306
			return new NotValid(['errors' => $e->getErrors()]);
307
		}
308
309
		// save and dispatch events
310
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_REMOVE, $model, $data);
311
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
312
		$rows = $model->save();
313
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_REMOVE, $model, $data);
314
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
315
316
		if ($rows > 0) {
317
			return Updated(['model' => $model]);
318
		}
319
320
		return NotUpdated(['model' => $model]);
321
	}
322
323
	/**
324
	 * Removes Localizations from Localization
325
	 * 
326
	 * @param mixed $id
327
	 * @param mixed $data
328
	 * @return PayloadInterface
329
	 */
330
	public function removeLocalizations($id, $data) {
331
		// find
332
		$model = $this->get($id);
333
334
		if ($model === null) {
335
			return new NotFound(['message' => 'Localization not found.']);
336
		}
337
338
		// pass remove to internal logic
339
		try {
340
			$this->doRemoveLocalizations($model, $data);
341
		} 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...
342
			return new NotValid(['errors' => $e->getErrors()]);
343
		}
344
345
		// save and dispatch events
346
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_REMOVE, $model, $data);
347
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
348
		$rows = $model->save();
349
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_REMOVE, $model, $data);
350
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
351
352
		if ($rows > 0) {
353
			return Updated(['model' => $model]);
354
		}
355
356
		return NotUpdated(['model' => $model]);
357
	}
358
359
	/**
360
	 * Sets the ExtLang id
361
	 * 
362
	 * @param mixed $id
363
	 * @param mixed $relatedId
364
	 * @return PayloadInterface
365
	 */
366
	public function setExtLangId($id, $relatedId) {
367
		// find
368
		$model = $this->get($id);
369
370
		if ($model === null) {
371
			return new NotFound(['message' => 'Localization not found.']);
372
		}
373
374
		// update
375
		if ($this->doSetExtLangId($model, $relatedId)) {
376
			$this->dispatch(LocalizationEvent::PRE_EXT_LANG_UPDATE, $model);
377
			$this->dispatch(LocalizationEvent::PRE_SAVE, $model);
378
			$model->save();
379
			$this->dispatch(LocalizationEvent::POST_EXT_LANG_UPDATE, $model);
380
			$this->dispatch(LocalizationEvent::POST_SAVE, $model);
381
382
			return Updated(['model' => $model]);
383
		}
384
385
		return NotUpdated(['model' => $model]);
386
	}
387
388
	/**
389
	 * Sets the Language id
390
	 * 
391
	 * @param mixed $id
392
	 * @param mixed $relatedId
393
	 * @return PayloadInterface
394
	 */
395
	public function setLanguageId($id, $relatedId) {
396
		// find
397
		$model = $this->get($id);
398
399
		if ($model === null) {
400
			return new NotFound(['message' => 'Localization not found.']);
401
		}
402
403
		// update
404
		if ($this->doSetLanguageId($model, $relatedId)) {
405
			$this->dispatch(LocalizationEvent::PRE_LANGUAGE_UPDATE, $model);
406
			$this->dispatch(LocalizationEvent::PRE_SAVE, $model);
407
			$model->save();
408
			$this->dispatch(LocalizationEvent::POST_LANGUAGE_UPDATE, $model);
409
			$this->dispatch(LocalizationEvent::POST_SAVE, $model);
410
411
			return Updated(['model' => $model]);
412
		}
413
414
		return NotUpdated(['model' => $model]);
415
	}
416
417
	/**
418
	 * Sets the Parent id
419
	 * 
420
	 * @param mixed $id
421
	 * @param mixed $relatedId
422
	 * @return PayloadInterface
423
	 */
424
	public function setParentId($id, $relatedId) {
425
		// find
426
		$model = $this->get($id);
427
428
		if ($model === null) {
429
			return new NotFound(['message' => 'Localization not found.']);
430
		}
431
432
		// update
433
		if ($this->doSetParentId($model, $relatedId)) {
434
			$this->dispatch(LocalizationEvent::PRE_PARENT_UPDATE, $model);
435
			$this->dispatch(LocalizationEvent::PRE_SAVE, $model);
436
			$model->save();
437
			$this->dispatch(LocalizationEvent::POST_PARENT_UPDATE, $model);
438
			$this->dispatch(LocalizationEvent::POST_SAVE, $model);
439
440
			return Updated(['model' => $model]);
441
		}
442
443
		return NotUpdated(['model' => $model]);
444
	}
445
446
	/**
447
	 * Sets the Script id
448
	 * 
449
	 * @param mixed $id
450
	 * @param mixed $relatedId
451
	 * @return PayloadInterface
452
	 */
453
	public function setScriptId($id, $relatedId) {
454
		// find
455
		$model = $this->get($id);
456
457
		if ($model === null) {
458
			return new NotFound(['message' => 'Localization not found.']);
459
		}
460
461
		// update
462
		if ($this->doSetScriptId($model, $relatedId)) {
463
			$this->dispatch(LocalizationEvent::PRE_SCRIPT_UPDATE, $model);
464
			$this->dispatch(LocalizationEvent::PRE_SAVE, $model);
465
			$model->save();
466
			$this->dispatch(LocalizationEvent::POST_SCRIPT_UPDATE, $model);
467
			$this->dispatch(LocalizationEvent::POST_SAVE, $model);
468
469
			return Updated(['model' => $model]);
470
		}
471
472
		return NotUpdated(['model' => $model]);
473
	}
474
475
	/**
476
	 * Updates a Localization with the given idand the provided data
477
	 * 
478
	 * @param mixed $id
479
	 * @param mixed $data
480
	 * @return PayloadInterface
481
	 */
482
	public function update($id, $data) {
483
		// find
484
		$model = $this->get($id);
485
486
		if ($model === null) {
487
			return new NotFound(['message' => 'Localization not found.']);
488
		}
489
490
		// hydrate
491
		$serializer = Localization::getSerializer();
492
		$model = $serializer->hydrate($model, $data);
493
		$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...
494
495
		// dispatch pre save hooks
496
		$this->dispatch(LocalizationEvent::PRE_UPDATE, $model, $data);
497
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
498
499
		// validate
500
		$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...
501
		if ($validator !== null && !$validator->validate($model)) {
502
			return new NotValid([
503
				'errors' => $validator->getValidationFailures()
504
			]);
505
		}
506
507
		// save and dispath post save hooks
508
		$rows = $model->save();
509
		$this->dispatch(LocalizationEvent::POST_UPDATE, $model, $data);
510
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
511
512
		$payload = ['model' => $model];
513
514
		if ($rows === 0) {
515
			return new NotUpdated($payload);
516
		}
517
518
		return new Updated($payload);
519
	}
520
521
	/**
522
	 * Updates ApplicationUris on Localization
523
	 * 
524
	 * @param mixed $id
525
	 * @param mixed $data
526
	 * @return PayloadInterface
527
	 */
528
	public function updateApplicationUris($id, $data) {
529
		// find
530
		$model = $this->get($id);
531
532
		if ($model === null) {
533
			return new NotFound(['message' => 'Localization not found.']);
534
		}
535
536
		// pass update to internal logic
537
		try {
538
			$this->doUpdateApplicationUris($model, $data);
539
		} 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...
540
			return new NotValid(['errors' => $e->getErrors()]);
541
		}
542
543
		// save and dispatch events
544
		$this->dispatch(LocalizationEvent::PRE_APPLICATION_URIS_UPDATE, $model, $data);
545
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
546
		$rows = $model->save();
547
		$this->dispatch(LocalizationEvent::POST_APPLICATION_URIS_UPDATE, $model, $data);
548
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
549
550
		if ($rows > 0) {
551
			return Updated(['model' => $model]);
552
		}
553
554
		return NotUpdated(['model' => $model]);
555
	}
556
557
	/**
558
	 * Updates LanguageVariants on Localization
559
	 * 
560
	 * @param mixed $id
561
	 * @param mixed $data
562
	 * @return PayloadInterface
563
	 */
564
	public function updateLanguageVariants($id, $data) {
565
		// find
566
		$model = $this->get($id);
567
568
		if ($model === null) {
569
			return new NotFound(['message' => 'Localization not found.']);
570
		}
571
572
		// pass update to internal logic
573
		try {
574
			$this->doUpdateLanguageVariants($model, $data);
575
		} 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...
576
			return new NotValid(['errors' => $e->getErrors()]);
577
		}
578
579
		// save and dispatch events
580
		$this->dispatch(LocalizationEvent::PRE_LANGUAGE_VARIANTS_UPDATE, $model, $data);
581
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
582
		$rows = $model->save();
583
		$this->dispatch(LocalizationEvent::POST_LANGUAGE_VARIANTS_UPDATE, $model, $data);
584
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
585
586
		if ($rows > 0) {
587
			return Updated(['model' => $model]);
588
		}
589
590
		return NotUpdated(['model' => $model]);
591
	}
592
593
	/**
594
	 * Updates Localizations on Localization
595
	 * 
596
	 * @param mixed $id
597
	 * @param mixed $data
598
	 * @return PayloadInterface
599
	 */
600
	public function updateLocalizations($id, $data) {
601
		// find
602
		$model = $this->get($id);
603
604
		if ($model === null) {
605
			return new NotFound(['message' => 'Localization not found.']);
606
		}
607
608
		// pass update to internal logic
609
		try {
610
			$this->doUpdateLocalizations($model, $data);
611
		} 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...
612
			return new NotValid(['errors' => $e->getErrors()]);
613
		}
614
615
		// save and dispatch events
616
		$this->dispatch(LocalizationEvent::PRE_LOCALIZATIONS_UPDATE, $model, $data);
617
		$this->dispatch(LocalizationEvent::PRE_SAVE, $model, $data);
618
		$rows = $model->save();
619
		$this->dispatch(LocalizationEvent::POST_LOCALIZATIONS_UPDATE, $model, $data);
620
		$this->dispatch(LocalizationEvent::POST_SAVE, $model, $data);
621
622
		if ($rows > 0) {
623
			return Updated(['model' => $model]);
624
		}
625
626
		return NotUpdated(['model' => $model]);
627
	}
628
629
	/**
630
	 * @param mixed $query
631
	 * @param mixed $filter
632
	 * @return void
633
	 */
634
	protected function applyFilter($query, $filter) {
635
		foreach ($filter as $column => $value) {
636
			$pos = strpos($column, '.');
637
			if ($pos !== false) {
638
				$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
639
				$col = substr($column, $pos + 1);
640
				$method = 'use' . $rel . 'Query';
641
				if (method_exists($query, $method)) {
642
					$sub = $query->$method();
643
					$this->applyFilter($sub, [$col => $value]);
644
					$sub->endUse();
645
				}
646
			} else {
647
				$method = 'filterBy' . NameUtils::toStudlyCase($column);
648
				if (method_exists($query, $method)) {
649
					$query->$method($value);
650
				}
651
			}
652
		}
653
	}
654
655
	/**
656
	 * @param string $type
657
	 * @param Localization $model
658
	 * @param array $data
659
	 */
660
	protected function dispatch($type, Localization $model, array $data = []) {
661
		$methods = [
662
			LocalizationEvent::PRE_CREATE => 'preCreate',
663
			LocalizationEvent::POST_CREATE => 'postCreate',
664
			LocalizationEvent::PRE_UPDATE => 'preUpdate',
665
			LocalizationEvent::POST_UPDATE => 'postUpdate',
666
			LocalizationEvent::PRE_DELETE => 'preDelete',
667
			LocalizationEvent::POST_DELETE => 'postDelete',
668
			LocalizationEvent::PRE_SAVE => 'preSave',
669
			LocalizationEvent::POST_SAVE => 'postSave'
670
		];
671
672
		if (isset($methods[$type])) {
673
			$method = $methods[$type];
674
			if (method_exists($this, $method)) {
675
				$this->$method($model, $data);
676
			}
677
		}
678
679
		$dispatcher = $this->getServiceContainer()->getDispatcher();
680
		$dispatcher->dispatch($type, new LocalizationEvent($model));
681
	}
682
683
	/**
684
	 * Interal mechanism to add ApplicationUris to Localization
685
	 * 
686
	 * @param Localization $model
687
	 * @param mixed $data
688
	 */
689
	protected function doAddApplicationUris(Localization $model, $data) {
690
		$errors = [];
691
		foreach ($data as $entry) {
692
			if (!isset($entry['id'])) {
693
				$errors[] = 'Missing id for ApplicationUri';
694
			} else {
695
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
696
				$model->addApplicationUri($related);
697
			}
698
		}
699
700
		if (count($errors) > 0) {
701
			return new ErrorsException($errors);
702
		}
703
	}
704
705
	/**
706
	 * Interal mechanism to add LanguageVariants to Localization
707
	 * 
708
	 * @param Localization $model
709
	 * @param mixed $data
710
	 */
711
	protected function doAddLanguageVariants(Localization $model, $data) {
712
		$errors = [];
713
		foreach ($data as $entry) {
714
			if (!isset($entry['id'])) {
715
				$errors[] = 'Missing id for LanguageVariant';
716
			} else {
717
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
718
				$model->addLanguageVariant($related);
719
			}
720
		}
721
722
		if (count($errors) > 0) {
723
			return new ErrorsException($errors);
724
		}
725
	}
726
727
	/**
728
	 * Interal mechanism to add Localizations to Localization
729
	 * 
730
	 * @param Localization $model
731
	 * @param mixed $data
732
	 */
733
	protected function doAddLocalizations(Localization $model, $data) {
734
		$errors = [];
735
		foreach ($data as $entry) {
736
			if (!isset($entry['id'])) {
737
				$errors[] = 'Missing id for Localization';
738
			} else {
739
				$related = LocalizationQuery::create()->findOneById($entry['id']);
740
				$model->addLocalization($related);
741
			}
742
		}
743
744
		if (count($errors) > 0) {
745
			return new ErrorsException($errors);
746
		}
747
	}
748
749
	/**
750
	 * Interal mechanism to remove ApplicationUris from Localization
751
	 * 
752
	 * @param Localization $model
753
	 * @param mixed $data
754
	 */
755
	protected function doRemoveApplicationUris(Localization $model, $data) {
756
		$errors = [];
757
		foreach ($data as $entry) {
758
			if (!isset($entry['id'])) {
759
				$errors[] = 'Missing id for ApplicationUri';
760
			} else {
761
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
762
				$model->removeApplicationUri($related);
763
			}
764
		}
765
766
		if (count($errors) > 0) {
767
			return new ErrorsException($errors);
768
		}
769
	}
770
771
	/**
772
	 * Interal mechanism to remove LanguageVariants from Localization
773
	 * 
774
	 * @param Localization $model
775
	 * @param mixed $data
776
	 */
777
	protected function doRemoveLanguageVariants(Localization $model, $data) {
778
		$errors = [];
779
		foreach ($data as $entry) {
780
			if (!isset($entry['id'])) {
781
				$errors[] = 'Missing id for LanguageVariant';
782
			} else {
783
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
784
				$model->removeLanguageVariant($related);
785
			}
786
		}
787
788
		if (count($errors) > 0) {
789
			return new ErrorsException($errors);
790
		}
791
	}
792
793
	/**
794
	 * Interal mechanism to remove Localizations from Localization
795
	 * 
796
	 * @param Localization $model
797
	 * @param mixed $data
798
	 */
799
	protected function doRemoveLocalizations(Localization $model, $data) {
800
		$errors = [];
801
		foreach ($data as $entry) {
802
			if (!isset($entry['id'])) {
803
				$errors[] = 'Missing id for Localization';
804
			} else {
805
				$related = LocalizationQuery::create()->findOneById($entry['id']);
806
				$model->removeLocalization($related);
807
			}
808
		}
809
810
		if (count($errors) > 0) {
811
			return new ErrorsException($errors);
812
		}
813
	}
814
815
	/**
816
	 * Internal mechanism to set the ExtLang id
817
	 * 
818
	 * @param Localization $model
819
	 * @param mixed $relatedId
820
	 */
821
	protected function doSetExtLangId(Localization $model, $relatedId) {
822
		if ($model->getExtLanguageId() !== $relatedId) {
823
			$model->setExtLanguageId($relatedId);
824
825
			return true;
826
		}
827
828
		return false;
829
	}
830
831
	/**
832
	 * Internal mechanism to set the Language id
833
	 * 
834
	 * @param Localization $model
835
	 * @param mixed $relatedId
836
	 */
837
	protected function doSetLanguageId(Localization $model, $relatedId) {
838
		if ($model->getLanguageId() !== $relatedId) {
839
			$model->setLanguageId($relatedId);
840
841
			return true;
842
		}
843
844
		return false;
845
	}
846
847
	/**
848
	 * Internal mechanism to set the Parent id
849
	 * 
850
	 * @param Localization $model
851
	 * @param mixed $relatedId
852
	 */
853
	protected function doSetParentId(Localization $model, $relatedId) {
854
		if ($model->getParentId() !== $relatedId) {
855
			$model->setParentId($relatedId);
856
857
			return true;
858
		}
859
860
		return false;
861
	}
862
863
	/**
864
	 * Internal mechanism to set the Script id
865
	 * 
866
	 * @param Localization $model
867
	 * @param mixed $relatedId
868
	 */
869
	protected function doSetScriptId(Localization $model, $relatedId) {
870
		if ($model->getScriptId() !== $relatedId) {
871
			$model->setScriptId($relatedId);
872
873
			return true;
874
		}
875
876
		return false;
877
	}
878
879
	/**
880
	 * Internal update mechanism of ApplicationUris on Localization
881
	 * 
882
	 * @param Localization $model
883
	 * @param mixed $data
884
	 */
885
	protected function doUpdateApplicationUris(Localization $model, $data) {
886
		// remove all relationships before
887
		ApplicationUriQuery::create()->filterByLocalization($model)->delete();
888
889
		// add them
890
		$errors = [];
891
		foreach ($data as $entry) {
892
			if (!isset($entry['id'])) {
893
				$errors[] = 'Missing id for ApplicationUri';
894
			} else {
895
				$related = ApplicationUriQuery::create()->findOneById($entry['id']);
896
				$model->addApplicationUri($related);
897
			}
898
		}
899
900
		if (count($errors) > 0) {
901
			throw new ErrorsException($errors);
902
		}
903
	}
904
905
	/**
906
	 * Internal update mechanism of LanguageVariants on Localization
907
	 * 
908
	 * @param Localization $model
909
	 * @param mixed $data
910
	 */
911
	protected function doUpdateLanguageVariants(Localization $model, $data) {
912
		// remove all relationships before
913
		LocalizationVariantQuery::create()->filterByLocalization($model)->delete();
914
915
		// add them
916
		$errors = [];
917
		foreach ($data as $entry) {
918
			if (!isset($entry['id'])) {
919
				$errors[] = 'Missing id for LanguageVariant';
920
			} else {
921
				$related = LanguageVariantQuery::create()->findOneById($entry['id']);
922
				$model->addLanguageVariant($related);
923
			}
924
		}
925
926
		if (count($errors) > 0) {
927
			throw new ErrorsException($errors);
928
		}
929
	}
930
931
	/**
932
	 * Internal update mechanism of Localizations on Localization
933
	 * 
934
	 * @param Localization $model
935
	 * @param mixed $data
936
	 */
937
	protected function doUpdateLocalizations(Localization $model, $data) {
938
		// remove all relationships before
939
		LocalizationQuery::create()->filterByParent($model)->delete();
940
941
		// add them
942
		$errors = [];
943
		foreach ($data as $entry) {
944
			if (!isset($entry['id'])) {
945
				$errors[] = 'Missing id for Localization';
946
			} else {
947
				$related = LocalizationQuery::create()->findOneById($entry['id']);
948
				$model->addLocalization($related);
949
			}
950
		}
951
952
		if (count($errors) > 0) {
953
			throw new ErrorsException($errors);
954
		}
955
	}
956
957
	/**
958
	 * Returns one Localization with the given id from cache
959
	 * 
960
	 * @param mixed $id
961
	 * @return Localization|null
962
	 */
963
	protected function get($id) {
964
		if ($this->pool === null) {
965
			$this->pool = new Map();
966
		} else if ($this->pool->has($id)) {
967
			return $this->pool->get($id);
968
		}
969
970
		$model = LocalizationQuery::create()->findOneById($id);
971
		$this->pool->set($id, $model);
972
973
		return $model;
974
	}
975
976
	/**
977
	 * Returns the service container
978
	 * 
979
	 * @return ServiceContainer
980
	 */
981
	abstract protected function getServiceContainer();
982
}
983