|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Publish\Core\Repository; |
|
8
|
|
|
|
|
9
|
|
|
use DateTime; |
|
10
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
|
11
|
|
|
use eZ\Publish\API\Repository\Repository as RepositoryInterface; |
|
12
|
|
|
use eZ\Publish\API\Repository\URLService as URLServiceInterface; |
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
|
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion as ContentCriterion; |
|
15
|
|
|
use eZ\Publish\API\Repository\Values\URL\SearchResult; |
|
16
|
|
|
use eZ\Publish\API\Repository\Values\URL\URL; |
|
17
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLQuery; |
|
18
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct; |
|
19
|
|
|
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult; |
|
20
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
21
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue; |
|
22
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
|
23
|
|
|
use eZ\Publish\SPI\Persistence\URL\Handler as URLHandler; |
|
24
|
|
|
use eZ\Publish\SPI\Persistence\URL\URL as SPIUrl; |
|
25
|
|
|
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct as SPIUrlUpdateStruct; |
|
26
|
|
|
|
|
27
|
|
|
class URLService implements URLServiceInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \eZ\Publish\Core\Repository\Repository |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $repository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \eZ\Publish\SPI\Persistence\URL\Handler |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $urlHandler; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* URLService constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
|
43
|
|
|
* @param \eZ\Publish\SPI\Persistence\URL\Handler $urlHandler |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(RepositoryInterface $repository, URLHandler $urlHandler) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->repository = $repository; |
|
|
|
|
|
|
48
|
|
|
$this->urlHandler = $urlHandler; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function findUrls(URLQuery $query) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->repository->hasAccess('url', 'view') !== true) { |
|
|
|
|
|
|
57
|
|
|
throw new UnauthorizedException('url', 'view'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
if ($query->offset !== null && !is_numeric($query->offset)) { |
|
|
|
|
|
|
61
|
|
|
throw new InvalidArgumentValue('offset', $query->offset); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
if ($query->limit !== null && !is_numeric($query->limit)) { |
|
|
|
|
|
|
65
|
|
|
throw new InvalidArgumentValue('limit', $query->limit); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$results = $this->urlHandler->find($query); |
|
69
|
|
|
|
|
70
|
|
|
$items = []; |
|
71
|
|
|
foreach ($results['items'] as $url) { |
|
72
|
|
|
$items[] = $this->buildDomainObject($url); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return new SearchResult([ |
|
76
|
|
|
'totalCount' => $results['count'], |
|
77
|
|
|
'items' => $items, |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
View Code Duplication |
public function updateUrl(URL $url, URLUpdateStruct $struct) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->repository->hasAccess('url', 'update') !== true) { |
|
|
|
|
|
|
87
|
|
|
throw new UnauthorizedException('url', 'update'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!$this->isUnique($url->id, $struct->url)) { |
|
|
|
|
|
|
91
|
|
|
throw new InvalidArgumentException('struct', 'url already exists'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$updateStruct = $this->buildUpdateStruct($this->loadById($url->id), $struct); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$this->repository->beginTransaction(); |
|
97
|
|
|
try { |
|
98
|
|
|
$this->urlHandler->updateUrl($url->id, $updateStruct); |
|
|
|
|
|
|
99
|
|
|
$this->repository->commit(); |
|
100
|
|
|
} catch (\Exception $e) { |
|
101
|
|
|
$this->repository->rollback(); |
|
102
|
|
|
throw $e; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->loadById($url->id); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
View Code Duplication |
public function loadById($id) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($this->repository->hasAccess('url', 'view') !== true) { |
|
|
|
|
|
|
114
|
|
|
throw new UnauthorizedException('url', 'view'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->buildDomainObject( |
|
118
|
|
|
$this->urlHandler->loadById($id) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function loadByUrl($url) |
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->repository->hasAccess('url', 'view') !== true) { |
|
|
|
|
|
|
128
|
|
|
throw new UnauthorizedException('url', 'view'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->buildDomainObject( |
|
132
|
|
|
$this->urlHandler->loadByUrl($url) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
|
|
public function createUpdateStruct() |
|
140
|
|
|
{ |
|
141
|
|
|
return new URLUpdateStruct(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
|
|
public function findUsages(URL $url, $offset = 0, $limit = -1) |
|
148
|
|
|
{ |
|
149
|
|
|
$contentIds = $this->urlHandler->findUsages($url->id); |
|
|
|
|
|
|
150
|
|
|
if (empty($contentIds)) { |
|
151
|
|
|
return new UsageSearchResult(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$query = new Query(); |
|
155
|
|
|
$query->filter = new ContentCriterion\LogicalAnd([ |
|
156
|
|
|
new ContentCriterion\ContentId($contentIds), |
|
157
|
|
|
new ContentCriterion\Visibility(ContentCriterion\Visibility::VISIBLE), |
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
$query->offset = $offset; |
|
161
|
|
|
if ($limit > -1) { |
|
162
|
|
|
$query->limit = $limit; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$searchResults = $this->repository->getSearchService()->findContentInfo($query); |
|
166
|
|
|
|
|
167
|
|
|
$usageResults = new UsageSearchResult(); |
|
168
|
|
|
$usageResults->totalCount = $searchResults->totalCount; |
|
169
|
|
|
foreach ($searchResults->searchHits as $hit) { |
|
170
|
|
|
$usageResults->items[] = $hit->valueObject; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $usageResults; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Builds domain object from ValueObject returned by Persistence API. |
|
178
|
|
|
* |
|
179
|
|
|
* @param \eZ\Publish\SPI\Persistence\URL\URL $data |
|
180
|
|
|
* @return \eZ\Publish\API\Repository\Values\URL\URL |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function buildDomainObject(SPIUrl $data) |
|
183
|
|
|
{ |
|
184
|
|
|
return new URL([ |
|
185
|
|
|
'id' => $data->id, |
|
186
|
|
|
'url' => $data->url, |
|
187
|
|
|
'isValid' => $data->isValid, |
|
188
|
|
|
'lastChecked' => $this->createDateTime($data->lastChecked), |
|
189
|
|
|
'created' => $this->createDateTime($data->created), |
|
190
|
|
|
'modified' => $this->createDateTime($data->modified), |
|
191
|
|
|
]); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Builds SPI update structure used by Persistence API. |
|
196
|
|
|
* |
|
197
|
|
|
* @param \eZ\Publish\API\Repository\Values\URL\URL $url |
|
198
|
|
|
* @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $data |
|
199
|
|
|
* @return \eZ\Publish\SPI\Persistence\URL\URLUpdateStruct |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function buildUpdateStruct(URL $url, URLUpdateStruct $data) |
|
202
|
|
|
{ |
|
203
|
|
|
$updateStruct = new SPIUrlUpdateStruct(); |
|
204
|
|
|
|
|
205
|
|
|
if ($data->url !== null && $url->url !== $data->url) { |
|
|
|
|
|
|
206
|
|
|
$updateStruct->url = $data->url; |
|
207
|
|
|
// Reset URL validity |
|
208
|
|
|
$updateStruct->lastChecked = 0; |
|
209
|
|
|
$updateStruct->isValid = true; |
|
210
|
|
|
} else { |
|
211
|
|
|
$updateStruct->url = $url->url; |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
if ($data->lastChecked !== null) { |
|
214
|
|
|
$updateStruct->lastChecked = $data->lastChecked->getTimestamp(); |
|
215
|
|
|
} elseif ($data->lastChecked !== null) { |
|
216
|
|
|
$updateStruct->lastChecked = $url->lastChecked->getTimestamp(); |
|
|
|
|
|
|
217
|
|
|
} else { |
|
218
|
|
|
$updateStruct->lastChecked = 0; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if ($data->isValid !== null) { |
|
222
|
|
|
$updateStruct->isValid = $data->isValid; |
|
223
|
|
|
} else { |
|
224
|
|
|
$updateStruct->isValid = $url->isValid; |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $updateStruct; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Check if URL is unique. |
|
233
|
|
|
* |
|
234
|
|
|
* @param int $id |
|
235
|
|
|
* @param string $url |
|
236
|
|
|
* @return bool |
|
237
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function isUnique($id, $url) |
|
240
|
|
|
{ |
|
241
|
|
|
try { |
|
242
|
|
|
return $this->loadByUrl($url)->id === $id; |
|
|
|
|
|
|
243
|
|
|
} catch (NotFoundException $e) { |
|
244
|
|
|
return true; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
private function createDateTime($timestamp) |
|
249
|
|
|
{ |
|
250
|
|
|
if ($timestamp > 0) { |
|
251
|
|
|
return new DateTime("@{$timestamp}"); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return null; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.