Test Failed
Push — master ( d7824a...a03b1d )
by Christian
06:13 queued 18s
created

UpdateOrCreateRole   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 71
ccs 23
cts 23
cp 1
rs 10
wmc 8

4 Methods

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