Completed
Push — master ( 79fd8a...81e819 )
by Christian
05:05
created

UpdateOrCreateTranslation::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
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);
0 ignored issues
show
Bug Best Practice introduced by
The property create does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25 9
        $this->update = app()->make(UpdateTranslation::class);
0 ignored issues
show
Bug Best Practice introduced by
The property update does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26 9
        $this->exists = app()->make(ExistsTranslation::class);
27 9
        $this->unique = app()->make(UniqueTranslation::class);
0 ignored issues
show
Bug Best Practice introduced by
The property unique does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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