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 |
||
| 55 | class ContentService implements ContentServiceInterface |
||
| 56 | { |
||
| 57 | /** @var \eZ\Publish\Core\Repository\Repository */ |
||
| 58 | protected $repository; |
||
| 59 | |||
| 60 | /** @var \eZ\Publish\SPI\Persistence\Handler */ |
||
| 61 | protected $persistenceHandler; |
||
| 62 | |||
| 63 | /** @var array */ |
||
| 64 | protected $settings; |
||
| 65 | |||
| 66 | /** @var \eZ\Publish\Core\Repository\Helper\DomainMapper */ |
||
| 67 | protected $domainMapper; |
||
| 68 | |||
| 69 | /** @var \eZ\Publish\Core\Repository\Helper\RelationProcessor */ |
||
| 70 | protected $relationProcessor; |
||
| 71 | |||
| 72 | /** @var \eZ\Publish\Core\Repository\Helper\NameSchemaService */ |
||
| 73 | protected $nameSchemaService; |
||
| 74 | |||
| 75 | /** @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry */ |
||
| 76 | protected $fieldTypeRegistry; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 80 | * |
||
| 81 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 82 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 83 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 84 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
| 85 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 86 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|
|||
| 87 | * @param array $settings |
||
| 88 | */ |
||
| 89 | public function __construct( |
||
| 90 | RepositoryInterface $repository, |
||
| 91 | Handler $handler, |
||
| 92 | Helper\DomainMapper $domainMapper, |
||
| 93 | Helper\RelationProcessor $relationProcessor, |
||
| 94 | Helper\NameSchemaService $nameSchemaService, |
||
| 95 | Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
| 96 | array $settings = [] |
||
| 97 | ) { |
||
| 98 | $this->repository = $repository; |
||
| 99 | $this->persistenceHandler = $handler; |
||
| 100 | $this->domainMapper = $domainMapper; |
||
| 101 | $this->relationProcessor = $relationProcessor; |
||
| 102 | $this->nameSchemaService = $nameSchemaService; |
||
| 103 | $this->fieldTypeRegistry = $fieldTypeRegistry; |
||
| 104 | // Union makes sure default settings are ignored if provided in argument |
||
| 105 | $this->settings = $settings + [ |
||
| 106 | // Version archive limit (0-50), only enforced on publish, not on un-publish. |
||
| 107 | 'default_version_archive_limit' => 5, |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Loads a content info object. |
||
| 113 | * |
||
| 114 | * To load fields use loadContent |
||
| 115 | * |
||
| 116 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 117 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 118 | * |
||
| 119 | * @param int $contentId |
||
| 120 | * |
||
| 121 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 122 | */ |
||
| 123 | public function loadContentInfo($contentId) |
||
| 124 | { |
||
| 125 | $contentInfo = $this->internalLoadContentInfo($contentId); |
||
| 126 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
| 127 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $contentInfo; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function loadContentInfoList(array $contentIds): iterable |
||
| 137 | { |
||
| 138 | $contentInfoList = []; |
||
| 139 | $spiInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds); |
||
| 140 | foreach ($spiInfoList as $id => $spiInfo) { |
||
| 141 | $contentInfo = $this->domainMapper->buildContentInfoDomainObject($spiInfo); |
||
| 142 | if ($this->repository->canUser('content', 'read', $contentInfo)) { |
||
| 143 | $contentInfoList[$id] = $contentInfo; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $contentInfoList; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Loads a content info object. |
||
| 152 | * |
||
| 153 | * To load fields use loadContent |
||
| 154 | * |
||
| 155 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 156 | * |
||
| 157 | * @param mixed $id |
||
| 158 | * @param bool $isRemoteId |
||
| 159 | * |
||
| 160 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 161 | */ |
||
| 162 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
| 163 | { |
||
| 164 | try { |
||
| 165 | $method = $isRemoteId ? 'loadContentInfoByRemoteId' : 'loadContentInfo'; |
||
| 166 | |||
| 167 | return $this->domainMapper->buildContentInfoDomainObject( |
||
| 168 | $this->persistenceHandler->contentHandler()->$method($id) |
||
| 169 | ); |
||
| 170 | } catch (APINotFoundException $e) { |
||
| 171 | throw new NotFoundException( |
||
| 172 | 'Content', |
||
| 173 | $id, |
||
| 174 | $e |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Loads a content info object for the given remoteId. |
||
| 181 | * |
||
| 182 | * To load fields use loadContent |
||
| 183 | * |
||
| 184 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 185 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
| 186 | * |
||
| 187 | * @param string $remoteId |
||
| 188 | * |
||
| 189 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 190 | */ |
||
| 191 | public function loadContentInfoByRemoteId($remoteId) |
||
| 192 | { |
||
| 193 | $contentInfo = $this->internalLoadContentInfo($remoteId, true); |
||
| 194 | |||
| 195 | if (!$this->repository->canUser('content', 'read', $contentInfo)) { |
||
| 196 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $contentInfo; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Loads a version info of the given content object. |
||
| 204 | * |
||
| 205 | * If no version number is given, the method returns the current version |
||
| 206 | * |
||
| 207 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 208 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 209 | * |
||
| 210 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 211 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 212 | * |
||
| 213 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 214 | */ |
||
| 215 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
| 216 | { |
||
| 217 | return $this->loadVersionInfoById($contentInfo->id, $versionNo); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Loads a version info of the given content object id. |
||
| 222 | * |
||
| 223 | * If no version number is given, the method returns the current version |
||
| 224 | * |
||
| 225 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 226 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 227 | * |
||
| 228 | * @param mixed $contentId |
||
| 229 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 230 | * |
||
| 231 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 232 | */ |
||
| 233 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
| 234 | { |
||
| 235 | try { |
||
| 236 | $spiVersionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo( |
||
| 237 | $contentId, |
||
| 238 | $versionNo |
||
| 239 | ); |
||
| 240 | } catch (APINotFoundException $e) { |
||
| 241 | throw new NotFoundException( |
||
| 242 | 'VersionInfo', |
||
| 243 | [ |
||
| 244 | 'contentId' => $contentId, |
||
| 245 | 'versionNo' => $versionNo, |
||
| 246 | ], |
||
| 247 | $e |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | |||
| 251 | $versionInfo = $this->domainMapper->buildVersionInfoDomainObject($spiVersionInfo); |
||
| 252 | |||
| 253 | if ($versionInfo->isPublished()) { |
||
| 254 | $function = 'read'; |
||
| 255 | } else { |
||
| 256 | $function = 'versionread'; |
||
| 257 | } |
||
| 258 | |||
| 259 | if (!$this->repository->canUser('content', $function, $versionInfo)) { |
||
| 260 | throw new UnauthorizedException('content', $function, ['contentId' => $contentId]); |
||
| 261 | } |
||
| 262 | |||
| 263 | return $versionInfo; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
| 270 | { |
||
| 271 | // Change $useAlwaysAvailable to false to avoid contentInfo lookup if we know alwaysAvailable is disabled |
||
| 272 | if ($useAlwaysAvailable && !$contentInfo->alwaysAvailable) { |
||
| 273 | $useAlwaysAvailable = false; |
||
| 274 | } |
||
| 275 | |||
| 276 | return $this->loadContent( |
||
| 277 | $contentInfo->id, |
||
| 278 | $languages, |
||
| 279 | $versionNo,// On purpose pass as-is and not use $contentInfo, to make sure to return actual current version on null |
||
| 280 | $useAlwaysAvailable |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
| 306 | { |
||
| 307 | $content = $this->internalLoadContent($contentId, $languages, $versionNo, false, $useAlwaysAvailable); |
||
| 308 | |||
| 309 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
| 310 | throw new UnauthorizedException('content', 'read', ['contentId' => $contentId]); |
||
| 311 | } |
||
| 312 | if ( |
||
| 313 | !$content->getVersionInfo()->isPublished() |
||
| 314 | && !$this->repository->canUser('content', 'versionread', $content) |
||
| 315 | ) { |
||
| 316 | throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId, 'versionNo' => $versionNo]); |
||
| 317 | } |
||
| 318 | |||
| 319 | return $content; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Loads content in a version of the given content object. |
||
| 324 | * |
||
| 325 | * If no version number is given, the method returns the current version |
||
| 326 | * |
||
| 327 | * @internal |
||
| 328 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
| 329 | * |
||
| 330 | * @param mixed $id |
||
| 331 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 332 | * returned value object. If not given all languages are returned. |
||
| 333 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
| 334 | * @param bool $isRemoteId |
||
| 335 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 336 | * |
||
| 337 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 338 | */ |
||
| 339 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
| 340 | { |
||
| 341 | try { |
||
| 342 | // Get Content ID if lookup by remote ID |
||
| 343 | if ($isRemoteId) { |
||
| 344 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($id); |
||
| 345 | $id = $spiContentInfo->id; |
||
| 346 | // Set $isRemoteId to false as the next loads will be for content id now that we have it (for exception use now) |
||
| 347 | $isRemoteId = false; |
||
| 348 | } |
||
| 349 | |||
| 350 | $loadLanguages = $languages; |
||
| 351 | $alwaysAvailableLanguageCode = null; |
||
| 352 | // Set main language on $languages filter if not empty (all) and $useAlwaysAvailable being true |
||
| 353 | // @todo Move use always available logic to SPI load methods, like done in location handler in 7.x |
||
| 354 | if (!empty($loadLanguages) && $useAlwaysAvailable) { |
||
| 355 | if (!isset($spiContentInfo)) { |
||
| 356 | $spiContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($id); |
||
| 357 | } |
||
| 358 | |||
| 359 | if ($spiContentInfo->alwaysAvailable) { |
||
| 360 | $loadLanguages[] = $alwaysAvailableLanguageCode = $spiContentInfo->mainLanguageCode; |
||
| 361 | $loadLanguages = array_unique($loadLanguages); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | $spiContent = $this->persistenceHandler->contentHandler()->load( |
||
| 366 | $id, |
||
| 367 | $versionNo, |
||
| 368 | $loadLanguages |
||
| 369 | ); |
||
| 370 | } catch (APINotFoundException $e) { |
||
| 371 | throw new NotFoundException( |
||
| 372 | 'Content', |
||
| 373 | [ |
||
| 374 | $isRemoteId ? 'remoteId' : 'id' => $id, |
||
| 375 | 'languages' => $languages, |
||
| 376 | 'versionNo' => $versionNo, |
||
| 377 | ], |
||
| 378 | $e |
||
| 379 | ); |
||
| 380 | } |
||
| 381 | |||
| 382 | return $this->domainMapper->buildContentDomainObject( |
||
| 383 | $spiContent, |
||
| 384 | $this->repository->getContentTypeService()->loadContentType( |
||
| 385 | $spiContent->versionInfo->contentInfo->contentTypeId |
||
| 386 | ), |
||
| 387 | $languages ?? [], |
||
| 388 | $alwaysAvailableLanguageCode |
||
| 389 | ); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Loads content in a version for the content object reference by the given remote id. |
||
| 394 | * |
||
| 395 | * If no version is given, the method returns the current version |
||
| 396 | * |
||
| 397 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
| 398 | * @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 |
||
| 399 | * |
||
| 400 | * @param string $remoteId |
||
| 401 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
| 402 | * @param int $versionNo the version number. If not given the current version is returned |
||
| 403 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 404 | * |
||
| 405 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 406 | */ |
||
| 407 | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
| 408 | { |
||
| 409 | $content = $this->internalLoadContent($remoteId, $languages, $versionNo, true, $useAlwaysAvailable); |
||
| 410 | |||
| 411 | if (!$this->repository->canUser('content', 'read', $content)) { |
||
| 412 | throw new UnauthorizedException('content', 'read', ['remoteId' => $remoteId]); |
||
| 413 | } |
||
| 414 | |||
| 415 | if ( |
||
| 416 | !$content->getVersionInfo()->isPublished() |
||
| 417 | && !$this->repository->canUser('content', 'versionread', $content) |
||
| 418 | ) { |
||
| 419 | throw new UnauthorizedException('content', 'versionread', ['remoteId' => $remoteId, 'versionNo' => $versionNo]); |
||
| 420 | } |
||
| 421 | |||
| 422 | return $content; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Bulk-load Content items by the list of ContentInfo Value Objects. |
||
| 427 | * |
||
| 428 | * Note: it does not throw exceptions on load, just ignores erroneous Content item. |
||
| 429 | * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is |
||
| 430 | * allowed to access every Content on the list. |
||
| 431 | * |
||
| 432 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList |
||
| 433 | * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 434 | * returned value object. If not given all languages are returned. |
||
| 435 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true, |
||
| 436 | * unless all languages have been asked for. |
||
| 437 | * |
||
| 438 | * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys |
||
| 439 | */ |
||
| 440 | public function loadContentListByContentInfo( |
||
| 441 | array $contentInfoList, |
||
| 442 | array $languages = [], |
||
| 443 | $useAlwaysAvailable = true |
||
| 444 | ) { |
||
| 445 | $loadAllLanguages = $languages === Language::ALL; |
||
| 446 | $contentIds = []; |
||
| 447 | $contentTypeIds = []; |
||
| 448 | $translations = $languages; |
||
| 449 | foreach ($contentInfoList as $contentInfo) { |
||
| 450 | $contentIds[] = $contentInfo->id; |
||
| 451 | $contentTypeIds[] = $contentInfo->contentTypeId; |
||
| 452 | // Unless we are told to load all languages, we add main language to translations so they are loaded too |
||
| 453 | // Might in some case load more languages then intended, but prioritised handling will pick right one |
||
| 454 | if (!$loadAllLanguages && $useAlwaysAvailable && $contentInfo->alwaysAvailable) { |
||
| 455 | $translations[] = $contentInfo->mainLanguageCode; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | $contentList = []; |
||
| 460 | $translations = array_unique($translations); |
||
| 461 | $spiContentList = $this->persistenceHandler->contentHandler()->loadContentList( |
||
| 462 | $contentIds, |
||
| 463 | $translations |
||
| 464 | ); |
||
| 465 | $contentTypeList = $this->repository->getContentTypeService()->loadContentTypeList( |
||
| 466 | array_unique($contentTypeIds), |
||
| 467 | $languages |
||
| 468 | ); |
||
| 469 | foreach ($spiContentList as $contentId => $spiContent) { |
||
| 470 | $contentInfo = $spiContent->versionInfo->contentInfo; |
||
| 471 | $contentList[$contentId] = $this->domainMapper->buildContentDomainObject( |
||
| 472 | $spiContent, |
||
| 473 | $contentTypeList[$contentInfo->contentTypeId], |
||
| 474 | $languages, |
||
| 475 | $contentInfo->alwaysAvailable ? $contentInfo->mainLanguageCode : null |
||
| 476 | ); |
||
| 477 | } |
||
| 478 | |||
| 479 | return $contentList; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Creates a new content draft assigned to the authenticated user. |
||
| 484 | * |
||
| 485 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
| 486 | * but this required special rights for the authenticated user |
||
| 487 | * (this is useful for content staging where the transfer process does not |
||
| 488 | * have to authenticate with the user which created the content object in the source server). |
||
| 489 | * The user has to publish the draft if it should be visible. |
||
| 490 | * In 4.x at least one location has to be provided in the location creation array. |
||
| 491 | * |
||
| 492 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
| 493 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
| 494 | * struct are missing or invalid, or if multiple locations are under the |
||
| 495 | * same parent. |
||
| 496 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 497 | * or if a required field is missing / set to an empty value. |
||
| 498 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 499 | * or value is set for non-translatable field in language |
||
| 500 | * other than main. |
||
| 501 | * |
||
| 502 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 503 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
| 504 | * |
||
| 505 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 506 | */ |
||
| 507 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) |
||
| 508 | { |
||
| 509 | if ($contentCreateStruct->mainLanguageCode === null) { |
||
| 510 | throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); |
||
| 511 | } |
||
| 512 | |||
| 513 | if ($contentCreateStruct->contentType === null) { |
||
| 514 | throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set"); |
||
| 515 | } |
||
| 516 | |||
| 517 | $contentCreateStruct = clone $contentCreateStruct; |
||
| 518 | |||
| 519 | if ($contentCreateStruct->ownerId === null) { |
||
| 520 | $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId(); |
||
| 521 | } |
||
| 522 | |||
| 523 | if ($contentCreateStruct->alwaysAvailable === null) { |
||
| 524 | $contentCreateStruct->alwaysAvailable = $contentCreateStruct->contentType->defaultAlwaysAvailable ?: false; |
||
| 525 | } |
||
| 526 | |||
| 527 | $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType( |
||
| 528 | $contentCreateStruct->contentType->id |
||
| 529 | ); |
||
| 530 | |||
| 531 | if (empty($contentCreateStruct->sectionId)) { |
||
| 532 | if (isset($locationCreateStructs[0])) { |
||
| 533 | $location = $this->repository->getLocationService()->loadLocation( |
||
| 534 | $locationCreateStructs[0]->parentLocationId |
||
| 535 | ); |
||
| 536 | $contentCreateStruct->sectionId = $location->contentInfo->sectionId; |
||
| 537 | } else { |
||
| 538 | $contentCreateStruct->sectionId = 1; |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) { |
||
| 543 | throw new UnauthorizedException( |
||
| 544 | 'content', |
||
| 545 | 'create', |
||
| 546 | [ |
||
| 547 | 'parentLocationId' => isset($locationCreateStructs[0]) ? |
||
| 548 | $locationCreateStructs[0]->parentLocationId : |
||
| 549 | null, |
||
| 550 | 'sectionId' => $contentCreateStruct->sectionId, |
||
| 551 | ] |
||
| 552 | ); |
||
| 553 | } |
||
| 554 | |||
| 555 | if (!empty($contentCreateStruct->remoteId)) { |
||
| 556 | try { |
||
| 557 | $this->loadContentByRemoteId($contentCreateStruct->remoteId); |
||
| 558 | |||
| 559 | throw new InvalidArgumentException( |
||
| 560 | '$contentCreateStruct', |
||
| 561 | "Another content with remoteId '{$contentCreateStruct->remoteId}' exists" |
||
| 562 | ); |
||
| 563 | } catch (APINotFoundException $e) { |
||
| 564 | // Do nothing |
||
| 565 | } |
||
| 566 | } else { |
||
| 567 | $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct); |
||
| 568 | } |
||
| 569 | |||
| 570 | $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs); |
||
| 571 | |||
| 572 | $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct); |
||
| 573 | $fields = $this->mapFieldsForCreate($contentCreateStruct); |
||
| 574 | |||
| 575 | $fieldValues = []; |
||
| 576 | $spiFields = []; |
||
| 577 | $allFieldErrors = []; |
||
| 578 | $inputRelations = []; |
||
| 579 | $locationIdToContentIdMapping = []; |
||
| 580 | |||
| 581 | foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
||
| 582 | /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */ |
||
| 583 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
| 584 | $fieldDefinition->fieldTypeIdentifier |
||
| 585 | ); |
||
| 586 | |||
| 587 | foreach ($languageCodes as $languageCode) { |
||
| 588 | $isEmptyValue = false; |
||
| 589 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode; |
||
| 590 | $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode; |
||
| 591 | if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) { |
||
| 592 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
| 593 | } else { |
||
| 594 | $fieldValue = $fieldDefinition->defaultValue; |
||
| 595 | } |
||
| 596 | |||
| 597 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
| 598 | |||
| 599 | if ($fieldType->isEmptyValue($fieldValue)) { |
||
| 600 | $isEmptyValue = true; |
||
| 601 | if ($fieldDefinition->isRequired) { |
||
| 602 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
| 603 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
| 604 | null, |
||
| 605 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
| 606 | 'empty' |
||
| 607 | ); |
||
| 608 | } |
||
| 609 | } else { |
||
| 610 | $fieldErrors = $fieldType->validate( |
||
| 611 | $fieldDefinition, |
||
| 612 | $fieldValue |
||
| 613 | ); |
||
| 614 | if (!empty($fieldErrors)) { |
||
| 615 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
| 616 | } |
||
| 617 | } |
||
| 618 | |||
| 619 | if (!empty($allFieldErrors)) { |
||
| 620 | continue; |
||
| 621 | } |
||
| 622 | |||
| 623 | $this->relationProcessor->appendFieldRelations( |
||
| 624 | $inputRelations, |
||
| 625 | $locationIdToContentIdMapping, |
||
| 626 | $fieldType, |
||
| 627 | $fieldValue, |
||
| 628 | $fieldDefinition->id |
||
| 629 | ); |
||
| 630 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
| 631 | |||
| 632 | // Only non-empty value for: translatable field or in main language |
||
| 633 | if ( |
||
| 634 | (!$isEmptyValue && $fieldDefinition->isTranslatable) || |
||
| 635 | (!$isEmptyValue && $isLanguageMain) |
||
| 636 | ) { |
||
| 637 | $spiFields[] = new SPIField( |
||
| 638 | [ |
||
| 639 | 'id' => null, |
||
| 640 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
| 641 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
| 642 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
| 643 | 'languageCode' => $languageCode, |
||
| 644 | 'versionNo' => null, |
||
| 645 | ] |
||
| 646 | ); |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 | |||
| 651 | if (!empty($allFieldErrors)) { |
||
| 652 | throw new ContentFieldValidationException($allFieldErrors); |
||
| 653 | } |
||
| 654 | |||
| 655 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
| 656 | [ |
||
| 657 | 'name' => $this->nameSchemaService->resolve( |
||
| 658 | $contentCreateStruct->contentType->nameSchema, |
||
| 659 | $contentCreateStruct->contentType, |
||
| 660 | $fieldValues, |
||
| 661 | $languageCodes |
||
| 662 | ), |
||
| 663 | 'typeId' => $contentCreateStruct->contentType->id, |
||
| 664 | 'sectionId' => $contentCreateStruct->sectionId, |
||
| 665 | 'ownerId' => $contentCreateStruct->ownerId, |
||
| 666 | 'locations' => $spiLocationCreateStructs, |
||
| 667 | 'fields' => $spiFields, |
||
| 668 | 'alwaysAvailable' => $contentCreateStruct->alwaysAvailable, |
||
| 669 | 'remoteId' => $contentCreateStruct->remoteId, |
||
| 670 | 'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), |
||
| 671 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
| 672 | $contentCreateStruct->mainLanguageCode |
||
| 673 | )->id, |
||
| 674 | ] |
||
| 675 | ); |
||
| 676 | |||
| 677 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
| 678 | |||
| 679 | $this->repository->beginTransaction(); |
||
| 680 | try { |
||
| 681 | $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct); |
||
| 682 | $this->relationProcessor->processFieldRelations( |
||
| 683 | $inputRelations, |
||
| 684 | $spiContent->versionInfo->contentInfo->id, |
||
| 685 | $spiContent->versionInfo->versionNo, |
||
| 686 | $contentCreateStruct->contentType |
||
| 687 | ); |
||
| 688 | |||
| 689 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
| 690 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
| 691 | $objectStateHandler->setContentState( |
||
| 692 | $spiContent->versionInfo->contentInfo->id, |
||
| 693 | $objectStateGroupId, |
||
| 694 | $objectState->id |
||
| 695 | ); |
||
| 696 | } |
||
| 697 | |||
| 698 | $this->repository->commit(); |
||
| 699 | } catch (Exception $e) { |
||
| 700 | $this->repository->rollback(); |
||
| 701 | throw $e; |
||
| 702 | } |
||
| 703 | |||
| 704 | return $this->domainMapper->buildContentDomainObject( |
||
| 705 | $spiContent, |
||
| 706 | $contentCreateStruct->contentType |
||
| 707 | ); |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Returns an array of default content states with content state group id as key. |
||
| 712 | * |
||
| 713 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 714 | */ |
||
| 715 | protected function getDefaultObjectStates() |
||
| 716 | { |
||
| 717 | $defaultObjectStatesMap = []; |
||
| 718 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
| 719 | |||
| 720 | foreach ($objectStateHandler->loadAllGroups() as $objectStateGroup) { |
||
| 721 | foreach ($objectStateHandler->loadObjectStates($objectStateGroup->id) as $objectState) { |
||
| 722 | // Only register the first object state which is the default one. |
||
| 723 | $defaultObjectStatesMap[$objectStateGroup->id] = $objectState; |
||
| 724 | break; |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | return $defaultObjectStatesMap; |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Returns all language codes used in given $fields. |
||
| 733 | * |
||
| 734 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
| 735 | * |
||
| 736 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 737 | * |
||
| 738 | * @return string[] |
||
| 739 | */ |
||
| 740 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 741 | { |
||
| 742 | $languageCodes = []; |
||
| 743 | |||
| 744 | foreach ($contentCreateStruct->fields as $field) { |
||
| 745 | if ($field->languageCode === null || isset($languageCodes[$field->languageCode])) { |
||
| 746 | continue; |
||
| 747 | } |
||
| 748 | |||
| 749 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
| 750 | $field->languageCode |
||
| 751 | ); |
||
| 752 | $languageCodes[$field->languageCode] = true; |
||
| 753 | } |
||
| 754 | |||
| 755 | if (!isset($languageCodes[$contentCreateStruct->mainLanguageCode])) { |
||
| 756 | $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
| 757 | $contentCreateStruct->mainLanguageCode |
||
| 758 | ); |
||
| 759 | $languageCodes[$contentCreateStruct->mainLanguageCode] = true; |
||
| 760 | } |
||
| 761 | |||
| 762 | return array_keys($languageCodes); |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 767 | * |
||
| 768 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 769 | * or value is set for non-translatable field in language |
||
| 770 | * other than main |
||
| 771 | * |
||
| 772 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 773 | * |
||
| 774 | * @return array |
||
| 775 | */ |
||
| 776 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 777 | { |
||
| 778 | $fields = []; |
||
| 779 | |||
| 780 | foreach ($contentCreateStruct->fields as $field) { |
||
| 781 | $fieldDefinition = $contentCreateStruct->contentType->getFieldDefinition($field->fieldDefIdentifier); |
||
| 782 | |||
| 783 | if ($fieldDefinition === null) { |
||
| 784 | throw new ContentValidationException( |
||
| 785 | "Field definition '%identifier%' does not exist in given ContentType", |
||
| 786 | ['%identifier%' => $field->fieldDefIdentifier] |
||
| 787 | ); |
||
| 788 | } |
||
| 789 | |||
| 790 | if ($field->languageCode === null) { |
||
| 791 | $field = $this->cloneField( |
||
| 792 | $field, |
||
| 793 | ['languageCode' => $contentCreateStruct->mainLanguageCode] |
||
| 794 | ); |
||
| 795 | } |
||
| 796 | |||
| 797 | if (!$fieldDefinition->isTranslatable && ($field->languageCode != $contentCreateStruct->mainLanguageCode)) { |
||
| 798 | throw new ContentValidationException( |
||
| 799 | "A value is set for non translatable field definition '%identifier%' with language '%languageCode%'", |
||
| 800 | ['%identifier%' => $field->fieldDefIdentifier, '%languageCode%' => $field->languageCode] |
||
| 801 | ); |
||
| 802 | } |
||
| 803 | |||
| 804 | $fields[$field->fieldDefIdentifier][$field->languageCode] = $field; |
||
| 805 | } |
||
| 806 | |||
| 807 | return $fields; |
||
| 808 | } |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Clones $field with overriding specific properties from given $overrides array. |
||
| 812 | * |
||
| 813 | * @param Field $field |
||
| 814 | * @param array $overrides |
||
| 815 | * |
||
| 816 | * @return Field |
||
| 817 | */ |
||
| 818 | private function cloneField(Field $field, array $overrides = []) |
||
| 819 | { |
||
| 820 | $fieldData = array_merge( |
||
| 821 | [ |
||
| 822 | 'id' => $field->id, |
||
| 823 | 'value' => $field->value, |
||
| 824 | 'languageCode' => $field->languageCode, |
||
| 825 | 'fieldDefIdentifier' => $field->fieldDefIdentifier, |
||
| 826 | 'fieldTypeIdentifier' => $field->fieldTypeIdentifier, |
||
| 827 | ], |
||
| 828 | $overrides |
||
| 829 | ); |
||
| 830 | |||
| 831 | return new Field($fieldData); |
||
| 832 | } |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 836 | * |
||
| 837 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
| 838 | * |
||
| 839 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
| 840 | */ |
||
| 841 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
| 842 | { |
||
| 843 | $spiLocationCreateStructs = []; |
||
| 844 | $parentLocationIdSet = []; |
||
| 845 | $mainLocation = true; |
||
| 846 | |||
| 847 | foreach ($locationCreateStructs as $locationCreateStruct) { |
||
| 848 | if (isset($parentLocationIdSet[$locationCreateStruct->parentLocationId])) { |
||
| 849 | throw new InvalidArgumentException( |
||
| 850 | '$locationCreateStructs', |
||
| 851 | "Multiple LocationCreateStructs with the same parent Location '{$locationCreateStruct->parentLocationId}' are given" |
||
| 852 | ); |
||
| 853 | } |
||
| 854 | |||
| 855 | if (!array_key_exists($locationCreateStruct->sortField, Location::SORT_FIELD_MAP)) { |
||
| 856 | $locationCreateStruct->sortField = Location::SORT_FIELD_NAME; |
||
| 857 | } |
||
| 858 | |||
| 859 | if (!array_key_exists($locationCreateStruct->sortOrder, Location::SORT_ORDER_MAP)) { |
||
| 860 | $locationCreateStruct->sortOrder = Location::SORT_ORDER_ASC; |
||
| 861 | } |
||
| 862 | |||
| 863 | $parentLocationIdSet[$locationCreateStruct->parentLocationId] = true; |
||
| 864 | $parentLocation = $this->repository->getLocationService()->loadLocation( |
||
| 865 | $locationCreateStruct->parentLocationId |
||
| 866 | ); |
||
| 867 | |||
| 868 | $spiLocationCreateStructs[] = $this->domainMapper->buildSPILocationCreateStruct( |
||
| 869 | $locationCreateStruct, |
||
| 870 | $parentLocation, |
||
| 871 | $mainLocation, |
||
| 872 | // For Content draft contentId and contentVersionNo are set in ContentHandler upon draft creation |
||
| 873 | null, |
||
| 874 | null |
||
| 875 | ); |
||
| 876 | |||
| 877 | // First Location in the list will be created as main Location |
||
| 878 | $mainLocation = false; |
||
| 879 | } |
||
| 880 | |||
| 881 | return $spiLocationCreateStructs; |
||
| 882 | } |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Updates the metadata. |
||
| 886 | * |
||
| 887 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
| 888 | * |
||
| 889 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
| 890 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
| 891 | * |
||
| 892 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 893 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
| 894 | * |
||
| 895 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
| 896 | */ |
||
| 897 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
| 898 | { |
||
| 899 | $propertyCount = 0; |
||
| 900 | foreach ($contentMetadataUpdateStruct as $propertyName => $propertyValue) { |
||
| 901 | if (isset($contentMetadataUpdateStruct->$propertyName)) { |
||
| 902 | $propertyCount += 1; |
||
| 903 | } |
||
| 904 | } |
||
| 905 | if ($propertyCount === 0) { |
||
| 906 | throw new InvalidArgumentException( |
||
| 907 | '$contentMetadataUpdateStruct', |
||
| 908 | 'At least one property must be set' |
||
| 909 | ); |
||
| 910 | } |
||
| 911 | |||
| 912 | $loadedContentInfo = $this->loadContentInfo($contentInfo->id); |
||
| 913 | |||
| 914 | if (!$this->repository->canUser('content', 'edit', $loadedContentInfo)) { |
||
| 915 | throw new UnauthorizedException('content', 'edit', ['contentId' => $loadedContentInfo->id]); |
||
| 916 | } |
||
| 917 | |||
| 918 | if (isset($contentMetadataUpdateStruct->remoteId)) { |
||
| 919 | try { |
||
| 920 | $existingContentInfo = $this->loadContentInfoByRemoteId($contentMetadataUpdateStruct->remoteId); |
||
| 921 | |||
| 922 | if ($existingContentInfo->id !== $loadedContentInfo->id) { |
||
| 923 | throw new InvalidArgumentException( |
||
| 924 | '$contentMetadataUpdateStruct', |
||
| 925 | "Another content with remoteId '{$contentMetadataUpdateStruct->remoteId}' exists" |
||
| 926 | ); |
||
| 927 | } |
||
| 928 | } catch (APINotFoundException $e) { |
||
| 929 | // Do nothing |
||
| 930 | } |
||
| 931 | } |
||
| 932 | |||
| 933 | $this->repository->beginTransaction(); |
||
| 934 | try { |
||
| 935 | if ($propertyCount > 1 || !isset($contentMetadataUpdateStruct->mainLocationId)) { |
||
| 936 | $this->persistenceHandler->contentHandler()->updateMetadata( |
||
| 937 | $loadedContentInfo->id, |
||
| 938 | new SPIMetadataUpdateStruct( |
||
| 939 | [ |
||
| 940 | 'ownerId' => $contentMetadataUpdateStruct->ownerId, |
||
| 941 | 'publicationDate' => isset($contentMetadataUpdateStruct->publishedDate) ? |
||
| 942 | $contentMetadataUpdateStruct->publishedDate->getTimestamp() : |
||
| 943 | null, |
||
| 944 | 'modificationDate' => isset($contentMetadataUpdateStruct->modificationDate) ? |
||
| 945 | $contentMetadataUpdateStruct->modificationDate->getTimestamp() : |
||
| 946 | null, |
||
| 947 | 'mainLanguageId' => isset($contentMetadataUpdateStruct->mainLanguageCode) ? |
||
| 948 | $this->repository->getContentLanguageService()->loadLanguage( |
||
| 949 | $contentMetadataUpdateStruct->mainLanguageCode |
||
| 950 | )->id : |
||
| 951 | null, |
||
| 952 | 'alwaysAvailable' => $contentMetadataUpdateStruct->alwaysAvailable, |
||
| 953 | 'remoteId' => $contentMetadataUpdateStruct->remoteId, |
||
| 954 | 'name' => $contentMetadataUpdateStruct->name, |
||
| 955 | ] |
||
| 956 | ) |
||
| 957 | ); |
||
| 958 | } |
||
| 959 | |||
| 960 | // Change main location |
||
| 961 | if (isset($contentMetadataUpdateStruct->mainLocationId) |
||
| 962 | && $loadedContentInfo->mainLocationId !== $contentMetadataUpdateStruct->mainLocationId) { |
||
| 963 | $this->persistenceHandler->locationHandler()->changeMainLocation( |
||
| 964 | $loadedContentInfo->id, |
||
| 965 | $contentMetadataUpdateStruct->mainLocationId |
||
| 966 | ); |
||
| 967 | } |
||
| 968 | |||
| 969 | // Republish URL aliases to update always-available flag |
||
| 970 | if (isset($contentMetadataUpdateStruct->alwaysAvailable) |
||
| 971 | && $loadedContentInfo->alwaysAvailable !== $contentMetadataUpdateStruct->alwaysAvailable) { |
||
| 972 | $content = $this->loadContent($loadedContentInfo->id); |
||
| 973 | $this->publishUrlAliasesForContent($content, false); |
||
| 974 | } |
||
| 975 | |||
| 976 | $this->repository->commit(); |
||
| 977 | } catch (Exception $e) { |
||
| 978 | $this->repository->rollback(); |
||
| 979 | throw $e; |
||
| 980 | } |
||
| 981 | |||
| 982 | return isset($content) ? $content : $this->loadContent($loadedContentInfo->id); |
||
| 983 | } |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Publishes URL aliases for all locations of a given content. |
||
| 987 | * |
||
| 988 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 989 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
| 990 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
| 991 | */ |
||
| 992 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Deletes a content object including all its versions and locations including their subtrees. |
||
| 1021 | * |
||
| 1022 | * @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) |
||
| 1023 | * |
||
| 1024 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1025 | * |
||
| 1026 | * @return mixed[] Affected Location Id's |
||
| 1027 | */ |
||
| 1028 | public function deleteContent(ContentInfo $contentInfo) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Creates a draft from a published or archived version. |
||
| 1058 | * |
||
| 1059 | * If no version is given, the current published version is used. |
||
| 1060 | * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
| 1061 | * It can be changed on updating the version. |
||
| 1062 | * |
||
| 1063 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1064 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1065 | * @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 |
||
| 1066 | * |
||
| 1067 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 1068 | * |
||
| 1069 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
| 1070 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the current-user is not allowed to create the draft |
||
| 1071 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
| 1072 | */ |
||
| 1073 | public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null) |
||
| 1074 | { |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * {@inheritdoc} |
||
| 1156 | */ |
||
| 1157 | public function countContentDrafts(?User $user = null): int |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Loads drafts for a user. |
||
| 1170 | * |
||
| 1171 | * If no user is given the drafts for the authenticated user are returned |
||
| 1172 | * |
||
| 1173 | * @param \eZ\Publish\API\Repository\Values\User\User|null $user |
||
| 1174 | * |
||
| 1175 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Drafts owned by the given user |
||
| 1176 | * |
||
| 1177 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 1178 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 1179 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 1180 | */ |
||
| 1181 | public function loadContentDrafts(User $user = null) |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * {@inheritdoc} |
||
| 1207 | */ |
||
| 1208 | public function loadContentDraftList(?User $user = null, int $offset = 0, int $limit = -1): ContentDraftList |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Updates the fields of a draft. |
||
| 1243 | * |
||
| 1244 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1245 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1246 | * |
||
| 1247 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
| 1248 | * |
||
| 1249 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 1250 | * or if a required field is missing / set to an empty value. |
||
| 1251 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 1252 | * or value is set for non-translatable field in language |
||
| 1253 | * other than main. |
||
| 1254 | * |
||
| 1255 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
| 1256 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1257 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
| 1258 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 1259 | */ |
||
| 1260 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * Returns only updated language codes. |
||
| 1450 | * |
||
| 1451 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1452 | * |
||
| 1453 | * @return array |
||
| 1454 | */ |
||
| 1455 | private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) |
||
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Returns all language codes used in given $fields. |
||
| 1474 | * |
||
| 1475 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
| 1476 | * |
||
| 1477 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1478 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 1479 | * |
||
| 1480 | * @return array |
||
| 1481 | */ |
||
| 1482 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 1497 | * |
||
| 1498 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 1499 | * or value is set for non-translatable field in language |
||
| 1500 | * other than main |
||
| 1501 | * |
||
| 1502 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1503 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 1504 | * @param string $mainLanguageCode |
||
| 1505 | * |
||
| 1506 | * @return array |
||
| 1507 | */ |
||
| 1508 | protected function mapFieldsForUpdate( |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Publishes a content version. |
||
| 1549 | * |
||
| 1550 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1551 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1552 | * |
||
| 1553 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1554 | * @param string[] $translations |
||
| 1555 | * |
||
| 1556 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1557 | * |
||
| 1558 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1559 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 1560 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 1561 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 1562 | */ |
||
| 1563 | public function publishVersion(APIVersionInfo $versionInfo, array $translations = Language::ALL) |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1609 | * @param array $translations |
||
| 1610 | * |
||
| 1611 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 1612 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException |
||
| 1613 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException |
||
| 1614 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 1615 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 1616 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 1617 | */ |
||
| 1618 | protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionInfo, array $translations = []): void |
||
| 1667 | |||
| 1668 | /** |
||
| 1669 | * Publishes a content version. |
||
| 1670 | * |
||
| 1671 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1672 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1673 | * |
||
| 1674 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1675 | * |
||
| 1676 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1677 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
| 1678 | * |
||
| 1679 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1680 | */ |
||
| 1681 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
| 1731 | |||
| 1732 | /** |
||
| 1733 | * @return int |
||
| 1734 | */ |
||
| 1735 | protected function getUnixTimestamp() |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Removes the given version. |
||
| 1742 | * |
||
| 1743 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
| 1744 | * published state or is a last version of Content in non draft state |
||
| 1745 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
| 1746 | * |
||
| 1747 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1748 | */ |
||
| 1749 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
| 1791 | |||
| 1792 | /** |
||
| 1793 | * Loads all versions for the given content. |
||
| 1794 | * |
||
| 1795 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
| 1796 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the given status is invalid |
||
| 1797 | * |
||
| 1798 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1799 | * @param int|null $status |
||
| 1800 | * |
||
| 1801 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
| 1802 | */ |
||
| 1803 | public function loadVersions(ContentInfo $contentInfo, ?int $status = null) |
||
| 1832 | |||
| 1833 | /** |
||
| 1834 | * Copies the content to a new location. If no version is given, |
||
| 1835 | * all versions are copied, otherwise only the given version. |
||
| 1836 | * |
||
| 1837 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
| 1838 | * |
||
| 1839 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1840 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
| 1841 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1842 | * |
||
| 1843 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1844 | */ |
||
| 1845 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Loads all outgoing relations for the given version. |
||
| 1903 | * |
||
| 1904 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1905 | * |
||
| 1906 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1907 | * |
||
| 1908 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1909 | */ |
||
| 1910 | public function loadRelations(APIVersionInfo $versionInfo) |
||
| 1945 | |||
| 1946 | /** |
||
| 1947 | * {@inheritdoc} |
||
| 1948 | */ |
||
| 1949 | public function countReverseRelations(ContentInfo $contentInfo): int |
||
| 1959 | |||
| 1960 | /** |
||
| 1961 | * Loads all incoming relations for a content object. |
||
| 1962 | * |
||
| 1963 | * The relations come only from published versions of the source content objects |
||
| 1964 | * |
||
| 1965 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1966 | * |
||
| 1967 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1968 | * |
||
| 1969 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1970 | */ |
||
| 1971 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * Adds a relation of type common. |
||
| 2000 | * |
||
| 2001 | * The source of the relation is the content and version |
||
| 2002 | * referenced by $versionInfo. |
||
| 2003 | * |
||
| 2004 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
| 2005 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 2006 | * |
||
| 2007 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 2008 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
| 2009 | * |
||
| 2010 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
| 2011 | */ |
||
| 2012 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Removes a relation of type COMMON from a draft. |
||
| 2056 | * |
||
| 2057 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
| 2058 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 2059 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
| 2060 | * |
||
| 2061 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 2062 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
| 2063 | */ |
||
| 2064 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 2114 | |||
| 2115 | /** |
||
| 2116 | * {@inheritdoc} |
||
| 2117 | */ |
||
| 2118 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 2126 | |||
| 2127 | /** |
||
| 2128 | * Delete Content item Translation from all Versions (including archived ones) of a Content Object. |
||
| 2129 | * |
||
| 2130 | * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it. |
||
| 2131 | * |
||
| 2132 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 2133 | * is the Main Translation of a Content Item. |
||
| 2134 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 2135 | * to delete the content (in one of the locations of the given Content Item). |
||
| 2136 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 2137 | * is invalid for the given content. |
||
| 2138 | * |
||
| 2139 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 2140 | * @param string $languageCode |
||
| 2141 | * |
||
| 2142 | * @since 6.13 |
||
| 2143 | */ |
||
| 2144 | public function deleteTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 2221 | |||
| 2222 | /** |
||
| 2223 | * Delete specified Translation from a Content Draft. |
||
| 2224 | * |
||
| 2225 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 2226 | * is the only one the Content Draft has or it is the main Translation of a Content Object. |
||
| 2227 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 2228 | * to edit the Content (in one of the locations of the given Content Object). |
||
| 2229 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 2230 | * is invalid for the given Draft. |
||
| 2231 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found |
||
| 2232 | * |
||
| 2233 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft |
||
| 2234 | * @param string $languageCode Language code of the Translation to be removed |
||
| 2235 | * |
||
| 2236 | * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation |
||
| 2237 | * |
||
| 2238 | * @since 6.12 |
||
| 2239 | */ |
||
| 2240 | public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) |
||
| 2306 | |||
| 2307 | /** |
||
| 2308 | * Hides Content by making all the Locations appear hidden. |
||
| 2309 | * It does not persist hidden state on Location object itself. |
||
| 2310 | * |
||
| 2311 | * Content hidden by this API can be revealed by revealContent API. |
||
| 2312 | * |
||
| 2313 | * @see revealContent |
||
| 2314 | * |
||
| 2315 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 2316 | */ |
||
| 2317 | public function hideContent(ContentInfo $contentInfo): void |
||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Reveals Content hidden by hideContent API. |
||
| 2345 | * Locations which were hidden before hiding Content will remain hidden. |
||
| 2346 | * |
||
| 2347 | * @see hideContent |
||
| 2348 | * |
||
| 2349 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 2350 | */ |
||
| 2351 | public function revealContent(ContentInfo $contentInfo): void |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * Instantiates a new content create struct object. |
||
| 2379 | * |
||
| 2380 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
| 2381 | * |
||
| 2382 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 2383 | * @param string $mainLanguageCode |
||
| 2384 | * |
||
| 2385 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
| 2386 | */ |
||
| 2387 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
| 2397 | |||
| 2398 | /** |
||
| 2399 | * Instantiates a new content meta data update struct. |
||
| 2400 | * |
||
| 2401 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
| 2402 | */ |
||
| 2403 | public function newContentMetadataUpdateStruct() |
||
| 2407 | |||
| 2408 | /** |
||
| 2409 | * Instantiates a new content update struct. |
||
| 2410 | * |
||
| 2411 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
| 2412 | */ |
||
| 2413 | public function newContentUpdateStruct() |
||
| 2417 | |||
| 2418 | /** |
||
| 2419 | * @param \eZ\Publish\API\Repository\Values\User\User|null $user |
||
| 2420 | * |
||
| 2421 | * @return \eZ\Publish\API\Repository\Values\User\UserReference |
||
| 2422 | */ |
||
| 2423 | private function resolveUser(?User $user): UserReference |
||
| 2431 | } |
||
| 2432 |
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.