1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Domains\Translations\Features; |
4
|
|
|
|
5
|
|
|
use Omatech\Mage\Core\Domains\Translations\Translation; |
6
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\CreateTranslation; |
7
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\ExistsTranslation; |
8
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\UniqueTranslation; |
9
|
|
|
use Omatech\Mage\Core\Domains\Translations\Jobs\UpdateTranslation; |
10
|
|
|
use Omatech\Mage\Core\Domains\Translations\Exceptions\TranslationAlreadyExistsException; |
11
|
|
|
use Omatech\Mage\Core\Domains\Translations\Exceptions\TranslationDoesNotExistsException; |
12
|
|
|
use Omatech\Mage\Core\Domains\Translations\Exceptions\TranslationExistsMustBeUniqueException; |
13
|
|
|
|
14
|
|
|
class UpdateOrCreateTranslation |
15
|
|
|
{ |
16
|
|
|
private $exists; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* UpdateOrCreateTranslation constructor. |
20
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
21
|
|
|
*/ |
22
|
9 |
|
public function __construct() |
23
|
|
|
{ |
24
|
9 |
|
$this->create = app()->make(CreateTranslation::class); |
|
|
|
|
25
|
9 |
|
$this->update = app()->make(UpdateTranslation::class); |
|
|
|
|
26
|
9 |
|
$this->exists = app()->make(ExistsTranslation::class); |
27
|
9 |
|
$this->unique = app()->make(UniqueTranslation::class); |
|
|
|
|
28
|
9 |
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Translation $translation |
33
|
|
|
* @return bool |
34
|
|
|
* @throws TranslationAlreadyExistsException |
35
|
|
|
* @throws TranslationDoesNotExistsException |
36
|
|
|
* @throws TranslationExistsMustBeUniqueException |
37
|
|
|
*/ |
38
|
9 |
|
public function make(Translation $translation): bool |
39
|
|
|
{ |
40
|
9 |
|
if ($translation->getId() !== null) { |
41
|
3 |
|
return $this->update($translation); |
42
|
|
|
} |
43
|
|
|
|
44
|
9 |
|
return $this->create($translation); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Translation $translation |
49
|
|
|
* @return bool |
50
|
|
|
* @throws TranslationAlreadyExistsException |
51
|
|
|
*/ |
52
|
9 |
|
private function create(Translation $translation): bool |
53
|
|
|
{ |
54
|
9 |
|
$exists = $this->exists->make($translation); |
55
|
|
|
|
56
|
9 |
|
if ($exists === true) { |
57
|
1 |
|
throw new TranslationAlreadyExistsException; |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
return $this->create->make($translation); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Translation $translation |
65
|
|
|
* @return bool |
66
|
|
|
* @throws TranslationDoesNotExistsException |
67
|
|
|
* @throws TranslationExistsMustBeUniqueException |
68
|
|
|
*/ |
69
|
3 |
|
private function update(Translation $translation): bool |
70
|
|
|
{ |
71
|
3 |
|
$exists = $this->unique->make($translation); |
72
|
|
|
|
73
|
3 |
|
if ($exists === true) { |
74
|
1 |
|
throw new TranslationExistsMustBeUniqueException; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
$exists = $this->exists->make($translation); |
78
|
|
|
|
79
|
2 |
|
if ($exists === false) { |
80
|
1 |
|
throw new TranslationDoesNotExistsException; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return $this->update->make($translation); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|