Passed
Push — master ( 880107...7e2e0f )
by Christian
03:08
created

RoleModel::removeRoles()   A

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\Roles;
4
5
use Omatech\Mage\Core\Domains\Roles\Contracts\RoleInterface;
6
use Omatech\Mage\Core\Domains\Roles\Exceptions\RoleIsNotSavedException;
7
8
class RoleModel
9
{
10 7
    public function assignRole(array $currentRoles, RoleInterface $assignRole): array
11
    {
12 7
        if ($assignRole->getId() === null) {
13 1
            throw new RoleIsNotSavedException;
14
        }
15
16 6
        if (!in_array($assignRole, $currentRoles, true)) {
17 6
            $currentRoles[] = $assignRole;
18
        }
19
20 6
        return $currentRoles;
21
    }
22
23 7
    public function assignRoles(array $currentRoles, array $assignRoles): array
24
    {
25 7
        foreach ($assignRoles as $role) {
26 6
            if ($role instanceof RoleInterface) {
27 6
                $currentRoles = $this->assignRole($currentRoles, $role);
28
            }
29
        }
30
31 6
        return $currentRoles;
32
    }
33
34 3
    public function removeRole(array $currentRoles, RoleInterface $deleteRole): array
35
    {
36 3
        if ($deleteRole->getId() === null) {
37 1
            throw new RoleIsNotSavedException;
38
        }
39
40 2
        $currentRoles = array_values(array_filter(
41 2
            $currentRoles,
42
            static function ($currentRole) use ($deleteRole) {
43 2
                return $currentRole !== $deleteRole;
44 2
            }
45
        ));
46
47 2
        return $currentRoles;
48
    }
49
50 2
    public function removeRoles(array $currentRoles, array $deleteRoles): array
51
    {
52 2
        foreach ($deleteRoles as $role) {
53 2
            if ($role instanceof RoleInterface) {
54 2
                $currentRoles = $this->removeRole($currentRoles, $role);
55
            }
56
        }
57
58 1
        return $currentRoles;
59
    }
60
}
61