1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\ContentCollection; |
8
|
|
|
|
9
|
|
|
class ContentMatcher extends AbstractMatcher |
10
|
|
|
{ |
11
|
|
|
const MATCH_CONTENT_ID = 'content_id'; |
12
|
|
|
const MATCH_LOCATION_ID = 'location_id'; |
13
|
|
|
const MATCH_CONTENT_REMOTE_ID = 'content_remote_id'; |
14
|
|
|
const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
15
|
|
|
const MATCH_PARENT_LOCATION_ID = 'parent_location_id'; |
16
|
|
|
const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id'; |
17
|
|
|
const MATCH_CONTENT_TYPE_IDENTIFIER = 'content_type'; |
18
|
|
|
|
19
|
|
|
protected $allowedConditions = array( |
20
|
|
|
self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID, |
21
|
|
|
self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER |
22
|
|
|
); |
23
|
|
|
protected $returns = 'Content'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
27
|
|
|
* @return ContentCollection |
28
|
|
|
*/ |
29
|
|
|
public function matchContent(array $conditions) |
30
|
|
|
{ |
31
|
|
|
$conditions = $this->validateConditions($conditions); |
|
|
|
|
32
|
|
|
|
33
|
|
|
foreach ($conditions as $key => $values) { |
|
|
|
|
34
|
|
|
|
35
|
|
|
if (!is_array($values)) { |
36
|
|
|
$values = array($values); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
switch ($key) { |
40
|
|
|
case self::MATCH_CONTENT_ID: |
41
|
|
|
return new ContentCollection($this->findContentsByContentIds($values)); |
42
|
|
|
|
43
|
|
|
case self::MATCH_LOCATION_ID: |
44
|
|
|
return new ContentCollection($this->findContentsByLocationIds($values)); |
45
|
|
|
|
46
|
|
|
case self::MATCH_CONTENT_REMOTE_ID: |
47
|
|
|
return new ContentCollection($this->findContentsByContentRemoteIds($values)); |
48
|
|
|
|
49
|
|
|
case self::MATCH_LOCATION_REMOTE_ID: |
50
|
|
|
return new ContentCollection($this->findContentsByLocationRemoteIds($values)); |
51
|
|
|
|
52
|
|
|
case self::MATCH_PARENT_LOCATION_ID: |
53
|
|
|
return new ContentCollection($this->findContentsByParentLocationIds($values)); |
54
|
|
|
|
55
|
|
|
case self::MATCH_PARENT_LOCATION_REMOTE_ID: |
56
|
|
|
return new ContentCollection($this->findContentsByParentLocationRemoteIds($values)); |
57
|
|
|
|
58
|
|
|
case self::MATCH_CONTENT_TYPE_IDENTIFIER: |
59
|
|
|
return new ContentCollection($this->findContentsByContentTypeIdentifiers($values)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int[] $contentIds |
67
|
|
|
* @return Content[] |
68
|
|
|
*/ |
69
|
|
|
protected function findContentsByContentIds(array $contentIds) |
70
|
|
|
{ |
71
|
|
|
$contents = []; |
72
|
|
|
|
73
|
|
|
foreach ($contentIds as $contentId) { |
74
|
|
|
$contents[] = $this->repository->getContentService()->loadContent($contentId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $contents; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string[] $remoteContentIds |
82
|
|
|
* @return Content[] |
83
|
|
|
*/ |
84
|
|
|
protected function findContentsByContentRemoteIds(array $remoteContentIds) |
85
|
|
|
{ |
86
|
|
|
$contents = []; |
87
|
|
|
|
88
|
|
|
foreach ($remoteContentIds as $remoteContentId) { |
89
|
|
|
$contents[] = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $contents; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int[] $locationIds |
97
|
|
|
* @return Content[] |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
protected function findContentsByLocationIds(array $locationIds) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$contentIds = []; |
102
|
|
|
|
103
|
|
|
foreach ($locationIds as $locationId) { |
104
|
|
|
$location = $this->repository->getLocationService()->loadLocation($locationId); |
105
|
|
|
$contentIds[] = $location->contentId; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->findContentsByContentIds($contentIds); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string[] $remoteLocationIds |
113
|
|
|
* @return Content[] |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
protected function findContentsByLocationRemoteIds($remoteLocationIds) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$contentIds = []; |
118
|
|
|
|
119
|
|
|
foreach ($remoteLocationIds as $remoteLocationId) { |
120
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteLocationId); |
121
|
|
|
$contentIds[] = $location->contentId; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->findContentsByContentIds($contentIds); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int[] $parentLocationIds |
129
|
|
|
* @return Content[] |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
protected function findContentsByParentLocationIds($parentLocationIds) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$query = new Query(); |
134
|
|
|
$query->limit = PHP_INT_MAX; |
135
|
|
|
$query->filter = new Query\Criterion\ParentLocationId($parentLocationIds); |
136
|
|
|
$results = $this->repository->getSearchService()->findContent($query); |
137
|
|
|
|
138
|
|
|
$contents = []; |
139
|
|
|
foreach ($results->searchHits as $result) { |
140
|
|
|
$contents[] = $result->valueObject; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $contents; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string[] $remoteParentLocationIds |
148
|
|
|
* @return Content[] |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$locationIds = []; |
153
|
|
|
|
154
|
|
|
foreach ($remoteParentLocationIds as $remoteParentLocationId) { |
155
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteParentLocationId); |
156
|
|
|
$locationIds[] = $location->id; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->findContentsByParentLocationIds($locationIds); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string[] $contentTypeIdentifiers |
164
|
|
|
* @return Content[] |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$query = new Query(); |
169
|
|
|
$query->limit = PHP_INT_MAX; |
170
|
|
|
$query->filter = new Query\Criterion\ContentTypeIdentifier($contentTypeIdentifiers); |
171
|
|
|
$results = $this->repository->getSearchService()->findContent($query); |
172
|
|
|
|
173
|
|
|
$contents = []; |
174
|
|
|
foreach ($results->searchHits as $result) { |
175
|
|
|
$contents[] = $result->valueObject; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $contents; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.