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

PermissionModel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
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 53
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 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