1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection; |
7
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationQuery; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
9
|
|
|
|
10
|
|
|
class LocationMatcher extends QueryBasedMatcher |
11
|
|
|
{ |
12
|
|
|
use FlexibleKeyMatcherTrait; |
13
|
|
|
|
14
|
|
|
const MATCH_DEPTH = 'depth'; |
15
|
|
|
const MATCH_IS_MAIN_LOCATION = 'is_main_location'; |
16
|
|
|
const MATCH_PRIORITY = 'priority'; |
17
|
|
|
|
18
|
|
|
protected $allowedConditions = array( |
19
|
|
|
self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
20
|
|
|
self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
21
|
|
|
self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP, |
22
|
|
|
self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER, |
23
|
|
|
self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE, |
24
|
|
|
self::MATCH_VISIBILITY, |
25
|
|
|
// aliases |
26
|
|
|
'content_type', 'content_type_id', 'content_type_identifier', |
27
|
|
|
// location-only |
28
|
|
|
self::MATCH_DEPTH, self::MATCH_IS_MAIN_LOCATION, self::MATCH_PRIORITY, |
29
|
|
|
); |
30
|
|
|
protected $returns = 'Location'; |
31
|
|
|
|
32
|
1 |
|
/** |
33
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
34
|
1 |
|
* @return LocationCollection |
35
|
|
|
*/ |
36
|
|
|
public function match(array $conditions) |
37
|
|
|
{ |
38
|
|
|
return $this->matchLocation($conditions); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
/** |
42
|
|
|
* @param array $conditions key: condition, value: value: int / string / int[] / string[] |
43
|
1 |
|
* @return LocationCollection |
44
|
|
|
*/ |
45
|
1 |
|
public function matchLocation(array $conditions) |
46
|
|
|
{ |
47
|
1 |
|
$this->validateConditions($conditions); |
48
|
1 |
|
|
49
|
1 |
View Code Duplication |
if (count($conditions) === 1) { |
|
|
|
|
50
|
|
|
$condition = reset($conditions); |
51
|
|
|
if (is_int($condition) || ctype_digit($condition)) { |
52
|
1 |
|
return $this->repository->getLocationService()->loadLocation($condition); |
|
|
|
|
53
|
|
|
} elseif (is_string($condition)) { |
54
|
|
|
return $this->repository->getLocationService()->loadLocationByRemoteId($condition); |
|
|
|
|
55
|
1 |
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
foreach ($conditions as $key => $values) { |
59
|
|
|
|
60
|
|
|
$query = new LocationQuery(); |
61
|
1 |
|
$query->limit = self::INT_MAX_16BIT; |
62
|
1 |
|
if (isset($query->performCount)) $query->performCount = false; |
63
|
|
|
$query->filter = $this->getQueryCriterion($key, $values); |
64
|
|
|
$results = $this->repository->getSearchService()->findLocations($query); |
65
|
|
|
|
66
|
|
|
$locations = []; |
67
|
|
|
foreach ($results->searchHits as $result) { |
68
|
|
|
$locations[$result->valueObject->id] = $result->valueObject; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return new LocationCollection($locations); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* When matching by key, we accept location Id and remote Id only |
77
|
|
|
* @param int|string $key |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function getConditionsFromKey($key) |
81
|
|
|
{ |
82
|
|
|
if (is_int($key) || ctype_digit($key)) { |
83
|
|
|
return array(self::MATCH_LOCATION_ID => $key); |
84
|
|
|
} |
85
|
|
|
return array(self::MATCH_LOCATION_REMOTE_ID => $key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getQueryCriterion($key, $values) |
89
|
|
|
{ |
90
|
|
|
if (!is_array($values)) { |
91
|
|
|
$values = array($values); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
switch ($key) { |
95
|
|
View Code Duplication |
case self::MATCH_DEPTH: |
|
|
|
|
96
|
|
|
$match = reset($values); |
97
|
|
|
$operator = key($values); |
98
|
|
|
if (!isset(self::$operatorsMap[$operator])) { |
99
|
|
|
throw new \Exception("Can not use '$operator' as comparison operator for depth"); |
100
|
|
|
} |
101
|
|
|
return new Query\Criterion\Location\Depth(self::$operatorsMap[$operator], $match); |
|
|
|
|
102
|
|
|
|
103
|
|
|
case self::MATCH_IS_MAIN_LOCATION: |
104
|
|
|
case 'is_main': |
|
|
|
|
105
|
|
|
/// @todo error/warning if there is more than 1 value... |
106
|
|
|
$value = reset($values); |
107
|
|
|
if ($value) { |
108
|
|
|
return new Query\Criterion\Location\IsMainLocation(Query\Criterion\Location\IsMainLocation::MAIN); |
|
|
|
|
109
|
|
|
} else { |
110
|
|
|
return new Query\Criterion\Location\IsMainLocation(Query\Criterion\Location\IsMainLocation::NOT_MAIN); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
View Code Duplication |
case self::MATCH_PRIORITY: |
|
|
|
|
114
|
|
|
$match = reset($values); |
115
|
1 |
|
$operator = key($values); |
116
|
|
|
if (!isset(self::$operatorsMap[$operator])) { |
117
|
1 |
|
throw new \Exception("Can not use '$operator' as comparison operator for depth"); |
118
|
1 |
|
} |
119
|
1 |
|
return new Query\Criterion\Location\Priority(self::$operatorsMap[$operator], $match); |
|
|
|
|
120
|
|
|
} |
121
|
1 |
|
|
122
|
|
|
return parent::getQueryCriterion($key, $values); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns all locations of a set of objects |
127
|
|
|
* |
128
|
1 |
|
* @param int[] $contentIds |
129
|
|
|
* @return Location[] |
130
|
1 |
|
* @deprecated |
131
|
|
|
*/ |
132
|
1 |
View Code Duplication |
protected function findLocationsByContentIds(array $contentIds) |
|
|
|
|
133
|
1 |
|
{ |
134
|
1 |
|
$locations = []; |
135
|
|
|
|
136
|
1 |
|
foreach ($contentIds as $contentId) { |
137
|
|
|
$content = $this->repository->getContentService()->loadContent($contentId); |
138
|
|
|
foreach($this->repository->getLocationService()->loadLocations($content->contentInfo) as $location) { |
139
|
|
|
$locations[$location->id] = $location; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $locations; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns all locations of a set of objects |
148
|
|
|
* |
149
|
|
|
* @param int[] $remoteContentIds |
150
|
|
|
* @return Location[] |
151
|
|
|
* @deprecated |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$locations = []; |
156
|
|
|
|
157
|
|
|
foreach ($remoteContentIds as $remoteContentId) { |
158
|
|
|
$content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId); |
159
|
|
|
foreach($this->repository->getLocationService()->loadLocations($content->contentInfo) as $location) { |
160
|
|
|
$locations[$location->id] = $location; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $locations; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int[] $locationIds |
169
|
|
|
* @return Location[] |
170
|
|
|
* @deprecated |
171
|
|
|
*/ |
172
|
|
|
protected function findLocationsByLocationIds(array $locationIds) |
173
|
|
|
{ |
174
|
|
|
$locations = []; |
175
|
|
|
|
176
|
|
|
foreach ($locationIds as $locationId) { |
177
|
|
|
$locations[$locationId] = $this->repository->getLocationService()->loadLocation($locationId); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $locations; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param int[] $locationRemoteIds |
185
|
|
|
* @return Location[] |
186
|
|
|
* @deprecated |
187
|
|
|
*/ |
188
|
|
|
protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
189
|
|
|
{ |
190
|
|
|
$locations = []; |
191
|
|
|
|
192
|
|
|
foreach ($locationRemoteIds as $locationRemoteId) { |
193
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($locationRemoteId); |
194
|
|
|
$locations[$location->id] = $location; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $locations; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param int[] $parentLocationIds |
202
|
|
|
* @return Location[] |
203
|
|
|
* @deprecated |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
protected function findLocationsByParentLocationIds($parentLocationIds) |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
$query = new LocationQuery(); |
208
|
|
|
$query->limit = self::INT_MAX_16BIT; |
209
|
|
|
if (isset($query->performCount)) $query->performCount = false; |
210
|
|
|
$query->filter = new Query\Criterion\ParentLocationId($parentLocationIds); |
211
|
|
|
|
212
|
|
|
$results = $this->repository->getSearchService()->findLocations($query); |
213
|
|
|
|
214
|
|
|
$locations = []; |
215
|
|
|
|
216
|
|
|
foreach ($results->searchHits as $result) { |
217
|
|
|
$locations[$result->valueObject->id] = $result->valueObject; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $locations; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param int[] $parentLocationRemoteIds |
225
|
|
|
* @return Location[] |
226
|
|
|
* @deprecated |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$locationIds = []; |
231
|
|
|
|
232
|
|
|
foreach ($parentLocationRemoteIds as $parentLocationRemoteId) { |
233
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($parentLocationRemoteId); |
234
|
|
|
$locationIds[$location->id] = $location->id; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->findLocationsByParentLocationIds($locationIds); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
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.