Passed
Push — master ( 342071...ce1cac )
by Christian
04:33
created

UpdateOrCreatePermission::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\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 22
    public function __construct()
27
    {
28 22
        $this->exists = app()->make(ExistsPermission::class);
29 22
        $this->create = app()->make(CreatePermission::class);
30 22
        $this->update = app()->make(UpdatePermission::class);
31 22
        $this->unique = app()->make(UniquePermission::class);
32 22
    }
33
34
    /**
35
     * @param Permission $permission
36
     *
37
     * @throws PermissionAlreadyExistsException
38
     * @throws PermissionNameExistsMustBeUniqueException
39
     * @throws PermissionDoesNotExistsException
40
     *
41
     * @return bool
42
     */
43 22
    public function make(Permission $permission): bool
44
    {
45 22
        if (null !== $permission->getId()) {
46 3
            return $this->update($permission);
47
        }
48
49 22
        return $this->create($permission);
50
    }
51
52
    /**
53
     * @param Permission $permission
54
     *
55
     * @throws PermissionAlreadyExistsException
56
     *
57
     * @return bool
58
     */
59 22
    private function create(Permission $permission): bool
60
    {
61 22
        $exists = $this->exists->make($permission);
62
63 22
        if (true === $exists) {
64 1
            throw new PermissionAlreadyExistsException();
65
        }
66
67 22
        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 3
    private function update(Permission $permission): bool
79
    {
80 3
        $exists = $this->unique->make($permission);
81
82 3
        if (true === $exists) {
83 1
            throw new PermissionNameExistsMustBeUniqueException();
84
        }
85
86 2
        $exists = $this->exists->make($permission);
87
88 2
        if (false === $exists) {
89 1
            throw new PermissionDoesNotExistsException();
90
        }
91
92 1
        return $this->update->make($permission);
93
    }
94
}
95