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 |
||
51 | class ContentService implements ContentServiceInterface |
||
52 | { |
||
53 | /** @var \eZ\Publish\Core\Repository\Repository */ |
||
54 | protected $repository; |
||
55 | |||
56 | /** @var \eZ\Publish\SPI\Persistence\Handler */ |
||
57 | protected $persistenceHandler; |
||
58 | |||
59 | /** @var array */ |
||
60 | protected $settings; |
||
61 | |||
62 | /** @var \eZ\Publish\Core\Repository\Helper\DomainMapper */ |
||
63 | protected $domainMapper; |
||
64 | |||
65 | /** @var \eZ\Publish\Core\Repository\Helper\RelationProcessor */ |
||
66 | protected $relationProcessor; |
||
67 | |||
68 | /** @var \eZ\Publish\Core\Repository\Helper\NameSchemaService */ |
||
69 | protected $nameSchemaService; |
||
70 | |||
71 | /** @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry */ |
||
72 | protected $fieldTypeRegistry; |
||
73 | |||
74 | /** |
||
75 | * Setups service with reference to repository object that created it & corresponding handler. |
||
76 | * |
||
77 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
78 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
79 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
80 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
81 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
82 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|||
83 | * @param array $settings |
||
84 | */ |
||
85 | public function __construct( |
||
86 | RepositoryInterface $repository, |
||
87 | Handler $handler, |
||
88 | Helper\DomainMapper $domainMapper, |
||
89 | Helper\RelationProcessor $relationProcessor, |
||
90 | Helper\NameSchemaService $nameSchemaService, |
||
91 | Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
92 | array $settings = [] |
||
93 | ) { |
||
94 | $this->repository = $repository; |
||
95 | $this->persistenceHandler = $handler; |
||
96 | $this->domainMapper = $domainMapper; |
||
97 | $this->relationProcessor = $relationProcessor; |
||
98 | $this->nameSchemaService = $nameSchemaService; |
||
99 | $this->fieldTypeRegistry = $fieldTypeRegistry; |
||
100 | // Union makes sure default settings are ignored if provided in argument |
||
101 | $this->settings = $settings + [ |
||
102 | // Version archive limit (0-50), only enforced on publish, not on un-publish. |
||
103 | 'default_version_archive_limit' => 5, |
||
104 | ]; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Loads a content info object. |
||
109 | * |
||
110 | * To load fields use loadContent |
||
111 | * |
||
112 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
113 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
114 | * |
||
115 | * @param int $contentId |
||
116 | * |
||
117 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
118 | */ |
||
119 | public function loadContentInfo($contentId) |
||
120 | { |
||
121 | $contentInfo = $this->internalLoadContentInfo($contentId); |
||
122 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
123 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
124 | } |
||
125 | |||
126 | return $contentInfo; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function loadContentInfoList(array $contentIds): iterable |
||
133 | { |
||
134 | $contentInfoList = []; |
||
135 | $spiInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds); |
||
136 | foreach ($spiInfoList as $id => $spiInfo) { |
||
137 | $contentInfo = $this->domainMapper->buildContentInfoDomainObject($spiInfo); |
||
138 | if ($this->repository->canUser('content', 'read', $contentInfo)) { |
||
139 | $contentInfoList[$id] = $contentInfo; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $contentInfoList; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Loads a content info object. |
||
148 | * |
||
149 | * To load fields use loadContent |
||
150 | * |
||
151 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
152 | * |
||
153 | * @param mixed $id |
||
154 | * @param bool $isRemoteId |
||
155 | * |
||
156 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
157 | */ |
||
158 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
174 | |||
175 | /** |
||
176 | * Loads a content info object for the given remoteId. |
||
177 | * |
||
178 | * To load fields use loadContent |
||
179 | * |
||
180 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
181 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
182 | * |
||
183 | * @param string $remoteId |
||
184 | * |
||
185 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
186 | */ |
||
187 | public function loadContentInfoByRemoteId($remoteId) |
||
188 | { |
||
189 | $contentInfo = $this->internalLoadContentInfo($remoteId, true); |
||
190 | |||
191 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
192 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
193 | } |
||
194 | |||
195 | return $contentInfo; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Loads a version info of the given content object. |
||
200 | * |
||
201 | * If no version number is given, the method returns the current version |
||
202 | * |
||
203 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
204 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
205 | * |
||
206 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
207 | * @param int $versionNo the version number. If not given the current version is returned. |
||
208 | * |
||
209 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
210 | */ |
||
211 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
215 | |||
216 | /** |
||
217 | * Loads a version info of the given content object id. |
||
218 | * |
||
219 | * If no version number is given, the method returns the current version |
||
220 | * |
||
221 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
222 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
223 | * |
||
224 | * @param mixed $contentId |
||
225 | * @param int $versionNo the version number. If not given the current version is returned. |
||
226 | * |
||
227 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
228 | */ |
||
229 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
230 | { |
||
231 | try { |
||
232 | $spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo( |
||
233 | $contentId, |
||
234 | $versionNo |
||
235 | ); |
||
236 | } catch (APINotFoundException $e) { |
||
237 | throw new NotFoundException( |
||
238 | 'VersionInfo', |
||
239 | [ |
||
240 | 'contentId' => $contentId, |
||
241 | 'versionNo' => $versionNo, |
||
242 | ], |
||
243 | $e |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo); |
||
248 | |||
249 | if ($versionInfo->isPublished()) { |
||
250 | $function = 'read'; |
||
251 | } else { |
||
252 | $function = 'versionread'; |
||
253 | } |
||
254 | |||
255 | if (!$this->repository->canUser('content', $function, $versionInfo)) { |
||
256 | throw new UnauthorizedException('content', $function, ['contentId' => $contentId]); |
||
257 | } |
||
258 | |||
259 | return $versionInfo; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
266 | { |
||
267 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
268 | if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) { |
||
269 | $useAlwaysAvailable = false; |
||
270 | } |
||
271 | |||
272 | return $this->loadContent( |
||
273 | $contentInfo->id, |
||
274 | $languages, |
||
275 | $versionNo,// On purpose pass as-is and not use $contentInfo, to make sure to return actual current version on null |
||
276 | $useAlwaysAvailable |
||
277 | ); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
284 | { |
||
285 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
286 | if ($useAlwaysAvailable && !$versionInfo->getContentInfo()->alwaysAvailable) { |
||
287 | $useAlwaysAvailable = false; |
||
288 | } |
||
289 | |||
290 | return $this->loadContent( |
||
291 | $versionInfo->getContentInfo()->id, |
||
292 | $languages, |
||
293 | $versionInfo->versionNo, |
||
294 | $useAlwaysAvailable |
||
295 | ); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
302 | { |
||
303 | $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable); |
||
304 | |||
305 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
306 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
307 | } |
||
308 | if ( |
||
309 | !$content->getVersionInfo()->isPublished() |
||
310 | && !$this->repository->canUser('content', 'versionread', $content) |
||
311 | ) { |
||
312 | throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId, 'versionNo' => $versionNo]); |
||
313 | } |
||
314 | |||
315 | return $content; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Loads content in a version of the given content object. |
||
320 | * |
||
321 | * If no version number is given, the method returns the current version |
||
322 | * |
||
323 | * @internal |
||
324 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
325 | * |
||
326 | * @param mixed $id |
||
327 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
328 | * returned value object. If not given all languages are returned. |
||
329 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
330 | * @param bool $isRemoteId |
||
331 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
332 | * |
||
333 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
334 | */ |
||
335 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
336 | { |
||
337 | try { |
||
338 | // Get Content ID if lookup by remote ID |
||
339 | if ($isRemoteId) { |
||
340 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id); |
||
341 | $id = $spiContentInfo->id; |
||
342 | // Set $isRemoteId to false as the next loads will be for content id now that we have it (for exception use now) |
||
343 | $isRemoteId = false; |
||
344 | } |
||
345 | |||
346 | $loadLanguages = $languages; |
||
347 | $alwaysAvailableLanguageCode = null; |
||
348 | // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true |
||
349 | // @todo Move use always available logic to SPI load methods, like done in location handler in 7.x |
||
350 | if (!empty($loadLanguages) && $useAlwaysAvailable) { |
||
351 | if (!isset($spiContentInfo)) { |
||
352 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); |
||
353 | } |
||
354 | |||
355 | if ($spiContentInfo->alwaysAvailable) { |
||
356 | $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; |
||
357 | $loadLanguages = array_unique($loadLanguages); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | $spiContent = $this->persistenceHandler->contentHandler()->load( |
||
362 | $id, |
||
363 | $versionNo, |
||
364 | $loadLanguages |
||
365 | ); |
||
366 | } catch (APINotFoundException $e) { |
||
367 | throw new NotFoundException( |
||
368 | 'Content', |
||
369 | [ |
||
370 | $isRemoteId ? 'remoteId' : 'id' => $id, |
||
371 | 'languages' => $languages, |
||
372 | 'versionNo' => $versionNo, |
||
373 | ], |
||
374 | $e |
||
375 | ); |
||
376 | } |
||
377 | |||
378 | return $this->domainMapper->buildContentDomainObject( |
||
379 | $spiContent, |
||
380 | $this->repository->getContentTypeService()->loadContentType( |
||
381 | $spiContent->versionInfo->contentInfo->contentTypeId |
||
382 | ), |
||
383 | $languages ?? [], |
||
384 | $alwaysAvailableLanguageCode |
||
385 | ); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Loads content in a version for the content object reference by the given remote id. |
||
390 | * |
||
391 | * If no version is given, the method returns the current version |
||
392 | * |
||
393 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
394 | * @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 |
||
395 | * |
||
396 | * @param string $remoteId |
||
397 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
398 | * @param int $versionNo the version number. If not given the current version is returned |
||
399 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
400 | * |
||
401 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
402 | */ |
||
403 | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
404 | { |
||
405 | $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable); |
||
406 | |||
407 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
408 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
409 | } |
||
410 | |||
411 | if ( |
||
412 | !$content->getVersionInfo()->isPublished() |
||
413 | && !$this->repository->canUser('content', 'versionread', $content) |
||
414 | ) { |
||
415 | throw new UnauthorizedException('content', 'versionread', ['remoteId' => $remoteId, 'versionNo' => $versionNo]); |
||
416 | } |
||
417 | |||
418 | return $content; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Bulk-load Content items by the list of ContentInfo Value Objects. |
||
423 | * |
||
424 | * Note: it does not throw exceptions on load, just ignores erroneous Content item. |
||
425 | * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is |
||
426 | * allowed to access every Content on the list. |
||
427 | * |
||
428 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList |
||
429 | * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on |
||
430 | * returned value object. If not given all languages are returned. |
||
431 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true, |
||
432 | * unless all languages have been asked for. |
||
433 | * |
||
434 | * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys |
||
435 | */ |
||
436 | public function loadContentListByContentInfo( |
||
437 | array $contentInfoList, |
||
438 | array $languages = [], |
||
439 | $useAlwaysAvailable = true |
||
440 | ) { |
||
441 | $loadAllLanguages = $languages === Language::ALL; |
||
442 | $contentIds = []; |
||
443 | $contentTypeIds = []; |
||
444 | $translations = $languages; |
||
445 | foreach ($contentInfoList as $contentInfo) { |
||
446 | $contentIds[] = $contentInfo->id; |
||
447 | $contentTypeIds[] = $contentInfo->contentTypeId; |
||
448 | // Unless we are told to load all languages, we add main language to translations so they are loaded too |
||
449 | // Might in some case load more languages then intended, but prioritised handling will pick right one |
||
450 | if (!$loadAllLanguages && $useAlwaysAvailable && $contentInfo->alwaysAvailable) { |
||
451 | $translations[] = $contentInfo->mainLanguageCode; |
||
452 | } |
||
453 | } |
||
454 | |||
455 | $contentList = []; |
||
456 | $translations = array_unique($translations); |
||
457 | $spiContentList = $this->persistenceHandler->contentHandler()->loadContentList( |
||
458 | $contentIds, |
||
459 | $translations |
||
460 | ); |
||
461 | $contentTypeList = $this->repository->getContentTypeService()->loadContentTypeList( |
||
462 | array_unique($contentTypeIds), |
||
463 | $languages |
||
464 | ); |
||
465 | foreach ($spiContentList as $contentId => $spiContent) { |
||
466 | $contentInfo = $spiContent->versionInfo->contentInfo; |
||
467 | $contentList[$contentId] = $this->domainMapper->buildContentDomainObject( |
||
468 | $spiContent, |
||
469 | $contentTypeList[$contentInfo->contentTypeId], |
||
470 | $languages, |
||
471 | $contentInfo->alwaysAvailable ? $contentInfo->mainLanguageCode : null |
||
472 | ); |
||
473 | } |
||
474 | |||
475 | return $contentList; |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * Creates a new content draft assigned to the authenticated user. |
||
480 | * |
||
481 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
482 | * but this required special rights for the authenticated user |
||
483 | * (this is useful for content staging where the transfer process does not |
||
484 | * have to authenticate with the user which created the content object in the source server). |
||
485 | * The user has to publish the draft if it should be visible. |
||
486 | * In 4.x at least one location has to be provided in the location creation array. |
||
487 | * |
||
488 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
489 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
490 | * struct are missing or invalid, or if multiple locations are under the |
||
491 | * same parent. |
||
492 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
493 | * or if a required field is missing / set to an empty value. |
||
494 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
495 | * or value is set for non-translatable field in language |
||
496 | * other than main. |
||
497 | * |
||
498 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
499 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
500 | * |
||
501 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
502 | */ |
||
503 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) |
||
504 | { |
||
505 | if ($contentCreateStruct->mainLanguageCode === null) { |
||
506 | throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); |
||
507 | } |
||
508 | |||
509 | if ($contentCreateStruct->contentType === null) { |
||
510 | throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set"); |
||
511 | } |
||
512 | |||
513 | $contentCreateStruct = clone $contentCreateStruct; |
||
514 | |||
515 | if ($contentCreateStruct->ownerId === null) { |
||
516 | $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId(); |
||
517 | } |
||
518 | |||
519 | if ($contentCreateStruct->alwaysAvailable === null) { |
||
520 | $contentCreateStruct->alwaysAvailable = $contentCreateStruct->contentType->defaultAlwaysAvailable ?: false; |
||
521 | } |
||
522 | |||
523 | $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType( |
||
524 | $contentCreateStruct->contentType->id |
||
525 | ); |
||
526 | |||
527 | if (empty($contentCreateStruct->sectionId)) { |
||
528 | if (isset($locationCreateStructs[0])) { |
||
529 | $location = $this->repository->getLocationService()->loadLocation( |
||
530 | $locationCreateStructs[0]->parentLocationId |
||
531 | ); |
||
532 | $contentCreateStruct->sectionId = $location->contentInfo->sectionId; |
||
533 | } else { |
||
534 | $contentCreateStruct->sectionId = 1; |
||
535 | } |
||
536 | } |
||
537 | |||
538 | if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) { |
||
539 | throw new UnauthorizedException( |
||
540 | 'content', |
||
541 | 'create', |
||
542 | [ |
||
543 | 'parentLocationId' => isset($locationCreateStructs[0]) ? |
||
544 | $locationCreateStructs[0]->parentLocationId : |
||
545 | null, |
||
546 | 'sectionId' => $contentCreateStruct->sectionId, |
||
547 | ] |
||
548 | ); |
||
549 | } |
||
550 | |||
551 | if (!empty($contentCreateStruct->remoteId)) { |
||
552 | try { |
||
553 | $this->loadContentByRemoteId($contentCreateStruct->remoteId); |
||
554 | |||
555 | throw new InvalidArgumentException( |
||
556 | '$contentCreateStruct', |
||
557 | "Another content with remoteId '{$contentCreateStruct->remoteId}' exists" |
||
558 | ); |
||
559 | } catch (APINotFoundException $e) { |
||
560 | // Do nothing |
||
561 | } |
||
562 | } else { |
||
563 | $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct); |
||
564 | } |
||
565 | |||
566 | $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs); |
||
567 | |||
568 | $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct); |
||
569 | $fields = $this->mapFieldsForCreate($contentCreateStruct); |
||
570 | |||
571 | $fieldValues = []; |
||
572 | $spiFields = []; |
||
573 | $allFieldErrors = []; |
||
574 | $inputRelations = []; |
||
575 | $locationIdToContentIdMapping = []; |
||
576 | |||
577 | foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
||
578 | /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */ |
||
579 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
580 | $fieldDefinition->fieldTypeIdentifier |
||
581 | ); |
||
582 | |||
583 | foreach ($languageCodes as $languageCode) { |
||
584 | $isEmptyValue = false; |
||
585 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode; |
||
586 | $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode; |
||
587 | if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) { |
||
588 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
589 | } else { |
||
590 | $fieldValue = $fieldDefinition->defaultValue; |
||
591 | } |
||
592 | |||
593 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
594 | |||
595 | if ($fieldType->isEmptyValue($fieldValue)) { |
||
596 | $isEmptyValue = true; |
||
597 | if ($fieldDefinition->isRequired) { |
||
598 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
599 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
600 | null, |
||
601 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
602 | 'empty' |
||
603 | ); |
||
604 | } |
||
605 | } else { |
||
606 | $fieldErrors = $fieldType->validate( |
||
607 | $fieldDefinition, |
||
608 | $fieldValue |
||
609 | ); |
||
610 | if (!empty($fieldErrors)) { |
||
611 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
612 | } |
||
613 | } |
||
614 | |||
615 | if (!empty($allFieldErrors)) { |
||
616 | continue; |
||
617 | } |
||
618 | |||
619 | $this->relationProcessor->appendFieldRelations( |
||
620 | $inputRelations, |
||
621 | $locationIdToContentIdMapping, |
||
622 | $fieldType, |
||
623 | $fieldValue, |
||
624 | $fieldDefinition->id |
||
625 | ); |
||
626 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
627 | |||
628 | // Only non-empty value for: translatable field or in main language |
||
629 | if ( |
||
630 | (!$isEmptyValue && $fieldDefinition->isTranslatable) || |
||
631 | (!$isEmptyValue && $isLanguageMain) |
||
632 | ) { |
||
633 | $spiFields[] = new SPIField( |
||
634 | [ |
||
635 | 'id' => null, |
||
636 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
637 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
638 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
639 | 'languageCode' => $languageCode, |
||
640 | 'versionNo' => null, |
||
641 | ] |
||
642 | ); |
||
643 | } |
||
644 | } |
||
645 | } |
||
646 | |||
647 | if (!empty($allFieldErrors)) { |
||
648 | throw new ContentFieldValidationException($allFieldErrors); |
||
649 | } |
||
650 | |||
651 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
652 | [ |
||
653 | 'name' => $this->nameSchemaService->resolve( |
||
654 | $contentCreateStruct->contentType->nameSchema, |
||
655 | $contentCreateStruct->contentType, |
||
656 | $fieldValues, |
||
657 | $languageCodes |
||
658 | ), |
||
659 | 'typeId' => $contentCreateStruct->contentType->id, |
||
660 | 'sectionId' => $contentCreateStruct->sectionId, |
||
661 | 'ownerId' => $contentCreateStruct->ownerId, |
||
662 | 'locations' => $spiLocationCreateStructs, |
||
663 | 'fields' => $spiFields, |
||
664 | 'alwaysAvailable' => $contentCreateStruct->alwaysAvailable, |
||
665 | 'remoteId' => $contentCreateStruct->remoteId, |
||
666 | 'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), |
||
667 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
668 | $contentCreateStruct->mainLanguageCode |
||
669 | )->id, |
||
670 | ] |
||
671 | ); |
||
672 | |||
673 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
674 | |||
675 | $this->repository->beginTransaction(); |
||
676 | try { |
||
677 | $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct); |
||
678 | $this->relationProcessor->processFieldRelations( |
||
679 | $inputRelations, |
||
680 | $spiContent->versionInfo->contentInfo->id, |
||
681 | $spiContent->versionInfo->versionNo, |
||
682 | $contentCreateStruct->contentType |
||
683 | ); |
||
684 | |||
685 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
686 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
687 | $objectStateHandler->setContentState( |
||
688 | $spiContent->versionInfo->contentInfo->id, |
||
689 | $objectStateGroupId, |
||
690 | $objectState->id |
||
691 | ); |
||
692 | } |
||
693 | |||
694 | $this->repository->commit(); |
||
695 | } catch (Exception $e) { |
||
696 | $this->repository->rollback(); |
||
697 | throw $e; |
||
698 | } |
||
699 | |||
700 | return $this->domainMapper->buildContentDomainObject( |
||
701 | $spiContent, |
||
702 | $contentCreateStruct->contentType |
||
703 | ); |
||
704 | } |
||
705 | |||
706 | /** |
||
707 | * Returns an array of default content states with content state group id as key. |
||
708 | * |
||
709 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
710 | */ |
||
711 | protected function getDefaultObjectStates() |
||
712 | { |
||
713 | $defaultObjectStatesMap = []; |
||
714 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
715 | |||
716 | foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) { |
||
717 | foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) { |
||
718 | // Only register the first object state which is the default one. |
||
719 | $defaultObjectStatesMap[$objectStateGroup->id] = $objectState; |
||
720 | break; |
||
721 | } |
||
722 | } |
||
723 | |||
724 | return $defaultObjectStatesMap; |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * Returns all language codes used in given $fields. |
||
729 | * |
||
730 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
731 | * |
||
732 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
733 | * |
||
734 | * @return string[] |
||
735 | */ |
||
736 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
737 | { |
||
738 | $languageCodes = []; |
||
739 | |||
740 | foreach ($contentCreateStruct->fields as $field) { |
||
741 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
742 | continue; |
||
743 | } |
||
744 | |||
745 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
746 | $field->languageCode |
||
747 | ); |
||
748 | $languageCodes[$field->languageCode] = true; |
||
749 | } |
||
750 | |||
751 | if (!isset($languageCodes[$contentCreateStruct->mainLanguageCode])) { |
||
752 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
753 | $contentCreateStruct->mainLanguageCode |
||
754 | ); |
||
755 | $languageCodes[$contentCreateStruct->mainLanguageCode] = true; |
||
756 | } |
||
757 | |||
758 | return array_keys($languageCodes); |
||
759 | } |
||
760 | |||
761 | /** |
||
762 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
763 | * |
||
764 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
765 | * or value is set for non-translatable field in language |
||
766 | * other than main |
||
767 | * |
||
768 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
769 | * |
||
770 | * @return array |
||
771 | */ |
||
772 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
773 | { |
||
774 | $fields = []; |
||
775 | |||
776 | foreach ($contentCreateStruct->fields as $field) { |
||
777 | $fieldDefinition = $contentCreateStruct->contentType->getFieldDefinition($field->fieldDefIdentifier); |
||
778 | |||
779 | if ($fieldDefinition === null) { |
||
780 | throw new ContentValidationException( |
||
781 | "Field definition '%identifier%' does not exist in given ContentType", |
||
782 | ['%identifier%' => $field->fieldDefIdentifier] |
||
783 | ); |
||
784 | } |
||
785 | |||
786 | if ($field->languageCode === null) { |
||
787 | $field = $this->cloneField( |
||
788 | $field, |
||
789 | ['languageCode' => $contentCreateStruct->mainLanguageCode] |
||
790 | ); |
||
791 | } |
||
792 | |||
793 | if (!$fieldDefinition->isTranslatable && ($field->languageCode != $contentCreateStruct->mainLanguageCode)) { |
||
794 | throw new ContentValidationException( |
||
795 | "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'", |
||
796 | ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode] |
||
797 | ); |
||
798 | } |
||
799 | |||
800 | $fields[$field->fieldDefIdentifier][$field->languageCode] = $field; |
||
801 | } |
||
802 | |||
803 | return $fields; |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * Clones $field with overriding specific properties from given $overrides array. |
||
808 | * |
||
809 | * @param Field $field |
||
810 | * @param array $overrides |
||
811 | * |
||
812 | * @return Field |
||
813 | */ |
||
814 | private function cloneField(Field $field, array $overrides = []) |
||
815 | { |
||
816 | $fieldData = array_merge( |
||
817 | [ |
||
818 | 'id' => $field->id, |
||
819 | 'value' => $field->value, |
||
820 | 'languageCode' => $field->languageCode, |
||
821 | 'fieldDefIdentifier' => $field->fieldDefIdentifier, |
||
822 | 'fieldTypeIdentifier' => $field->fieldTypeIdentifier, |
||
823 | ], |
||
824 | $overrides |
||
825 | ); |
||
826 | |||
827 | return new Field($fieldData); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
832 | * |
||
833 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
834 | * |
||
835 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
836 | */ |
||
837 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
838 | { |
||
839 | $spiLocationCreateStructs = []; |
||
840 | $parentLocationIdSet = []; |
||
841 | $mainLocation = true; |
||
842 | |||
843 | foreach ($locationCreateStructs as $locationCreateStruct) { |
||
844 | if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) { |
||
845 | throw new InvalidArgumentException( |
||
846 | '$locationCreateStructs', |
||
847 | "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given" |
||
848 | ); |
||
849 | } |
||
850 | |||
851 | if (!array_key_exists($locationCreateStruct->sortField, Location::SORT_FIELD_MAP)) { |
||
852 | $locationCreateStruct->sortField = Location::SORT_FIELD_NAME; |
||
853 | } |
||
854 | |||
855 | if (!array_key_exists($locationCreateStruct->sortOrder, Location::SORT_ORDER_MAP)) { |
||
856 | $locationCreateStruct->sortOrder = Location::SORT_ORDER_ASC; |
||
857 | } |
||
858 | |||
859 | $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true; |
||
860 | $parentLocation = $this->repository->getLocationService()->loadLocation( |
||
861 | $locationCreateStruct->parentLocationId |
||
862 | ); |
||
863 | |||
864 | $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct( |
||
865 | $locationCreateStruct, |
||
866 | $parentLocation, |
||
867 | $mainLocation, |
||
868 | // For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation |
||
869 | null, |
||
870 | null |
||
871 | ); |
||
872 | |||
873 | // First Location in the list will be created as main Location |
||
874 | $mainLocation = false; |
||
875 | } |
||
876 | |||
877 | return $spiLocationCreateStructs; |
||
878 | } |
||
879 | |||
880 | /** |
||
881 | * Updates the metadata. |
||
882 | * |
||
883 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
884 | * |
||
885 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
886 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
887 | * |
||
888 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
889 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
890 | * |
||
891 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
892 | */ |
||
893 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
894 | { |
||
895 | $propertyCount = 0; |
||
896 | foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) { |
||
897 | if (isset($contentMetadataUpdateStruct->$propertyName)) { |
||
898 | $propertyCount += 1; |
||
899 | } |
||
900 | } |
||
901 | if ($propertyCount === 0) { |
||
902 | throw new InvalidArgumentException( |
||
903 | '$contentMetadataUpdateStruct', |
||
904 | 'At least one property must be set' |
||
905 | ); |
||
906 | } |
||
907 | |||
908 | $loadedContentInfo = $this->loadContentInfo($contentInfo->id); |
||
909 | |||
910 | if (!$this->repository->canUser('content', 'edit', $loadedContentInfo)) { |
||
911 | throw new UnauthorizedException('content', 'edit', ['contentId' => $loadedContentInfo->id]); |
||
912 | } |
||
913 | |||
914 | if (isset($contentMetadataUpdateStruct->remoteId)) { |
||
915 | try { |
||
916 | $existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId); |
||
917 | |||
918 | if ($existingContentInfo->id !== $loadedContentInfo->id) { |
||
919 | throw new InvalidArgumentException( |
||
920 | '$contentMetadataUpdateStruct', |
||
921 | "Another content with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists" |
||
922 | ); |
||
923 | } |
||
924 | } catch (APINotFoundException $e) { |
||
925 | // Do nothing |
||
926 | } |
||
927 | } |
||
928 | |||
929 | $this->repository->beginTransaction(); |
||
930 | try { |
||
931 | if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) { |
||
932 | $this->persistenceHandler->contentHandler()->updateMetadata( |
||
933 | $loadedContentInfo->id, |
||
934 | new SPIMetadataUpdateStruct( |
||
935 | [ |
||
936 | 'ownerId' => $contentMetadataUpdateStruct->ownerId, |
||
937 | 'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ? |
||
938 | $contentMetadataUpdateStruct->publishedDate->getTimestamp() : |
||
939 | null, |
||
940 | 'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ? |
||
941 | $contentMetadataUpdateStruct->modificationDate->getTimestamp() : |
||
942 | null, |
||
943 | 'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ? |
||
944 | $this->repository->getContentLanguageService()->loadLanguage( |
||
945 | $contentMetadataUpdateStruct->mainLanguageCode |
||
946 | )->id : |
||
947 | null, |
||
948 | 'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable, |
||
949 | 'remoteId' => $contentMetadataUpdateStruct->remoteId, |
||
950 | 'name' => $contentMetadataUpdateStruct->name, |
||
951 | ] |
||
952 | ) |
||
953 | ); |
||
954 | } |
||
955 | |||
956 | // Change main location |
||
957 | if (isset($contentMetadataUpdateStruct->mainLocationId) |
||
958 | && $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) { |
||
959 | $this->persistenceHandler->locationHandler()->changeMainLocation( |
||
960 | $loadedContentInfo->id, |
||
961 | $contentMetadataUpdateStruct->mainLocationId |
||
962 | ); |
||
963 | } |
||
964 | |||
965 | // Republish URL aliases to update always-available flag |
||
966 | if (isset($contentMetadataUpdateStruct->alwaysAvailable) |
||
967 | && $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) { |
||
968 | $content = $this->loadContent($loadedContentInfo->id); |
||
969 | $this->publishUrlAliasesForContent($content, false); |
||
970 | } |
||
971 | |||
972 | $this->repository->commit(); |
||
973 | } catch (Exception $e) { |
||
974 | $this->repository->rollback(); |
||
975 | throw $e; |
||
976 | } |
||
977 | |||
978 | return isset($content) ? $content : $this->loadContent($loadedContentInfo->id); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * Publishes URL aliases for all locations of a given content. |
||
983 | * |
||
984 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
985 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
986 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
987 | */ |
||
988 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
1014 | |||
1015 | /** |
||
1016 | * Deletes a content object including all its versions and locations including their subtrees. |
||
1017 | * |
||
1018 | * @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) |
||
1019 | * |
||
1020 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1021 | * |
||
1022 | * @return mixed[] Affected Location Id's |
||
1023 | */ |
||
1024 | public function deleteContent(ContentInfo $contentInfo) |
||
1051 | |||
1052 | /** |
||
1053 | * Creates a draft from a published or archived version. |
||
1054 | * |
||
1055 | * If no version is given, the current published version is used. |
||
1056 | * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
1057 | * It can be changed on updating the version. |
||
1058 | * |
||
1059 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1060 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1061 | * @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 |
||
1062 | * |
||
1063 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
1064 | * |
||
1065 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
1066 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the current-user is not allowed to create the draft |
||
1067 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
1068 | */ |
||
1069 | public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null) |
||
1149 | |||
1150 | /** |
||
1151 | * Loads drafts for a user. |
||
1152 | * |
||
1153 | * If no user is given the drafts for the authenticated user a returned |
||
1154 | * |
||
1155 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to load the draft list |
||
1156 | * |
||
1157 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
1158 | * |
||
1159 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo the drafts ({@link VersionInfo}) owned by the given user |
||
1160 | */ |
||
1161 | public function loadContentDrafts(User $user = null) |
||
1186 | |||
1187 | /** |
||
1188 | * Updates the fields of a draft. |
||
1189 | * |
||
1190 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1191 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1192 | * |
||
1193 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
1194 | * |
||
1195 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
1196 | * or if a required field is missing / set to an empty value. |
||
1197 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
1198 | * or value is set for non-translatable field in language |
||
1199 | * other than main. |
||
1200 | * |
||
1201 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
1202 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1203 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
1204 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1205 | */ |
||
1206 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
1393 | |||
1394 | /** |
||
1395 | * Returns only updated language codes. |
||
1396 | * |
||
1397 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1398 | * |
||
1399 | * @return array |
||
1400 | */ |
||
1401 | private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) |
||
1417 | |||
1418 | /** |
||
1419 | * Returns all language codes used in given $fields. |
||
1420 | * |
||
1421 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
1422 | * |
||
1423 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1424 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
1425 | * |
||
1426 | * @return array |
||
1427 | */ |
||
1428 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
1440 | |||
1441 | /** |
||
1442 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
1443 | * |
||
1444 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
1445 | * or value is set for non-translatable field in language |
||
1446 | * other than main |
||
1447 | * |
||
1448 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
1449 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
1450 | * @param string $mainLanguageCode |
||
1451 | * |
||
1452 | * @return array |
||
1453 | */ |
||
1454 | protected function mapFieldsForUpdate( |
||
1492 | |||
1493 | /** |
||
1494 | * Publishes a content version. |
||
1495 | * |
||
1496 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1497 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1498 | * |
||
1499 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1500 | * @param string[] $translations |
||
1501 | * |
||
1502 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1503 | * |
||
1504 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1505 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1506 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1507 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
1508 | */ |
||
1509 | public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL) |
||
1552 | |||
1553 | /** |
||
1554 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1555 | * @param array $translations |
||
1556 | * |
||
1557 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
1558 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException |
||
1559 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException |
||
1560 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
1561 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
1562 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
1563 | */ |
||
1564 | protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionInfo, array $translations = []): void |
||
1613 | |||
1614 | /** |
||
1615 | * Publishes a content version. |
||
1616 | * |
||
1617 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
1618 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
1619 | * |
||
1620 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1621 | * |
||
1622 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1623 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
1624 | * |
||
1625 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1626 | */ |
||
1627 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
1677 | |||
1678 | /** |
||
1679 | * @return int |
||
1680 | */ |
||
1681 | protected function getUnixTimestamp() |
||
1685 | |||
1686 | /** |
||
1687 | * Removes the given version. |
||
1688 | * |
||
1689 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
1690 | * published state or is a last version of Content in non draft state |
||
1691 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
1692 | * |
||
1693 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1694 | */ |
||
1695 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
1737 | |||
1738 | /** |
||
1739 | * Loads all versions for the given content. |
||
1740 | * |
||
1741 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
1742 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the given status is invalid |
||
1743 | * |
||
1744 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1745 | * @param int|null $status |
||
1746 | * |
||
1747 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
1748 | */ |
||
1749 | public function loadVersions(ContentInfo $contentInfo, ?int $status = null) |
||
1778 | |||
1779 | /** |
||
1780 | * Copies the content to a new location. If no version is given, |
||
1781 | * all versions are copied, otherwise only the given version. |
||
1782 | * |
||
1783 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
1784 | * |
||
1785 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1786 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
1787 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1788 | * |
||
1789 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
1790 | */ |
||
1791 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
1846 | |||
1847 | /** |
||
1848 | * Loads all outgoing relations for the given version. |
||
1849 | * |
||
1850 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
1851 | * |
||
1852 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
1853 | * |
||
1854 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
1855 | */ |
||
1856 | public function loadRelations(APIVersionInfo $versionInfo) |
||
1891 | |||
1892 | /** |
||
1893 | * {@inheritdoc} |
||
1894 | */ |
||
1895 | public function countReverseRelations(ContentInfo $contentInfo): int |
||
1905 | |||
1906 | /** |
||
1907 | * Loads all incoming relations for a content object. |
||
1908 | * |
||
1909 | * The relations come only from published versions of the source content objects |
||
1910 | * |
||
1911 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
1912 | * |
||
1913 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
1914 | * |
||
1915 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
1916 | */ |
||
1917 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
1943 | |||
1944 | /** |
||
1945 | * Adds a relation of type common. |
||
1946 | * |
||
1947 | * The source of the relation is the content and version |
||
1948 | * referenced by $versionInfo. |
||
1949 | * |
||
1950 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
1951 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
1952 | * |
||
1953 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
1954 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
1955 | * |
||
1956 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
1957 | */ |
||
1958 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
1999 | |||
2000 | /** |
||
2001 | * Removes a relation of type COMMON from a draft. |
||
2002 | * |
||
2003 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
2004 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
2005 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
2006 | * |
||
2007 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
2008 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
2009 | */ |
||
2010 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
2060 | |||
2061 | /** |
||
2062 | * {@inheritdoc} |
||
2063 | */ |
||
2064 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
2072 | |||
2073 | /** |
||
2074 | * Delete Content item Translation from all Versions (including archived ones) of a Content Object. |
||
2075 | * |
||
2076 | * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it. |
||
2077 | * |
||
2078 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
2079 | * is the Main Translation of a Content Item. |
||
2080 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
2081 | * to delete the content (in one of the locations of the given Content Item). |
||
2082 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
2083 | * is invalid for the given content. |
||
2084 | * |
||
2085 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2086 | * @param string $languageCode |
||
2087 | * |
||
2088 | * @since 6.13 |
||
2089 | */ |
||
2090 | public function deleteTranslation(ContentInfo $contentInfo, $languageCode) |
||
2167 | |||
2168 | /** |
||
2169 | * Delete specified Translation from a Content Draft. |
||
2170 | * |
||
2171 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
2172 | * is the only one the Content Draft has or it is the main Translation of a Content Object. |
||
2173 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
2174 | * to edit the Content (in one of the locations of the given Content Object). |
||
2175 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
2176 | * is invalid for the given Draft. |
||
2177 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found |
||
2178 | * |
||
2179 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft |
||
2180 | * @param string $languageCode Language code of the Translation to be removed |
||
2181 | * |
||
2182 | * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation |
||
2183 | * |
||
2184 | * @since 6.12 |
||
2185 | */ |
||
2186 | public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) |
||
2252 | |||
2253 | /** |
||
2254 | * Hides Content by making all the Locations appear hidden. |
||
2255 | * It does not persist hidden state on Location object itself. |
||
2256 | * |
||
2257 | * Content hidden by this API can be revealed by revealContent API. |
||
2258 | * |
||
2259 | * @see revealContent |
||
2260 | * |
||
2261 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2262 | */ |
||
2263 | public function hideContent(ContentInfo $contentInfo): void |
||
2288 | |||
2289 | /** |
||
2290 | * Reveals Content hidden by hideContent API. |
||
2291 | * Locations which were hidden before hiding Content will remain hidden. |
||
2292 | * |
||
2293 | * @see hideContent |
||
2294 | * |
||
2295 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
2296 | */ |
||
2297 | public function revealContent(ContentInfo $contentInfo): void |
||
2322 | |||
2323 | /** |
||
2324 | * Instantiates a new content create struct object. |
||
2325 | * |
||
2326 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
2327 | * |
||
2328 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
2329 | * @param string $mainLanguageCode |
||
2330 | * |
||
2331 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
2332 | */ |
||
2333 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
2343 | |||
2344 | /** |
||
2345 | * Instantiates a new content meta data update struct. |
||
2346 | * |
||
2347 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
2348 | */ |
||
2349 | public function newContentMetadataUpdateStruct() |
||
2353 | |||
2354 | /** |
||
2355 | * Instantiates a new content update struct. |
||
2356 | * |
||
2357 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
2358 | */ |
||
2359 | public function newContentUpdateStruct() |
||
2363 | } |
||
2364 |
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.