Completed
Push — master ( 8c5037...eed80c )
by Gaetano
07:22
created

RoleMatcher   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 119
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 119
loc 119
c 0
b 0
f 0
wmc 21
lcom 1
cbo 6
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 4 4 1
C matchRole() 33 33 11
A getConditionsFromKey() 7 7 3
A findRolesById() 12 12 2
A findRolesByIdentifier() 12 12 2
A findAllRoles() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
9 View Code Duplication
class RoleMatcher extends RepositoryMatcher implements KeyMatcherInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    use FlexibleKeyMatcherTrait;
12
13
    const MATCH_ROLE_ID = 'role_id';
14
    const MATCH_ROLE_IDENTIFIER = 'role_identifier';
15
16
    protected $allowedConditions = array(
17
        self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
18
        self::MATCH_ROLE_ID, self::MATCH_ROLE_IDENTIFIER,
19
        // aliases
20
        'id', 'identifier'
21
    );
22
    protected $returns = 'Role';
23
24
    /**
25
     * @param array $conditions key: condition, value: int / string / int[] / string[]
26
     * @return RoleCollection
27
     */
28
    public function match(array $conditions)
29
    {
30
        return $this->matchRole($conditions);
31
    }
32
33
    /**
34
     * @param array $conditions key: condition, value: int / string / int[] / string[]
35
     * @return RoleCollection
36
     */
37
    public function matchRole(array $conditions)
38
    {
39
        $this->validateConditions($conditions);
40
41
        foreach ($conditions as $key => $values) {
42
43
            if (!is_array($values)) {
44
                $values = array($values);
45
            }
46
47
            switch ($key) {
48
                case 'id':
49
                case self::MATCH_ROLE_ID:
50
                   return new RoleCollection($this->findRolesById($values));
51
52
                case 'identifier':
53
                case self::MATCH_ROLE_IDENTIFIER:
54
                    return new RoleCollection($this->findRolesByIdentifier($values));
55
56
                case self::MATCH_ALL:
57
                    return new RoleCollection($this->findAllRoles());
58
59
                case self::MATCH_AND:
60
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchAnd($values); of type object|array adds the type array to the return on line 60 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\RoleMatcher::matchRole of type Kaliop\eZMigrationBundle...ion\RoleCollection|null.
Loading history...
61
62
                case self::MATCH_OR:
63
                    return $this->matchOr($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchOr($values); of type object|array adds the type array to the return on line 63 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...\RoleMatcher::matchRole of type Kaliop\eZMigrationBundle...ion\RoleCollection|null.
Loading history...
64
65
                case self::MATCH_NOT:
66
                    return new RoleCollection(array_diff_key($this->findAllRoles(), $this->matchRole($values)->getArrayCopy()));
67
            }
68
        }
69
    }
70
71
    protected function getConditionsFromKey($key)
72
    {
73
        if (is_int($key) || ctype_digit($key)) {
74
            return array(self::MATCH_ROLE_ID => $key);
75
        }
76
        return array(self::MATCH_ROLE_IDENTIFIER => $key);
77
    }
78
79
    /**
80
     * @param int[] $roleIds
81
     * @return Role[]
82
     */
83
    protected function findRolesById(array $roleIds)
84
    {
85
        $roles = [];
86
87
        foreach ($roleIds as $roleId) {
88
            // return unique contents
89
            $role = $this->repository->getRoleService()->loadRole($roleId);
90
            $roles[$role->id] = $role;
91
        }
92
93
        return $roles;
94
    }
95
96
    /**
97
     * @param string[] $roleIdentifiers
98
     * @return Role[]
99
     */
100
    protected function findRolesByIdentifier(array $roleIdentifiers)
101
    {
102
        $roles = [];
103
104
        foreach ($roleIdentifiers as $roleIdentifier) {
105
            // return unique contents
106
            $role = $this->repository->getRoleService()->loadRoleByIdentifier($roleIdentifier);
107
            $roles[$role->id] = $role;
108
        }
109
110
        return $roles;
111
    }
112
113
    /**
114
     * @return Role[]
115
     */
116
    protected function findAllRoles()
117
    {
118
        $roles = [];
119
120
        foreach ($this->repository->getRoleService()->loadRoles() as $role) {
121
            // return unique contents
122
            $roles[$role->id] = $role;
123
        }
124
125
        return $roles;
126
    }
127
}
128