PermissionModel::assignPermissions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 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