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

UpdateOrCreatePermission::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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