Test Failed
Push — master ( da5def...342071 )
by Christian
04:54
created

UpdateOrCreateTranslation::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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