UpdateOrCreatePermission   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 73
ccs 23
cts 23
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 16 3
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 21
    public function __construct()
25
    {
26 21
        $this->exists = new ExistsPermission();
27 21
        $this->create = new CreatePermission();
28 21
        $this->update = new UpdatePermission();
29 21
        $this->unique = new UniquePermission();
30 21
    }
31
32
    /**
33
     * @param Permission $permission
34
     * @return bool
35
     * @throws PermissionAlreadyExistsException
36
     * @throws PermissionDoesNotExistsException
37
     * @throws PermissionNameExistsMustBeUniqueException
38
     */
39 21
    public function make(Permission $permission): bool
40
    {
41 21
        if (null !== $permission->getId()) {
42 3
            return $this->update($permission);
43
        }
44
45 21
        return $this->create($permission);
46
    }
47
48
    /**
49
     * @param Permission $permission
50
     * @return bool
51
     * @throws PermissionAlreadyExistsException
52
     */
53 21
    private function create(Permission $permission): bool
54
    {
55 21
        $exists = $this->exists->make($permission);
56
57 21
        if (true === $exists) {
58 1
            throw new PermissionAlreadyExistsException();
59
        }
60
61 21
        return $this->create->make($permission);
62
    }
63
64
    /**
65
     * @param Permission $permission
66
     * @return bool
67
     * @throws PermissionDoesNotExistsException
68
     * @throws PermissionNameExistsMustBeUniqueException
69
     */
70 3
    private function update(Permission $permission): bool
71
    {
72 3
        $exists = $this->unique->make($permission);
73
74 3
        if (true === $exists) {
75 1
            throw new PermissionNameExistsMustBeUniqueException();
76
        }
77
78 2
        $exists = $this->exists->make($permission);
79
80 2
        if (false === $exists) {
81 1
            throw new PermissionDoesNotExistsException();
82
        }
83
84 1
        return $this->update->make($permission);
85
    }
86
}
87