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

RoleModel   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 assignRole() 0 12 3
A assignRoles() 0 10 3
A removeRole() 0 15 2
A removeRoles() 0 10 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