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
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @todo extend to allow matching by visibility, depth, ... |
11
|
|
|
* @todo extend to allow matching on multiple conditions (AND) |
12
|
|
|
*/ |
13
|
|
|
class LocationMatcher extends AbstractMatcher |
14
|
|
|
{ |
15
|
|
|
const MATCH_CONTENT_ID = 'content_id'; |
16
|
|
|
const MATCH_LOCATION_ID = 'location_id'; |
17
|
|
|
const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
18
|
|
|
const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
19
|
|
|
const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
20
|
|
|
const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
21
|
|
|
|
22
|
|
|
protected $allowedConditions = array( |
23
|
|
|
self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
24
|
|
|
self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID |
25
|
|
|
); |
26
|
|
|
protected $returns = 'Location'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
30
|
|
|
* @return LocationCollection |
31
|
|
|
*/ |
32
|
|
|
public function match(array $conditions) |
33
|
|
|
{ |
34
|
|
|
return $this->matchLocation($conditions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $conditions key: condition, value: value: int / string / int[] / string[] |
39
|
|
|
* @return LocationCollection |
40
|
|
|
*/ |
41
|
|
|
public function matchLocation(array $conditions) |
42
|
|
|
{ |
43
|
|
|
foreach ($conditions as $key => $values) { |
44
|
|
|
|
45
|
|
|
if (!is_array($values)) { |
46
|
|
|
$values = array($values); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
switch ($key) { |
50
|
|
|
case self::MATCH_CONTENT_ID: |
51
|
|
|
return new LocationCollection($this->findLocationsByContentIds($values)); |
52
|
|
|
|
53
|
|
|
case self::MATCH_LOCATION_ID: |
54
|
|
|
return new LocationCollection($this->findLocationsByLocationIds($values)); |
55
|
|
|
|
56
|
|
|
case self::MATCH_CONTENT_REMOTE_ID: |
57
|
|
|
return new LocationCollection($this->findLocationsByContentRemoteIds($values)); |
58
|
|
|
|
59
|
|
|
case self::MATCH_LOCATION_REMOTE_ID: |
60
|
|
|
return new LocationCollection($this->findLocationsByLocationRemoteIds($values)); |
61
|
|
|
|
62
|
|
|
case self::MATCH_PARENT_LOCATION_ID: |
63
|
|
|
return new LocationCollection($this->findLocationsByParentLocationIds($values)); |
64
|
|
|
|
65
|
|
|
case self::MATCH_PARENT_LOCATION_REMOTE_ID: |
66
|
|
|
return new LocationCollection($this->findLocationsByParentLocationRemoteIds($values)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns all locations of a set of objects |
73
|
|
|
* |
74
|
|
|
* @param int[] $contentIds |
75
|
|
|
* @return Location[] |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
protected function findLocationsByContentIds(array $contentIds) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$locations = []; |
80
|
|
|
|
81
|
|
|
foreach ($contentIds as $contentId) { |
82
|
|
|
$content = $this->repository->getContentService()->loadContent($contentId); |
83
|
|
|
$locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $locations; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns all locations of a set of objects |
91
|
|
|
* |
92
|
|
|
* @param int[] $remoteContentIds |
93
|
|
|
* @return Location[] |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
protected function findLocationsByContentRemoteIds(array $remoteContentIds) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$locations = []; |
98
|
|
|
|
99
|
|
|
foreach ($remoteContentIds as $remoteContentId) { |
100
|
|
|
$content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId); |
101
|
|
|
$locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $locations; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param int[] $locationIds |
109
|
|
|
* @return Location[] |
110
|
|
|
*/ |
111
|
|
|
protected function findLocationsByLocationIds(array $locationIds) |
112
|
|
|
{ |
113
|
|
|
$locations = []; |
114
|
|
|
|
115
|
|
|
foreach ($locationIds as $locationId) { |
116
|
|
|
$locations[] = $this->repository->getLocationService()->loadLocation($locationId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $locations; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int[] $locationRemoteIds |
124
|
|
|
* @return Location[] |
125
|
|
|
*/ |
126
|
|
|
protected function findLocationsByLocationRemoteIds($locationRemoteIds) |
127
|
|
|
{ |
128
|
|
|
$locations = []; |
129
|
|
|
|
130
|
|
|
foreach ($locationRemoteIds as $locationRemoteId) { |
131
|
|
|
$locations[] = $this->repository->getLocationService()->loadLocationByRemoteId($locationRemoteId); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $locations; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int[] $parentLocationIds |
139
|
|
|
* @return Location[] |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
protected function findLocationsByParentLocationIds($parentLocationIds) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$query = new LocationQuery(); |
144
|
|
|
$query->limit = PHP_INT_MAX; |
145
|
|
|
$query->filter = new Query\Criterion\ParentLocationId($parentLocationIds); |
146
|
|
|
|
147
|
|
|
$results = $this->repository->getSearchService()->findLocations($query); |
148
|
|
|
|
149
|
|
|
$locations = []; |
150
|
|
|
|
151
|
|
|
foreach ($results->searchHits as $result) { |
152
|
|
|
$locations[] = $result->valueObject; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $locations; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int[] $parentLocationRemoteIds |
160
|
|
|
* @return Location[] |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$locationIds = []; |
165
|
|
|
|
166
|
|
|
foreach ($parentLocationRemoteIds as $parentLocationRemoteId) { |
167
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($parentLocationRemoteId); |
168
|
|
|
$locationIds[] = $location->id; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->findLocationsByParentLocationIds($locationIds); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.