Passed
Pull Request — master (#225)
by
unknown
04:29
created

RoleMatcher::findRolesByIdentifier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\User\Role;
6
use Kaliop\eZMigrationBundle\API\Collection\RoleCollection;
7
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface;
8
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException;
9
10
class RoleMatcher extends RepositoryMatcher implements KeyMatcherInterface
11
{
12
    use FlexibleKeyMatcherTrait;
13
14
    const MATCH_ROLE_ID = 'role_id';
15
    const MATCH_ROLE_IDENTIFIER = 'role_identifier';
16
17
    protected $allowedConditions = array(
18
        self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
19
        self::MATCH_ROLE_ID, self::MATCH_ROLE_IDENTIFIER,
20
        // aliases
21
        'id', 'identifier'
22
    );
23
    protected $returns = 'Role';
24
25
    /**
26
     * @param array $conditions key: condition, value: int / string / int[] / string[]
27
     * @return RoleCollection
28
     * @throws InvalidMatchConditionsException
29
     */
30
    public function match(array $conditions)
31
    {
32
        return $this->matchRole($conditions);
33
    }
34
35
    /**
36
     * @param array $conditions key: condition, value: int / string / int[] / string[]
37
     * @return RoleCollection
38
     * @throws InvalidMatchConditionsException
39
     */
40
    public function matchRole(array $conditions)
41
    {
42
        $this->validateConditions($conditions);
43
44
        foreach ($conditions as $key => $values) {
45
46
            if (!is_array($values)) {
47
                $values = array($values);
48
            }
49
50
            switch ($key) {
51
                case 'id':
52
                case self::MATCH_ROLE_ID:
53
                   return new RoleCollection($this->findRolesById($values));
54
55
                case 'identifier':
56
                case self::MATCH_ROLE_IDENTIFIER:
57
                    return new RoleCollection($this->findRolesByIdentifier($values));
58
59
                case self::MATCH_ALL:
60
                    return new RoleCollection($this->findAllRoles());
61
62
                case self::MATCH_AND:
63
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchAnd($values) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...llection\RoleCollection.
Loading history...
64
65
                case self::MATCH_OR:
66
                    return $this->matchOr($values);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->matchOr($values) returns the type array which is incompatible with the documented return type Kaliop\eZMigrationBundle...llection\RoleCollection.
Loading history...
67
68
                case self::MATCH_NOT:
69
                    return new RoleCollection(array_diff_key($this->findAllRoles(), $this->matchRole($values)->getArrayCopy()));
70
            }
71
        }
72
    }
73
74
    protected function getConditionsFromKey($key)
75
    {
76
        if (is_int($key) || ctype_digit($key)) {
77
            return array(self::MATCH_ROLE_ID => $key);
78
        }
79
        return array(self::MATCH_ROLE_IDENTIFIER => $key);
80
    }
81
82
    /**
83
     * @param int[] $roleIds
84
     * @return Role[]
85
     */
86
    protected function findRolesById(array $roleIds)
87
    {
88
        $roles = [];
89
90
        foreach ($roleIds as $roleId) {
91
            // return unique contents
92
            $role = $this->repository->getRoleService()->loadRole($roleId);
93
            $roles[$role->id] = $role;
94
        }
95
96
        return $roles;
97
    }
98
99
    /**
100
     * @param string[] $roleIdentifiers
101
     * @return Role[]
102
     */
103
    protected function findRolesByIdentifier(array $roleIdentifiers)
104
    {
105
        $roles = [];
106
107
        foreach ($roleIdentifiers as $roleIdentifier) {
108
            // return unique contents
109
            $role = $this->repository->getRoleService()->loadRoleByIdentifier($roleIdentifier);
110
            $roles[$role->id] = $role;
111
        }
112
113
        return $roles;
114
    }
115
116
    /**
117
     * @return Role[]
118
     */
119
    protected function findAllRoles()
120
    {
121
        $roles = [];
122
123
        foreach ($this->repository->getRoleService()->loadRoles() as $role) {
124
            // return unique contents
125
            $roles[$role->id] = $role;
126
        }
127
128
        return $roles;
129
    }
130
}
131