Complex classes like ContentService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class ContentService implements ContentServiceInterface |
||
59 | { |
||
60 | /** @var \eZ\Publish\Core\Repository\Repository */ |
||
61 | protected $repository; |
||
62 | |||
63 | /** @var \eZ\Publish\SPI\Persistence\Handler */ |
||
64 | protected $persistenceHandler; |
||
65 | |||
66 | /** @var array */ |
||
67 | protected $settings; |
||
68 | |||
69 | /** @var \eZ\Publish\Core\Repository\Helper\DomainMapper */ |
||
70 | protected $domainMapper; |
||
71 | |||
72 | /** @var \eZ\Publish\Core\Repository\Helper\RelationProcessor */ |
||
73 | protected $relationProcessor; |
||
74 | |||
75 | /** @var \eZ\Publish\Core\Repository\Helper\NameSchemaService */ |
||
76 | protected $nameSchemaService; |
||
77 | |||
78 | /** @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry */ |
||
79 | protected $fieldTypeRegistry; |
||
80 | |||
81 | /** |
||
82 | * Setups service with reference to repository object that created it & corresponding handler. |
||
83 | * |
||
84 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
85 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
86 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
87 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
88 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
89 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|||
90 | * @param array $settings |
||
91 | */ |
||
92 | public function __construct( |
||
93 | RepositoryInterface $repository, |
||
94 | Handler $handler, |
||
95 | Helper\DomainMapper $domainMapper, |
||
96 | Helper\RelationProcessor $relationProcessor, |
||
97 | Helper\NameSchemaService $nameSchemaService, |
||
98 | Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
99 | array $settings = [] |
||
100 | ) { |
||
101 | $this->repository = $repository; |
||
102 | $this->persistenceHandler = $handler; |
||
103 | $this->domainMapper = $domainMapper; |
||
104 | $this->relationProcessor = $relationProcessor; |
||
105 | $this->nameSchemaService = $nameSchemaService; |
||
106 | $this->fieldTypeRegistry = $fieldTypeRegistry; |
||
107 | // Union makes sure default settings are ignored if provided in argument |
||
108 | $this->settings = $settings + [ |
||
109 | // Version archive limit (0-50), only enforced on publish, not on un-publish. |
||
110 | 'default_version_archive_limit' => 5, |
||
111 | ]; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Loads a content info object. |
||
116 | * |
||
117 | * To load fields use loadContent |
||
118 | * |
||
119 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
120 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
121 | * |
||
122 | * @param int $contentId |
||
123 | * |
||
124 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
125 | */ |
||
126 | public function loadContentInfo($contentId) |
||
127 | { |
||
128 | $contentInfo = $this->internalLoadContentInfo($contentId); |
||
129 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
130 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
131 | } |
||
132 | |||
133 | return $contentInfo; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function loadContentInfoList(array $contentIds): iterable |
||
140 | { |
||
141 | $contentInfoList = []; |
||
142 | $spiInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds); |
||
143 | foreach ($spiInfoList as $id => $spiInfo) { |
||
144 | $contentInfo = $this->domainMapper->buildContentInfoDomainObject($spiInfo); |
||
145 | if ($this->repository->canUser('content', 'read', $contentInfo)) { |
||
146 | $contentInfoList[$id] = $contentInfo; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return $contentInfoList; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Loads a content info object. |
||
155 | * |
||
156 | * To load fields use loadContent |
||
157 | * |
||
158 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
159 | * |
||
160 | * @param mixed $id |
||
161 | * @param bool $isRemoteId |
||
162 | * |
||
163 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
164 | */ |
||
165 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
166 | { |
||
167 | try { |
||
168 | $method = $isRemoteId ? 'loadContentInfoByRemoteId' : 'loadContentInfo'; |
||
169 | |||
170 | return $this->domainMapper->buildContentInfoDomainObject( |
||
171 | $this->persistenceHandler->contentHandler()->$method($id) |
||
172 | ); |
||
173 | } catch (APINotFoundException $e) { |
||
174 | throw new NotFoundException( |
||
175 | 'Content', |
||
176 | $id, |
||
177 | $e |
||
178 | ); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Loads a content info object for the given remoteId. |
||
184 | * |
||
185 | * To load fields use loadContent |
||
186 | * |
||
187 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
188 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
189 | * |
||
190 | * @param string $remoteId |
||
191 | * |
||
192 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
193 | */ |
||
194 | public function loadContentInfoByRemoteId($remoteId) |
||
195 | { |
||
196 | $contentInfo = $this->internalLoadContentInfo($remoteId, true); |
||
197 | |||
198 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
199 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
200 | } |
||
201 | |||
202 | return $contentInfo; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Loads a version info of the given content object. |
||
207 | * |
||
208 | * If no version number is given, the method returns the current version |
||
209 | * |
||
210 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
211 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
212 | * |
||
213 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
214 | * @param int $versionNo the version number. If not given the current version is returned. |
||
215 | * |
||
216 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
217 | */ |
||
218 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
219 | { |
||
220 | return $this->loadVersionInfoById($contentInfo->id, $versionNo); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Loads a version info of the given content object id. |
||
225 | * |
||
226 | * If no version number is given, the method returns the current version |
||
227 | * |
||
228 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
229 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
230 | * |
||
231 | * @param mixed $contentId |
||
232 | * @param int $versionNo the version number. If not given the current version is returned. |
||
233 | * |
||
234 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
235 | */ |
||
236 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
237 | { |
||
238 | try { |
||
239 | $spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo( |
||
240 | $contentId, |
||
241 | $versionNo |
||
242 | ); |
||
243 | } catch (APINotFoundException $e) { |
||
244 | throw new NotFoundException( |
||
245 | 'VersionInfo', |
||
246 | [ |
||
247 | 'contentId' => $contentId, |
||
248 | 'versionNo' => $versionNo, |
||
249 | ], |
||
250 | $e |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo); |
||
255 | |||
256 | if ($versionInfo->isPublished()) { |
||
257 | $function = 'read'; |
||
258 | } else { |
||
259 | $function = 'versionread'; |
||
260 | } |
||
261 | |||
262 | if (!$this->repository->canUser('content', $function, $versionInfo)) { |
||
263 | throw new UnauthorizedException('content', $function, ['contentId' => $contentId]); |
||
264 | } |
||
265 | |||
266 | return $versionInfo; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
273 | { |
||
274 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
275 | if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) { |
||
276 | $useAlwaysAvailable = false; |
||
277 | } |
||
278 | |||
279 | return $this->loadContent( |
||
280 | $contentInfo->id, |
||
281 | $languages, |
||
282 | $versionNo,// On purpose pass as-is and not use $contentInfo, to make sure to return actual current version on null |
||
283 | $useAlwaysAvailable |
||
284 | ); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
291 | { |
||
292 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
293 | if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) { |
||
294 | $useAlwaysAvailable = false; |
||
295 | } |
||
296 | |||
297 | return $this->loadContent( |
||
298 | $versionInfo->getContentInfo()->id, |
||
299 | $languages, |
||
300 | $versionInfo->versionNo, |
||
301 | $useAlwaysAvailable |
||
302 | ); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
309 | { |
||
310 | $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable); |
||
311 | |||
312 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
313 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
314 | } |
||
315 | if ( |
||
316 | !$content->getVersionInfo()->isPublished() |
||
317 | && !$this->repository->canUser('content', 'versionread', $content) |
||
318 | ) { |
||
319 | throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId, 'versionNo' => $versionNo]); |
||
320 | } |
||
321 | |||
322 | return $content; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Loads content in a version of the given content object. |
||
327 | * |
||
328 | * If no version number is given, the method returns the current version |
||
329 | * |
||
330 | * @internal |
||
331 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
332 | * |
||
333 | * @param mixed $id |
||
334 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
335 | * returned value object. If not given all languages are returned. |
||
336 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
337 | * @param bool $isRemoteId |
||
338 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
339 | * |
||
340 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
341 | */ |
||
342 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
343 | { |
||
344 | try { |
||
345 | // Get Content ID if lookup by remote ID |
||
346 | if ($isRemoteId) { |
||
347 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id); |
||
348 | $id = $spiContentInfo->id; |
||
349 | // Set $isRemoteId to false as the next loads will be for content id now that we have it (for exception use now) |
||
350 | $isRemoteId = false; |
||
351 | } |
||
352 | |||
353 | $loadLanguages = $languages; |
||
354 | $alwaysAvailableLanguageCode = null; |
||
355 | // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true |
||
356 | // @todo Move use always available logic to SPI load methods, like done in location handler in 7.x |
||
357 | if (!empty($loadLanguages) && $useAlwaysAvailable) { |
||
358 | if (!isset($spiContentInfo)) { |
||
359 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); |
||
360 | } |
||
361 | |||
362 | if ($spiContentInfo->alwaysAvailable) { |
||
363 | $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; |
||
364 | $loadLanguages = array_unique($loadLanguages); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | $spiContent = $this->persistenceHandler->contentHandler()->load( |
||
369 | $id, |
||
370 | $versionNo, |
||
371 | $loadLanguages |
||
372 | ); |
||
373 | } catch (APINotFoundException $e) { |
||
374 | throw new NotFoundException( |
||
375 | 'Content', |
||
376 | [ |
||
377 | $isRemoteId ? 'remoteId' : 'id' => $id, |
||
378 | 'languages' => $languages, |
||
379 | 'versionNo' => $versionNo, |
||
380 | ], |
||
381 | $e |
||
382 | ); |
||
383 | } |
||
384 | |||
385 | if ($languages === null) { |
||
386 | $languages = []; |
||
387 | } |
||
388 | |||
389 | return $this->domainMapper->buildContentDomainObject( |
||
390 | $spiContent, |
||
391 | $this->repository->getContentTypeService()->loadContentType( |
||
392 | $spiContent->versionInfo->contentInfo->contentTypeId, |
||
393 | $languages |
||
394 | ), |
||
395 | $languages, |
||
396 | $alwaysAvailableLanguageCode |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Loads content in a version for the content object reference by the given remote id. |
||
402 | * |
||
403 | * If no version is given, the method returns the current version |
||
404 | * |
||
405 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
406 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions |
||
407 | * |
||
408 | * @param string $remoteId |
||
409 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
410 | * @param int $versionNo the version number. If not given the current version is returned |
||
411 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
412 | * |
||
413 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
414 | */ |
||
415 | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
416 | { |
||
417 | $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable); |
||
418 | |||
419 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
420 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
421 | } |
||
422 | |||
423 | if ( |
||
424 | !$content->getVersionInfo()->isPublished() |
||
425 | && !$this->repository->canUser('content', 'versionread', $content) |
||
426 | ) { |
||
427 | throw new UnauthorizedException('content', 'versionread', ['remoteId' => $remoteId, 'versionNo' => $versionNo]); |
||
428 | } |
||
429 | |||
430 | return $content; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Bulk-load Content items by the list of ContentInfo Value Objects. |
||
435 | * |
||
436 | * Note: it does not throw exceptions on load, just ignores erroneous Content item. |
||
437 | * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is |
||
438 | * allowed to access every Content on the list. |
||
439 | * |
||
440 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList |
||
441 | * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on |
||
442 | * returned value object. If not given all languages are returned. |
||
443 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true, |
||
444 | * unless all languages have been asked for. |
||
445 | * |
||
446 | * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys |
||
447 | */ |
||
448 | public function loadContentListByContentInfo( |
||
449 | array $contentInfoList, |
||
450 | array $languages = [], |
||
451 | $useAlwaysAvailable = true |
||
452 | ) { |
||
453 | $loadAllLanguages = $languages === Language::ALL; |
||
454 | $contentIds = []; |
||
455 | $contentTypeIds = []; |
||
456 | $translations = $languages; |
||
457 | foreach ($contentInfoList as $contentInfo) { |
||
458 | $contentIds[] = $contentInfo->id; |
||
459 | $contentTypeIds[] = $contentInfo->contentTypeId; |
||
460 | // Unless we are told to load all languages, we add main language to translations so they are loaded too |
||
461 | // Might in some case load more languages then intended, but prioritised handling will pick right one |
||
462 | if (!$loadAllLanguages && $useAlwaysAvailable && $contentInfo->alwaysAvailable) { |
||
463 | $translations[] = $contentInfo->mainLanguageCode; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | $contentList = []; |
||
468 | $translations = array_unique($translations); |
||
469 | $spiContentList = $this->persistenceHandler->contentHandler()->loadContentList( |
||
470 | $contentIds, |
||
471 | $translations |
||
472 | ); |
||
473 | $contentTypeList = $this->repository->getContentTypeService()->loadContentTypeList( |
||
474 | array_unique($contentTypeIds), |
||
475 | $languages |
||
476 | ); |
||
477 | foreach ($spiContentList as $contentId => $spiContent) { |
||
478 | $contentInfo = $spiContent->versionInfo->contentInfo; |
||
479 | $contentList[$contentId] = $this->domainMapper->buildContentDomainObject( |
||
480 | $spiContent, |
||
481 | $contentTypeList[$contentInfo->contentTypeId], |
||
482 | $languages, |
||
483 | $contentInfo->alwaysAvailable ? $contentInfo->mainLanguageCode : null |
||
484 | ); |
||
485 | } |
||
486 | |||
487 | return $contentList; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Creates a new content draft assigned to the authenticated user. |
||
492 | * |
||
493 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
494 | * but this required special rights for the authenticated user |
||
495 | * (this is useful for content staging where the transfer process does not |
||
496 | * have to authenticate with the user which created the content object in the source server). |
||
497 | * The user has to publish the draft if it should be visible. |
||
498 | * In 4.x at least one location has to be provided in the location creation array. |
||
499 | * |
||
500 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
501 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
502 | * struct are missing or invalid, or if multiple locations are under the |
||
503 | * same parent. |
||
504 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
505 | * or if a required field is missing / set to an empty value. |
||
506 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
507 | * or value is set for non-translatable field in language |
||
508 | * other than main. |
||
509 | * |
||
510 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
511 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
512 | * |
||
513 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
514 | */ |
||
515 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) |
||
516 | { |
||
517 | if ($contentCreateStruct->mainLanguageCode === null) { |
||
518 | throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); |
||
519 | } |
||
520 | |||
521 | if ($contentCreateStruct->contentType === null) { |
||
522 | throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set"); |
||
523 | } |
||
524 | |||
525 | $contentCreateStruct = clone $contentCreateStruct; |
||
526 | |||
527 | if ($contentCreateStruct->ownerId === null) { |
||
528 | $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId(); |
||
529 | } |
||
530 | |||
531 | if ($contentCreateStruct->alwaysAvailable === null) { |
||
532 | $contentCreateStruct->alwaysAvailable = $contentCreateStruct->contentType->defaultAlwaysAvailable ?: false; |
||
533 | } |
||
534 | |||
535 | $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType( |
||
536 | $contentCreateStruct->contentType->id |
||
537 | ); |
||
538 | |||
539 | if (empty($contentCreateStruct->sectionId)) { |
||
540 | if (isset($locationCreateStructs[0])) { |
||
541 | $location = $this->repository->getLocationService()->loadLocation( |
||
542 | $locationCreateStructs[0]->parentLocationId |
||
543 | ); |
||
544 | $contentCreateStruct->sectionId = $location->contentInfo->sectionId; |
||
545 | } else { |
||
546 | $contentCreateStruct->sectionId = 1; |
||
547 | } |
||
548 | } |
||
549 | |||
550 | if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) { |
||
551 | throw new UnauthorizedException( |
||
552 | 'content', |
||
553 | 'create', |
||
554 | [ |
||
555 | 'parentLocationId' => isset($locationCreateStructs[0]) ? |
||
556 | $locationCreateStructs[0]->parentLocationId : |
||
557 | null, |
||
558 | 'sectionId' => $contentCreateStruct->sectionId, |
||
559 | ] |
||
560 | ); |
||
561 | } |
||
562 | |||
563 | if (!empty($contentCreateStruct->remoteId)) { |
||
564 | try { |
||
565 | $this->loadContentByRemoteId($contentCreateStruct->remoteId); |
||
566 | |||
567 | throw new InvalidArgumentException( |
||
568 | '$contentCreateStruct', |
||
569 | "Another content with remoteId '{$contentCreateStruct->remoteId}' exists" |
||
570 | ); |
||
571 | } catch (APINotFoundException $e) { |
||
572 | // Do nothing |
||
573 | } |
||
574 | } else { |
||
575 | $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct); |
||
576 | } |
||
577 | |||
578 | $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs); |
||
579 | |||
580 | $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct); |
||
581 | $fields = $this->mapFieldsForCreate($contentCreateStruct); |
||
582 | |||
583 | $fieldValues = []; |
||
584 | $spiFields = []; |
||
585 | $allFieldErrors = []; |
||
586 | $inputRelations = []; |
||
587 | $locationIdToContentIdMapping = []; |
||
588 | |||
589 | foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
||
590 | /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */ |
||
591 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
592 | $fieldDefinition->fieldTypeIdentifier |
||
593 | ); |
||
594 | |||
595 | foreach ($languageCodes as $languageCode) { |
||
596 | $isEmptyValue = false; |
||
597 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode; |
||
598 | $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode; |
||
599 | if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) { |
||
600 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
601 | } else { |
||
602 | $fieldValue = $fieldDefinition->defaultValue; |
||
603 | } |
||
604 | |||
605 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
606 | |||
607 | if ($fieldType->isEmptyValue($fieldValue)) { |
||
608 | $isEmptyValue = true; |
||
609 | if ($fieldDefinition->isRequired) { |
||
610 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
611 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
612 | null, |
||
613 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
614 | 'empty' |
||
615 | ); |
||
616 | } |
||
617 | } else { |
||
618 | $fieldErrors = $fieldType->validate( |
||
619 | $fieldDefinition, |
||
620 | $fieldValue |
||
621 | ); |
||
622 | if (!empty($fieldErrors)) { |
||
623 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
624 | } |
||
625 | } |
||
626 | |||
627 | if (!empty($allFieldErrors)) { |
||
628 | continue; |
||
629 | } |
||
630 | |||
631 | $this->relationProcessor->appendFieldRelations( |
||
632 | $inputRelations, |
||
633 | $locationIdToContentIdMapping, |
||
634 | $fieldType, |
||
635 | $fieldValue, |
||
636 | $fieldDefinition->id |
||
637 | ); |
||
638 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
639 | |||
640 | // Only non-empty value for: translatable field or in main language |
||
641 | if ( |
||
642 | (!$isEmptyValue && $fieldDefinition->isTranslatable) || |
||
643 | (!$isEmptyValue && $isLanguageMain) |
||
644 | ) { |
||
645 | $spiFields[] = new SPIField( |
||
646 | [ |
||
647 | 'id' => null, |
||
648 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
649 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
650 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
651 | 'languageCode' => $languageCode, |
||
652 | 'versionNo' => null, |
||
653 | ] |
||
654 | ); |
||
655 | } |
||
656 | } |
||
657 | } |
||
658 | |||
659 | if (!empty($allFieldErrors)) { |
||
660 | throw new ContentFieldValidationException($allFieldErrors); |
||
661 | } |
||
662 | |||
663 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
664 | [ |
||
665 | 'name' => $this->nameSchemaService->resolve( |
||
666 | $contentCreateStruct->contentType->nameSchema, |
||
667 | $contentCreateStruct->contentType, |
||
668 | $fieldValues, |
||
669 | $languageCodes |
||
670 | ), |
||
671 | 'typeId' => $contentCreateStruct->contentType->id, |
||
672 | 'sectionId' => $contentCreateStruct->sectionId, |
||
673 | 'ownerId' => $contentCreateStruct->ownerId, |
||
674 | 'locations' => $spiLocationCreateStructs, |
||
675 | 'fields' => $spiFields, |
||
676 | 'alwaysAvailable' => $contentCreateStruct->alwaysAvailable, |
||
677 | 'remoteId' => $contentCreateStruct->remoteId, |
||
678 | 'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), |
||
679 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
680 | $contentCreateStruct->mainLanguageCode |
||
681 | )->id, |
||
682 | ] |
||
683 | ); |
||
684 | |||
685 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
686 | |||
687 | $this->repository->beginTransaction(); |
||
688 | try { |
||
689 | $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct); |
||
690 | $this->relationProcessor->processFieldRelations( |
||
691 | $inputRelations, |
||
692 | $spiContent->versionInfo->contentInfo->id, |
||
693 | $spiContent->versionInfo->versionNo, |
||
694 | $contentCreateStruct->contentType |
||
695 | ); |
||
696 | |||
697 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
698 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
699 | $objectStateHandler->setContentState( |
||
700 | $spiContent->versionInfo->contentInfo->id, |
||
701 | $objectStateGroupId, |
||
702 | $objectState->id |
||
703 | ); |
||
704 | } |
||
705 | |||
706 | $this->repository->commit(); |
||
707 | } catch (Exception $e) { |
||
708 | $this->repository->rollback(); |
||
709 | throw $e; |
||
710 | } |
||
711 | |||
712 | return $this->domainMapper->buildContentDomainObject( |
||
713 | $spiContent, |
||
714 | $contentCreateStruct->contentType |
||
715 | ); |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * Returns an array of default content states with content state group id as key. |
||
720 | * |
||
721 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
722 | */ |
||
723 | protected function getDefaultObjectStates() |
||
724 | { |
||
725 | $defaultObjectStatesMap = []; |
||
726 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
727 | |||
728 | foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) { |
||
729 | foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) { |
||
730 | // Only register the first object state which is the default one. |
||
731 | $defaultObjectStatesMap[$objectStateGroup->id] = $objectState; |
||
732 | break; |
||
733 | } |
||
734 | } |
||
735 | |||
736 | return $defaultObjectStatesMap; |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Returns all language codes used in given $fields. |
||
741 | * |
||
742 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
743 | * |
||
744 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
745 | * |
||
746 | * @return string[] |
||
747 | */ |
||
748 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
749 | { |
||
750 | $languageCodes = []; |
||
751 | |||
752 | foreach ($contentCreateStruct->fields as $field) { |
||
753 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
754 | continue; |
||
755 | } |
||
756 | |||
757 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
758 | $field->languageCode |
||
759 | ); |
||
760 | $languageCodes[$field->languageCode] = true; |
||
761 | } |
||
762 | |||
763 | if (!isset($languageCodes[$contentCreateStruct->mainLanguageCode])) { |
||
764 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
765 | $contentCreateStruct->mainLanguageCode |
||
766 | ); |
||
767 | $languageCodes[$contentCreateStruct->mainLanguageCode] = true; |
||
768 | } |
||
769 | |||
770 | return array_keys($languageCodes); |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
775 | * |
||
776 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
777 | * or value is set for non-translatable field in language |
||
778 | * other than main |
||
779 | * |
||
780 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
781 | * |
||
782 | * @return array |
||
783 | */ |
||
784 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
785 | { |
||
786 | $fields = []; |
||
787 | |||
788 | foreach ($contentCreateStruct->fields as $field) { |
||
789 | $fieldDefinition = $contentCreateStruct->contentType->getFieldDefinition($field->fieldDefIdentifier); |
||
790 | |||
791 | if ($fieldDefinition === null) { |
||
792 | throw new ContentValidationException( |
||
793 | "Field definition '%identifier%' does not exist in given ContentType", |
||
794 | ['%identifier%' => $field->fieldDefIdentifier] |
||
795 | ); |
||
796 | } |
||
797 | |||
798 | if ($field->languageCode === null) { |
||
799 | $field = $this->cloneField( |
||
800 | $field, |
||
801 | ['languageCode' => $contentCreateStruct->mainLanguageCode] |
||
802 | ); |
||
803 | } |
||
804 | |||
805 | if (!$fieldDefinition->isTranslatable && ($field->languageCode != $contentCreateStruct->mainLanguageCode)) { |
||
806 | throw new ContentValidationException( |
||
807 | "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'", |
||
808 | ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode] |
||
809 | ); |
||
810 | } |
||
811 | |||
812 | $fields[$field->fieldDefIdentifier][$field->languageCode] = $field; |
||
813 | } |
||
814 | |||
815 | return $fields; |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * Clones $field with overriding specific properties from given $overrides array. |
||
820 | * |
||
821 | * @param Field $field |
||
822 | * @param array $overrides |
||
823 | * |
||
824 | * @return Field |
||
825 | */ |
||
826 | private function cloneField(Field $field, array $overrides = []) |
||
827 | { |
||
828 | $fieldData = array_merge( |
||
829 | [ |
||
830 | 'id' => $field->id, |
||
831 | 'value' => $field->value, |
||
832 | 'languageCode' => $field->languageCode, |
||
833 | 'fieldDefIdentifier' => $field->fieldDefIdentifier, |
||
834 | 'fieldTypeIdentifier' => $field->fieldTypeIdentifier, |
||
835 | ], |
||
836 | $overrides |
||
837 | ); |
||
838 | |||
839 | return new Field($fieldData); |
||
840 | } |
||
841 | |||
842 | /** |
||
843 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
844 | * |
||
845 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
846 | * |
||
847 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
848 | */ |
||
849 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
850 | { |
||
851 | $spiLocationCreateStructs = []; |
||
852 | $parentLocationIdSet = []; |
||
853 | $mainLocation = true; |
||
854 | |||
855 | foreach ($locationCreateStructs as $locationCreateStruct) { |
||
856 | if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) { |
||
857 | throw new InvalidArgumentException( |
||
858 | '$locationCreateStructs', |
||
859 | "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given" |
||
860 | ); |
||
861 | } |
||
862 | |||
863 | if (!array_key_exists($locationCreateStruct->sortField, Location::SORT_FIELD_MAP)) { |
||
864 | $locationCreateStruct->sortField = Location::SORT_FIELD_NAME; |
||
865 | } |
||
866 | |||
867 | if (!array_key_exists($locationCreateStruct->sortOrder, Location::SORT_ORDER_MAP)) { |
||
868 | $locationCreateStruct->sortOrder = Location::SORT_ORDER_ASC; |
||
869 | } |
||
870 | |||
871 | $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true; |
||
872 | $parentLocation = $this->repository->getLocationService()->loadLocation( |
||
873 | $locationCreateStruct->parentLocationId |
||
874 | ); |
||
875 | |||
876 | $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct( |
||
877 | $locationCreateStruct, |
||
878 | $parentLocation, |
||
879 | $mainLocation, |
||
880 | // For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation |
||
881 | null, |
||
882 | null |
||
883 | ); |
||
884 | |||
885 | // First Location in the list will be created as main Location |
||
886 | $mainLocation = false; |
||
887 | } |
||
888 | |||
889 | return $spiLocationCreateStructs; |
||
890 | } |
||
891 | |||
892 | /** |
||
893 | * Updates the metadata. |
||
894 | * |
||
895 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
896 | * |
||
897 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
898 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
899 | * |
||
900 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
901 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
902 | * |
||
903 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
904 | */ |
||
905 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
906 | { |
||
907 | $propertyCount = 0; |
||
908 | foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) { |
||
909 | if (isset($contentMetadataUpdateStruct->$propertyName)) { |
||
910 | $propertyCount += 1; |
||
911 | } |
||
912 | } |
||
913 | if ($propertyCount === 0) { |
||
914 | throw new InvalidArgumentException( |
||
915 | '$contentMetadataUpdateStruct', |
||
916 | 'At least one property must be set' |
||
917 | ); |
||
918 | } |
||
919 | |||
920 | $loadedContentInfo = $this->loadContentInfo($contentInfo->id); |
||
921 | |||
922 | if (!$this->repository->canUser('content', 'edit', $loadedContentInfo)) { |
||
923 | throw new UnauthorizedException('content', 'edit', ['contentId' => $loadedContentInfo->id]); |
||
924 | } |
||
925 | |||
926 | if (isset($contentMetadataUpdateStruct->remoteId)) { |
||
927 | try { |
||
928 | $existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId); |
||
929 | |||
930 | if ($existingContentInfo->id !== $loadedContentInfo->id) { |
||
931 | throw new InvalidArgumentException( |
||
932 | '$contentMetadataUpdateStruct', |
||
933 | "Another content with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists" |
||
934 | ); |
||
935 | } |
||
936 | } catch (APINotFoundException $e) { |
||
937 | // Do nothing |
||
938 | } |
||
939 | } |
||
940 | |||
941 | $this->repository->beginTransaction(); |
||
942 | try { |
||
943 | if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) { |
||
944 | $this->persistenceHandler->contentHandler()->updateMetadata( |
||
945 | $loadedContentInfo->id, |
||
946 | new SPIMetadataUpdateStruct( |
||
947 | [ |
||
948 | 'ownerId' => $contentMetadataUpdateStruct->ownerId, |
||
949 | 'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ? |
||
950 | $contentMetadataUpdateStruct->publishedDate->getTimestamp() : |
||
951 | null, |
||
952 | 'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ? |
||
953 | $contentMetadataUpdateStruct->modificationDate->getTimestamp() : |
||
954 | null, |
||
955 | 'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ? |
||
956 | $this->repository->getContentLanguageService()->loadLanguage( |
||
957 | $contentMetadataUpdateStruct->mainLanguageCode |
||
958 | )->id : |
||
959 | null, |
||
960 | 'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable, |
||
961 | 'remoteId' => $contentMetadataUpdateStruct->remoteId, |
||
962 | 'name' => $contentMetadataUpdateStruct->name, |
||
963 | ] |
||
964 | ) |
||
965 | ); |
||
966 | } |
||
967 | |||
968 | // Change main location |
||
969 | if (isset($contentMetadataUpdateStruct->mainLocationId) |
||
970 | && $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) { |
||
971 | $this->persistenceHandler->locationHandler()->changeMainLocation( |
||
972 | $loadedContentInfo->id, |
||
973 | $contentMetadataUpdateStruct->mainLocationId |
||
974 | ); |
||
975 | } |
||
976 | |||
977 | // Republish URL aliases to update always-available flag |
||
978 | if (isset($contentMetadataUpdateStruct->alwaysAvailable) |
||
979 | && $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) { |
||
980 | $content = $this->loadContent($loadedContentInfo->id); |
||
981 | $this->publishUrlAliasesForContent($content, false); |
||
982 | } |
||
983 | |||
984 | $this->repository->commit(); |
||
985 | } catch (Exception $e) { |
||
986 | $this->repository->rollback(); |
||
987 | throw $e; |
||
988 | } |
||
989 | |||
990 | return isset($content) ? $content : $this->loadContent($loadedContentInfo->id); |
||
991 | } |
||
992 | |||
993 | /** |
||
994 | * Publishes URL aliases for all locations of a given content. |
||
995 | * |
||
996 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
997 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
998 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
999 | */ |
||
1000 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
1026 | |||
1027 | /** |
||
1028 | * Deletes a content object including all its versions and locations including their subtrees. |
||
1029 | * |
||
1030 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete the content (in one of the locations of the given content object) |
||
1031 | * |
||
1032 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1033 | * |
||
1034 | * @return mixed[] Affected Location Id's |
||
1035 | */ |
||
1036 | public function deleteContent(ContentInfo $contentInfo) |
||
1063 | |||
1064 | /** |
||
1065 | * Creates a draft from a published or archived version. |
||
1066 | * |
||
1067 | * If no version is given, the current published version is used. |
||
1068 | * |
||
1069 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1070 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1071 | * @param \eZ\Publish\API\Repository\Values\User\User $creator if set given user is used to create the draft - otherwise the current-user is used |
||
1072 | * @param \eZ\Publish\API\Repository\Values\Content\Language|null if not set the draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
1073 | * |
||
1074 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
1075 | * |
||
1076 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
1077 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the current-user is not allowed to create the draft |
||
1078 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
1079 | */ |
||
1080 | public function createContentDraft( |
||
1081 | ContentInfo $contentInfo, |
||
1082 | APIVersionInfo $versionInfo = null, |
||
1083 | User $creator = null, |
||
1084 | ?Language $language = null |
||
1085 | ) { |
||
1086 | $contentInfo = $this->loadContentInfo($contentInfo->id); |
||
1087 | |||
1088 | if ($versionInfo !== null) { |
||
1089 | // Check that given $contentInfo and $versionInfo belong to the same content |
||
1090 | if ($versionInfo->getContentInfo()->id != $contentInfo->id) { |
||
1091 | throw new InvalidArgumentException( |
||
1092 | '$versionInfo', |
||
1093 | 'VersionInfo does not belong to the same content as given ContentInfo' |
||
1094 | ); |
||
1095 | } |
||
1096 | |||
1097 | $versionInfo = $this->loadVersionInfoById($contentInfo->id, $versionInfo->versionNo); |
||
1098 | |||
1099 | switch ($versionInfo->status) { |
||
1100 | case VersionInfo::STATUS_PUBLISHED: |
||
1101 | case VersionInfo::STATUS_ARCHIVED: |
||
1102 | break; |
||
1103 | |||
1104 | default: |
||
1105 | // @todo: throw an exception here, to be defined |
||
1106 | throw new BadStateException( |
||
1107 | '$versionInfo', |
||
1108 | 'Draft can not be created from a draft version' |
||
1109 | ); |
||
1110 | } |
||
1111 | |||
1112 | $versionNo = $versionInfo->versionNo; |
||
1113 | } elseif ($contentInfo->published) { |
||
1114 | $versionNo = $contentInfo->currentVersionNo; |
||
1115 | } else { |
||
1116 | // @todo: throw an exception here, to be defined |
||
1117 | throw new BadStateException( |
||
1118 | '$contentInfo', |
||
1119 | 'Content is not published, draft can be created only from published or archived version' |
||
1120 | ); |
||
1121 | } |
||
1122 | |||
1123 | if ($creator === null) { |
||
1124 | $creator = $this->repository->getCurrentUserReference(); |
||
1125 | } |
||
1126 | |||
1127 | $fallbackLanguageCode = $versionInfo->initialLanguageCode ?? $contentInfo->mainLanguageCode; |
||
1128 | $languageCode = $language->languageCode ?? $fallbackLanguageCode; |
||
1129 | |||
1130 | if (!$this->repository->getPermissionResolver()->canUser( |
||
1131 | 'content', |
||
1132 | 'edit', |
||
1133 | $contentInfo, |
||
1134 | [ |
||
1135 | (new Target\Builder\VersionBuilder()) |
||
1136 | ->changeStatusTo(APIVersionInfo::STATUS_DRAFT) |
||
1137 | ->build(), |
||
1138 | ] |
||
1139 | )) { |
||
1140 | throw new UnauthorizedException( |
||
1141 | 'content', |
||
1142 | 'edit', |
||
1143 | ['contentId' => $contentInfo->id] |
||
1144 | ); |
||
1145 | } |
||
1146 | |||
1147 | $this->repository->beginTransaction(); |
||
1148 | try { |
||
1149 | $spiContent = $this->persistenceHandler->contentHandler()->createDraftFromVersion( |
||
1150 | $contentInfo->id, |
||
1151 | $versionNo, |
||
1152 | $creator->getUserId(), |
||
1153 | $languageCode |
||
1154 | ); |
||
1155 | $this->repository->commit(); |
||
1156 | } catch (Exception $e) { |
||
1157 | $this->repository->rollback(); |
||
1158 | throw $e; |
||
1159 | } |
||
1160 | |||
1161 | return $this->domainMapper->buildContentDomainObject( |
||
1162 | $spiContent, |
||
1163 | $this->repository->getContentTypeService()->loadContentType( |
||
1164 | $spiContent->versionInfo->contentInfo->contentTypeId |
||
1165 | ) |
||
1166 | ); |
||
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * {@inheritdoc} |
||
1171 | */ |
||
1172 | public function countContentDrafts(?User $user = null): int |
||
1182 | |||
1183 | /** |
||
1184 | * Loads drafts for a user. |
||
1185 | * |
||
1186 | * If no user is given the drafts for the authenticated user are returned |
||
1187 | * |
||
1188 | * @param \eZ\Publish\API\Repository\Values\User\User|null $user |
||
1189 | * |
||
1190 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Drafts owned by the given user |
||
1191 | * |
||
1192 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
1193 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
1194 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1195 | */ |
||
1196 | public function loadContentDrafts(User $user = null) |
||
1219 | |||
1220 | /** |
||
1221 | * {@inheritdoc} |
||
1222 | */ |
||
1223 | public function loadContentDraftList(?User $user = null, int $offset = 0, int $limit = -1): ContentDraftList |
||
1255 | |||
1256 | /** |
||
1257 | * Updates the fields of a draft. |
||
1258 | * |
||
1259 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1260 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1261 | * |
||
1262 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
1263 | * |
||
1264 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
1265 | * or if a required field is missing / set to an empty value. |
||
1266 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
1267 | * or value is set for non-translatable field in language |
||
1268 | * other than main. |
||
1269 | * |
||
1270 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
1271 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1272 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
1273 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1274 | */ |
||
1275 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
1462 | |||
1463 | /** |
||
1464 | * Returns only updated language codes. |
||
1465 | * |
||
1466 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1467 | * |
||
1468 | * @return array |
||
1469 | */ |
||
1470 | private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) |
||
1486 | |||
1487 | /** |
||
1488 | * Returns all language codes used in given $fields. |
||
1489 | * |
||
1490 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
1491 | * |
||
1492 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1493 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
1494 | * |
||
1495 | * @return array |
||
1496 | */ |
||
1497 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
1509 | |||
1510 | /** |
||
1511 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
1512 | * |
||
1513 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
1514 | * or value is set for non-translatable field in language |
||
1515 | * other than main |
||
1516 | * |
||
1517 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1518 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
1519 | * @param string $mainLanguageCode |
||
1520 | * |
||
1521 | * @return array |
||
1522 | */ |
||
1523 | protected function mapFieldsForUpdate( |
||
1561 | |||
1562 | /** |
||
1563 | * Publishes a content version. |
||
1564 | * |
||
1565 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1566 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1567 | * |
||
1568 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1569 | * @param string[] $translations |
||
1570 | * |
||
1571 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1572 | * |
||
1573 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1574 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1575 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1576 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
1577 | */ |
||
1578 | public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL) |
||
1621 | |||
1622 | /** |
||
1623 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1624 | * @param array $translations |
||
1625 | * |
||
1626 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
1627 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException |
||
1628 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException |
||
1629 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1630 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1631 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
1632 | */ |
||
1633 | protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionInfo, array $translations = []): void |
||
1727 | |||
1728 | /** |
||
1729 | * Publishes a content version. |
||
1730 | * |
||
1731 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1732 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1733 | * |
||
1734 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1735 | * |
||
1736 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1737 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
1738 | * |
||
1739 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1740 | */ |
||
1741 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
1793 | |||
1794 | /** |
||
1795 | * @return int |
||
1796 | */ |
||
1797 | protected function getUnixTimestamp() |
||
1801 | |||
1802 | /** |
||
1803 | * Removes the given version. |
||
1804 | * |
||
1805 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
1806 | * published state or is a last version of Content in non draft state |
||
1807 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
1808 | * |
||
1809 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1810 | */ |
||
1811 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
1853 | |||
1854 | /** |
||
1855 | * Loads all versions for the given content. |
||
1856 | * |
||
1857 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
1858 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the given status is invalid |
||
1859 | * |
||
1860 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1861 | * @param int|null $status |
||
1862 | * |
||
1863 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
1864 | */ |
||
1865 | public function loadVersions(ContentInfo $contentInfo, ?int $status = null) |
||
1894 | |||
1895 | /** |
||
1896 | * Copies the content to a new location. If no version is given, |
||
1897 | * all versions are copied, otherwise only the given version. |
||
1898 | * |
||
1899 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
1900 | * |
||
1901 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1902 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
1903 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1904 | * |
||
1905 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1906 | */ |
||
1907 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
1962 | |||
1963 | /** |
||
1964 | * Loads all outgoing relations for the given version. |
||
1965 | * |
||
1966 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
1967 | * |
||
1968 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1969 | * |
||
1970 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
1971 | */ |
||
1972 | public function loadRelations(APIVersionInfo $versionInfo) |
||
2007 | |||
2008 | /** |
||
2009 | * {@inheritdoc} |
||
2010 | */ |
||
2011 | public function countReverseRelations(ContentInfo $contentInfo): int |
||
2021 | |||
2022 | /** |
||
2023 | * Loads all incoming relations for a content object. |
||
2024 | * |
||
2025 | * The relations come only from published versions of the source content objects |
||
2026 | * |
||
2027 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
2028 | * |
||
2029 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2030 | * |
||
2031 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
2032 | */ |
||
2033 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
2059 | |||
2060 | /** |
||
2061 | * {@inheritdoc} |
||
2062 | */ |
||
2063 | public function loadReverseRelationList(ContentInfo $contentInfo, int $offset = 0, int $limit = -1): RelationList |
||
2100 | |||
2101 | /** |
||
2102 | * Adds a relation of type common. |
||
2103 | * |
||
2104 | * The source of the relation is the content and version |
||
2105 | * referenced by $versionInfo. |
||
2106 | * |
||
2107 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
2108 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
2109 | * |
||
2110 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
2111 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
2112 | * |
||
2113 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
2114 | */ |
||
2115 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
2156 | |||
2157 | /** |
||
2158 | * Removes a relation of type COMMON from a draft. |
||
2159 | * |
||
2160 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
2161 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
2162 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
2163 | * |
||
2164 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
2165 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
2166 | */ |
||
2167 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
2217 | |||
2218 | /** |
||
2219 | * {@inheritdoc} |
||
2220 | */ |
||
2221 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
2229 | |||
2230 | /** |
||
2231 | * Delete Content item Translation from all Versions (including archived ones) of a Content Object. |
||
2232 | * |
||
2233 | * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it. |
||
2234 | * |
||
2235 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
2236 | * is the Main Translation of a Content Item. |
||
2237 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
2238 | * to delete the content (in one of the locations of the given Content Item). |
||
2239 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
2240 | * is invalid for the given content. |
||
2241 | * |
||
2242 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2243 | * @param string $languageCode |
||
2244 | * |
||
2245 | * @since 6.13 |
||
2246 | */ |
||
2247 | public function deleteTranslation(ContentInfo $contentInfo, $languageCode) |
||
2324 | |||
2325 | /** |
||
2326 | * Delete specified Translation from a Content Draft. |
||
2327 | * |
||
2328 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
2329 | * is the only one the Content Draft has or it is the main Translation of a Content Object. |
||
2330 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
2331 | * to edit the Content (in one of the locations of the given Content Object). |
||
2332 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
2333 | * is invalid for the given Draft. |
||
2334 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found |
||
2335 | * |
||
2336 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft |
||
2337 | * @param string $languageCode Language code of the Translation to be removed |
||
2338 | * |
||
2339 | * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation |
||
2340 | * |
||
2341 | * @since 6.12 |
||
2342 | */ |
||
2343 | public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) |
||
2409 | |||
2410 | /** |
||
2411 | * Hides Content by making all the Locations appear hidden. |
||
2412 | * It does not persist hidden state on Location object itself. |
||
2413 | * |
||
2414 | * Content hidden by this API can be revealed by revealContent API. |
||
2415 | * |
||
2416 | * @see revealContent |
||
2417 | * |
||
2418 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2419 | */ |
||
2420 | public function hideContent(ContentInfo $contentInfo): void |
||
2445 | |||
2446 | /** |
||
2447 | * Reveals Content hidden by hideContent API. |
||
2448 | * Locations which were hidden before hiding Content will remain hidden. |
||
2449 | * |
||
2450 | * @see hideContent |
||
2451 | * |
||
2452 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2453 | */ |
||
2454 | public function revealContent(ContentInfo $contentInfo): void |
||
2479 | |||
2480 | /** |
||
2481 | * Instantiates a new content create struct object. |
||
2482 | * |
||
2483 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
2484 | * |
||
2485 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
2486 | * @param string $mainLanguageCode |
||
2487 | * |
||
2488 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
2489 | */ |
||
2490 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
2500 | |||
2501 | /** |
||
2502 | * Instantiates a new content meta data update struct. |
||
2503 | * |
||
2504 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
2505 | */ |
||
2506 | public function newContentMetadataUpdateStruct() |
||
2510 | |||
2511 | /** |
||
2512 | * Instantiates a new content update struct. |
||
2513 | * |
||
2514 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
2515 | */ |
||
2516 | public function newContentUpdateStruct() |
||
2520 | |||
2521 | /** |
||
2522 | * @param \eZ\Publish\API\Repository\Values\User\User|null $user |
||
2523 | * |
||
2524 | * @return \eZ\Publish\API\Repository\Values\User\UserReference |
||
2525 | */ |
||
2526 | private function resolveUser(?User $user): UserReference |
||
2534 | } |
||
2535 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.