|
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 |
|
|
|
|
|
|
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, |
|
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
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getConditionsFromKey($key) |
|
63
|
|
|
{ |
|
64
|
|
|
if (is_int($key) || ctype_digit($key)) { |
|
65
|
|
|
return array(self::MATCH_ROLE_ID => $key); |
|
66
|
|
|
} |
|
67
|
|
|
return array(self::MATCH_ROLE_IDENTIFIER => $key); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param int[] $roleIds |
|
72
|
|
|
* @return Role[] |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function findRolesById(array $roleIds) |
|
75
|
|
|
{ |
|
76
|
|
|
$roles = []; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($roleIds as $roleId) { |
|
79
|
|
|
// return unique contents |
|
80
|
|
|
$role = $this->repository->getRoleService()->loadRole($roleId); |
|
81
|
|
|
$roles[$role->id] = $role; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $roles; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string[] $roleIdentifiers |
|
89
|
|
|
* @return Role[] |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function findRolesByIdentifier(array $roleIdentifiers) |
|
92
|
|
|
{ |
|
93
|
|
|
$roles = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($roleIdentifiers as $roleIdentifier) { |
|
96
|
|
|
// return unique contents |
|
97
|
|
|
$role = $this->repository->getRoleService()->loadRoleByIdentifier($roleIdentifier); |
|
98
|
|
|
$roles[$role->id] = $role; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $roles; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return Role[] |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function findAllRoles() |
|
108
|
|
|
{ |
|
109
|
|
|
$roles = []; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->repository->getRoleService()->loadRoles() as $role) { |
|
112
|
|
|
// return unique contents |
|
113
|
|
|
$roles[$role->id] = $role; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $roles; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.