Passed
Push — master ( 2c144a...cc2b65 )
by Christian
03:20
created

UpdateOrCreatePermission   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 66
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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