PermissionModel   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 77
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assignPermission() 0 12 3
A assignPermissions() 0 10 3
A removePermission() 0 15 2
A removePermissions() 0 10 3
1
<?php
2
3
namespace Omatech\Mage\Core\Domains\Permissions;
4
5
use Omatech\Mage\Core\Domains\Permissions\Contracts\PermissionInterface;
6
use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionIsNotSavedException;
7
8
class PermissionModel
9
{
10
    /**
11
     * @param array $currentPermissions
12
     * @param PermissionInterface $assignPermission
13
     * @return array
14
     * @throws PermissionIsNotSavedException
15
     */
16 15
    public function assignPermission(array $currentPermissions, PermissionInterface $assignPermission): array
17
    {
18 15
        if (null === $assignPermission->getId()) {
19 2
            throw new PermissionIsNotSavedException();
20
        }
21
22 13
        if (! in_array($assignPermission, $currentPermissions, true)) {
23 13
            $currentPermissions[] = $assignPermission;
24
        }
25
26 13
        return $currentPermissions;
27
    }
28
29
    /**
30
     * @param array $currentPermissions
31
     * @param array $assignPermissions
32
     * @return array
33
     * @throws PermissionIsNotSavedException
34
     */
35 14
    public function assignPermissions(array $currentPermissions, array $assignPermissions): array
36
    {
37 14
        foreach ($assignPermissions as $permission) {
38 12
            if ($permission instanceof PermissionInterface) {
39 12
                $currentPermissions = $this->assignPermission($currentPermissions, $permission);
40
            }
41
        }
42
43 12
        return $currentPermissions;
44
    }
45
46
    /**
47
     * @param array $currentPermissions
48
     * @param PermissionInterface $deletePermission
49
     * @return array
50
     * @throws PermissionIsNotSavedException
51
     */
52 6
    public function removePermission(array $currentPermissions, PermissionInterface $deletePermission): array
53
    {
54 6
        if (null === $deletePermission->getId()) {
55 2
            throw new PermissionIsNotSavedException();
56
        }
57
58 4
        $currentPermissions = array_values(array_filter(
59 4
            $currentPermissions,
60
            static function ($currentPermission) use ($deletePermission) {
61 4
                return $currentPermission !== $deletePermission;
62 4
            }
63
        ));
64
65 4
        return $currentPermissions;
66
    }
67
68
    /**
69
     * @param array $currentPermissions
70
     * @param array $deletePermissions
71
     * @return array
72
     * @throws PermissionIsNotSavedException
73
     */
74 4
    public function removePermissions(array $currentPermissions, array $deletePermissions): array
75
    {
76 4
        foreach ($deletePermissions as $permission) {
77 4
            if ($permission instanceof PermissionInterface) {
78 4
                $currentPermissions = $this->removePermission($currentPermissions, $permission);
79
            }
80
        }
81
82 2
        return $currentPermissions;
83
    }
84
}
85