Passed
Push — master ( 34de7b...2b2a7c )
by Christian
03:02
created

PermissionModel::assignPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
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 6
    public function assignPermission(array $currentPermissions, PermissionInterface $assignPermission): array
11
    {
12 6
        if ($assignPermission->getId() === null) {
13 1
            throw new PermissionIsNotSavedException;
14
        }
15
16 5
        if (!in_array($assignPermission, $currentPermissions, true)) {
17 5
            $currentPermissions[] = $assignPermission;
18
        }
19
20 5
        return $currentPermissions;
21
    }
22
23 7
    public function assignPermissions(array $currentPermissions, array $assignPermissions): array
24
    {
25 7
        foreach ($assignPermissions as $permission) {
26 6
            if ($permission instanceof PermissionInterface) {
27 6
                $currentPermissions = $this->assignPermission($currentPermissions, $permission);
28
            }
29
        }
30
31 6
        return $currentPermissions;
32
    }
33
34 2
    public function removePermission(array $currentPermissions, PermissionInterface $deletePermission): array
35
    {
36 2
        if ($deletePermission->getId() === null) {
37 1
            throw new PermissionIsNotSavedException;
38
        }
39
40 1
        $currentPermissions = array_values(array_filter(
41 1
            $currentPermissions,
42
            static function ($currentPermission) use ($deletePermission) {
43 1
                return $currentPermission !== $deletePermission;
44 1
            }
45
        ));
46
47 1
        return $currentPermissions;
48
    }
49
50 2
    public function removePermissions(array $currentPermissions, array $deletePermissions): array
51
    {
52 2
        foreach ($deletePermissions as $permission) {
53 2
            if ($permission instanceof PermissionInterface) {
54 2
                $currentPermissions = $this->removePermission($currentPermissions, $permission);
55
            }
56
        }
57
58 1
        return $currentPermissions;
59
    }
60
}
61