Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 50 | class ContentService implements ContentServiceInterface |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 54 | */ |
||
| 55 | protected $repository; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 59 | */ |
||
| 60 | protected $persistenceHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $settings; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 69 | */ |
||
| 70 | protected $domainMapper; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
| 74 | */ |
||
| 75 | protected $relationProcessor; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 79 | */ |
||
| 80 | protected $nameSchemaService; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
| 84 | */ |
||
| 85 | protected $fieldTypeRegistry; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 89 | * |
||
| 90 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 91 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 92 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 93 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
| 94 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 95 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|
|||
| 96 | * @param array $settings |
||
| 97 | */ |
||
| 98 | public function __construct( |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Loads a content info object. |
||
| 122 | * |
||
| 123 | * To load fields use loadContent |
||
| 124 | * |
||
| 125 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 126 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 127 | * |
||
| 128 | * @param int $contentId |
||
| 129 | * |
||
| 130 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function loadContentInfo($contentId) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Loads a content info object. |
||
| 144 | * |
||
| 145 | * To load fields use loadContent |
||
| 146 | * |
||
| 147 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 148 | * |
||
| 149 | * @param mixed $id |
||
| 150 | * @param bool $isRemoteId |
||
| 151 | * |
||
| 152 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 153 | */ |
||
| 154 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Loads a content info object for the given remoteId. |
||
| 173 | * |
||
| 174 | * To load fields use loadContent |
||
| 175 | * |
||
| 176 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
| 178 | * |
||
| 179 | * @param string $remoteId |
||
| 180 | * |
||
| 181 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Loads a version info of the given content object. |
||
| 196 | * |
||
| 197 | * If no version number is given, the method returns the current version |
||
| 198 | * |
||
| 199 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 200 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 201 | * |
||
| 202 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 203 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 204 | * |
||
| 205 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 206 | */ |
||
| 207 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Loads a version info of the given content object id. |
||
| 214 | * |
||
| 215 | * If no version number is given, the method returns the current version |
||
| 216 | * |
||
| 217 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 218 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 219 | * |
||
| 220 | * @param mixed $contentId |
||
| 221 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 222 | * |
||
| 223 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 224 | */ |
||
| 225 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | View Code Duplication | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Loads content in a version of the given content object. |
||
| 325 | * |
||
| 326 | * If no version number is given, the method returns the current version |
||
| 327 | * |
||
| 328 | * @internal |
||
| 329 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
| 330 | * |
||
| 331 | * @param mixed $id |
||
| 332 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 333 | * returned value object. If not given all languages are returned. |
||
| 334 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
| 335 | * @param bool $isRemoteId |
||
| 336 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 337 | * |
||
| 338 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 339 | */ |
||
| 340 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Loads content in a version for the content object reference by the given remote id. |
||
| 401 | * |
||
| 402 | * If no version is given, the method returns the current version |
||
| 403 | * |
||
| 404 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
| 405 | * @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 |
||
| 406 | * |
||
| 407 | * @param string $remoteId |
||
| 408 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
| 409 | * @param int $versionNo the version number. If not given the current version is returned |
||
| 410 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 411 | * |
||
| 412 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 413 | */ |
||
| 414 | View Code Duplication | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Bulk-load Content items by the list of ContentInfo Value Objects. |
||
| 434 | * |
||
| 435 | * Note: it does not throw exceptions on load, just ignores erroneous Content item. |
||
| 436 | * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is |
||
| 437 | * allowed to access every Content on the list. |
||
| 438 | * |
||
| 439 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList |
||
| 440 | * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 441 | * returned value object. If not given all languages are returned. |
||
| 442 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true, |
||
| 443 | * unless all languages have been asked for. |
||
| 444 | * |
||
| 445 | * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys |
||
| 446 | */ |
||
| 447 | public function loadContentListByContentInfo( |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Creates a new content draft assigned to the authenticated user. |
||
| 485 | * |
||
| 486 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
| 487 | * but this required special rights for the authenticated user |
||
| 488 | * (this is useful for content staging where the transfer process does not |
||
| 489 | * have to authenticate with the user which created the content object in the source server). |
||
| 490 | * The user has to publish the draft if it should be visible. |
||
| 491 | * In 4.x at least one location has to be provided in the location creation array. |
||
| 492 | * |
||
| 493 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
| 494 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
| 495 | * struct are missing or invalid, or if multiple locations are under the |
||
| 496 | * same parent. |
||
| 497 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 498 | * or if a required field is missing / set to an empty value. |
||
| 499 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 500 | * or value is set for non-translatable field in language |
||
| 501 | * other than main. |
||
| 502 | * |
||
| 503 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 504 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
| 505 | * |
||
| 506 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 507 | */ |
||
| 508 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = array()) |
||
| 509 | { |
||
| 510 | if ($contentCreateStruct->mainLanguageCode === null) { |
||
| 511 | throw new InvalidArgumentException('$contentCreateStruct', "'mainLanguageCode' property must be set"); |
||
| 512 | } |
||
| 513 | |||
| 514 | if ($contentCreateStruct->contentType === null) { |
||
| 515 | throw new InvalidArgumentException('$contentCreateStruct', "'contentType' property must be set"); |
||
| 516 | } |
||
| 517 | |||
| 518 | $contentCreateStruct = clone $contentCreateStruct; |
||
| 519 | |||
| 520 | if ($contentCreateStruct->ownerId === null) { |
||
| 521 | $contentCreateStruct->ownerId = $this->repository->getCurrentUserReference()->getUserId(); |
||
| 522 | } |
||
| 523 | |||
| 524 | if ($contentCreateStruct->alwaysAvailable === null) { |
||
| 525 | $contentCreateStruct->alwaysAvailable = $contentCreateStruct->contentType->defaultAlwaysAvailable ?: false; |
||
| 526 | } |
||
| 527 | |||
| 528 | $contentCreateStruct->contentType = $this->repository->getContentTypeService()->loadContentType( |
||
| 529 | $contentCreateStruct->contentType->id |
||
| 530 | ); |
||
| 531 | |||
| 532 | if (empty($contentCreateStruct->sectionId)) { |
||
| 533 | if (isset($locationCreateStructs[0])) { |
||
| 534 | $location = $this->repository->getLocationService()->loadLocation( |
||
| 535 | $locationCreateStructs[0]->parentLocationId |
||
| 536 | ); |
||
| 537 | $contentCreateStruct->sectionId = $location->contentInfo->sectionId; |
||
| 538 | } else { |
||
| 539 | $contentCreateStruct->sectionId = 1; |
||
| 540 | } |
||
| 541 | } |
||
| 542 | |||
| 543 | if (!$this->repository->canUser('content', 'create', $contentCreateStruct, $locationCreateStructs)) { |
||
| 544 | throw new UnauthorizedException( |
||
| 545 | 'content', |
||
| 546 | 'create', |
||
| 547 | array( |
||
| 548 | 'parentLocationId' => isset($locationCreateStructs[0]) ? |
||
| 549 | $locationCreateStructs[0]->parentLocationId : |
||
| 550 | null, |
||
| 551 | 'sectionId' => $contentCreateStruct->sectionId, |
||
| 552 | ) |
||
| 553 | ); |
||
| 554 | } |
||
| 555 | |||
| 556 | if (!empty($contentCreateStruct->remoteId)) { |
||
| 557 | try { |
||
| 558 | $this->loadContentByRemoteId($contentCreateStruct->remoteId); |
||
| 559 | |||
| 560 | throw new InvalidArgumentException( |
||
| 561 | '$contentCreateStruct', |
||
| 562 | "Another content with remoteId '{$contentCreateStruct->remoteId}' exists" |
||
| 563 | ); |
||
| 564 | } catch (APINotFoundException $e) { |
||
| 565 | // Do nothing |
||
| 566 | } |
||
| 567 | } else { |
||
| 568 | $contentCreateStruct->remoteId = $this->domainMapper->getUniqueHash($contentCreateStruct); |
||
| 569 | } |
||
| 570 | |||
| 571 | $spiLocationCreateStructs = $this->buildSPILocationCreateStructs($locationCreateStructs); |
||
| 572 | |||
| 573 | $languageCodes = $this->getLanguageCodesForCreate($contentCreateStruct); |
||
| 574 | $fields = $this->mapFieldsForCreate($contentCreateStruct); |
||
| 575 | |||
| 576 | $fieldValues = array(); |
||
| 577 | $spiFields = array(); |
||
| 578 | $allFieldErrors = array(); |
||
| 579 | $inputRelations = array(); |
||
| 580 | $locationIdToContentIdMapping = array(); |
||
| 581 | |||
| 582 | foreach ($contentCreateStruct->contentType->getFieldDefinitions() as $fieldDefinition) { |
||
| 583 | /** @var $fieldType \eZ\Publish\Core\FieldType\FieldType */ |
||
| 584 | $fieldType = $this->fieldTypeRegistry->getFieldType( |
||
| 585 | $fieldDefinition->fieldTypeIdentifier |
||
| 586 | ); |
||
| 587 | |||
| 588 | foreach ($languageCodes as $languageCode) { |
||
| 589 | $isEmptyValue = false; |
||
| 590 | $valueLanguageCode = $fieldDefinition->isTranslatable ? $languageCode : $contentCreateStruct->mainLanguageCode; |
||
| 591 | $isLanguageMain = $languageCode === $contentCreateStruct->mainLanguageCode; |
||
| 592 | if (isset($fields[$fieldDefinition->identifier][$valueLanguageCode])) { |
||
| 593 | $fieldValue = $fields[$fieldDefinition->identifier][$valueLanguageCode]->value; |
||
| 594 | } else { |
||
| 595 | $fieldValue = $fieldDefinition->defaultValue; |
||
| 596 | } |
||
| 597 | |||
| 598 | $fieldValue = $fieldType->acceptValue($fieldValue); |
||
| 599 | |||
| 600 | View Code Duplication | if ($fieldType->isEmptyValue($fieldValue)) { |
|
| 601 | $isEmptyValue = true; |
||
| 602 | if ($fieldDefinition->isRequired) { |
||
| 603 | $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( |
||
| 604 | "Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
||
| 605 | null, |
||
| 606 | ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], |
||
| 607 | 'empty' |
||
| 608 | ); |
||
| 609 | } |
||
| 610 | } else { |
||
| 611 | $fieldErrors = $fieldType->validate( |
||
| 612 | $fieldDefinition, |
||
| 613 | $fieldValue |
||
| 614 | ); |
||
| 615 | if (!empty($fieldErrors)) { |
||
| 616 | $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; |
||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | if (!empty($allFieldErrors)) { |
||
| 621 | continue; |
||
| 622 | } |
||
| 623 | |||
| 624 | $this->relationProcessor->appendFieldRelations( |
||
| 625 | $inputRelations, |
||
| 626 | $locationIdToContentIdMapping, |
||
| 627 | $fieldType, |
||
| 628 | $fieldValue, |
||
| 629 | $fieldDefinition->id |
||
| 630 | ); |
||
| 631 | $fieldValues[$fieldDefinition->identifier][$languageCode] = $fieldValue; |
||
| 632 | |||
| 633 | // Only non-empty value for: translatable field or in main language |
||
| 634 | if ( |
||
| 635 | (!$isEmptyValue && $fieldDefinition->isTranslatable) || |
||
| 636 | (!$isEmptyValue && $isLanguageMain) |
||
| 637 | ) { |
||
| 638 | $spiFields[] = new SPIField( |
||
| 639 | array( |
||
| 640 | 'id' => null, |
||
| 641 | 'fieldDefinitionId' => $fieldDefinition->id, |
||
| 642 | 'type' => $fieldDefinition->fieldTypeIdentifier, |
||
| 643 | 'value' => $fieldType->toPersistenceValue($fieldValue), |
||
| 644 | 'languageCode' => $languageCode, |
||
| 645 | 'versionNo' => null, |
||
| 646 | ) |
||
| 647 | ); |
||
| 648 | } |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | if (!empty($allFieldErrors)) { |
||
| 653 | throw new ContentFieldValidationException($allFieldErrors); |
||
| 654 | } |
||
| 655 | |||
| 656 | $spiContentCreateStruct = new SPIContentCreateStruct( |
||
| 657 | array( |
||
| 658 | 'name' => $this->nameSchemaService->resolve( |
||
| 659 | $contentCreateStruct->contentType->nameSchema, |
||
| 660 | $contentCreateStruct->contentType, |
||
| 661 | $fieldValues, |
||
| 662 | $languageCodes |
||
| 663 | ), |
||
| 664 | 'typeId' => $contentCreateStruct->contentType->id, |
||
| 665 | 'sectionId' => $contentCreateStruct->sectionId, |
||
| 666 | 'ownerId' => $contentCreateStruct->ownerId, |
||
| 667 | 'locations' => $spiLocationCreateStructs, |
||
| 668 | 'fields' => $spiFields, |
||
| 669 | 'alwaysAvailable' => $contentCreateStruct->alwaysAvailable, |
||
| 670 | 'remoteId' => $contentCreateStruct->remoteId, |
||
| 671 | 'modified' => isset($contentCreateStruct->modificationDate) ? $contentCreateStruct->modificationDate->getTimestamp() : time(), |
||
| 672 | 'initialLanguageId' => $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( |
||
| 673 | $contentCreateStruct->mainLanguageCode |
||
| 674 | )->id, |
||
| 675 | ) |
||
| 676 | ); |
||
| 677 | |||
| 678 | $defaultObjectStates = $this->getDefaultObjectStates(); |
||
| 679 | |||
| 680 | $this->repository->beginTransaction(); |
||
| 681 | try { |
||
| 682 | $spiContent = $this->persistenceHandler->contentHandler()->create($spiContentCreateStruct); |
||
| 683 | $this->relationProcessor->processFieldRelations( |
||
| 684 | $inputRelations, |
||
| 685 | $spiContent->versionInfo->contentInfo->id, |
||
| 686 | $spiContent->versionInfo->versionNo, |
||
| 687 | $contentCreateStruct->contentType |
||
| 688 | ); |
||
| 689 | |||
| 690 | $objectStateHandler = $this->persistenceHandler->objectStateHandler(); |
||
| 691 | foreach ($defaultObjectStates as $objectStateGroupId => $objectState) { |
||
| 692 | $objectStateHandler->setContentState( |
||
| 693 | $spiContent->versionInfo->contentInfo->id, |
||
| 694 | $objectStateGroupId, |
||
| 695 | $objectState->id |
||
| 696 | ); |
||
| 697 | } |
||
| 698 | |||
| 699 | $this->repository->commit(); |
||
| 700 | } catch (Exception $e) { |
||
| 701 | $this->repository->rollback(); |
||
| 702 | throw $e; |
||
| 703 | } |
||
| 704 | |||
| 705 | return $this->domainMapper->buildContentDomainObject($spiContent); |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Returns an array of default content states with content state group id as key. |
||
| 710 | * |
||
| 711 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 712 | */ |
||
| 713 | protected function getDefaultObjectStates() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Returns all language codes used in given $fields. |
||
| 731 | * |
||
| 732 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
| 733 | * |
||
| 734 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 735 | * |
||
| 736 | * @return string[] |
||
| 737 | */ |
||
| 738 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 765 | * |
||
| 766 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 767 | * or value is set for non-translatable field in language |
||
| 768 | * other than main |
||
| 769 | * |
||
| 770 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 771 | * |
||
| 772 | * @return array |
||
| 773 | */ |
||
| 774 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Clones $field with overriding specific properties from given $overrides array. |
||
| 810 | * |
||
| 811 | * @param Field $field |
||
| 812 | * @param array $overrides |
||
| 813 | * |
||
| 814 | * @return Field |
||
| 815 | */ |
||
| 816 | private function cloneField(Field $field, array $overrides = []) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 834 | * |
||
| 835 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
| 836 | * |
||
| 837 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
| 838 | */ |
||
| 839 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Updates the metadata. |
||
| 884 | * |
||
| 885 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
| 886 | * |
||
| 887 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
| 888 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
| 889 | * |
||
| 890 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 891 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
| 892 | * |
||
| 893 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
| 894 | */ |
||
| 895 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Publishes URL aliases for all locations of a given content. |
||
| 984 | * |
||
| 985 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 986 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
| 987 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
| 988 | */ |
||
| 989 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Deletes a content object including all its versions and locations including their subtrees. |
||
| 1018 | * |
||
| 1019 | * @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) |
||
| 1020 | * |
||
| 1021 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1022 | * |
||
| 1023 | * @return mixed[] Affected Location Id's |
||
| 1024 | */ |
||
| 1025 | public function deleteContent(ContentInfo $contentInfo) |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Creates a draft from a published or archived version. |
||
| 1055 | * |
||
| 1056 | * If no version is given, the current published version is used. |
||
| 1057 | * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
| 1058 | * It can be changed on updating the version. |
||
| 1059 | * |
||
| 1060 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
| 1061 | * |
||
| 1062 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1063 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1064 | * @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 |
||
| 1065 | * |
||
| 1066 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 1067 | */ |
||
| 1068 | public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null) |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Loads drafts for a user. |
||
| 1133 | * |
||
| 1134 | * If no user is given the drafts for the authenticated user a returned |
||
| 1135 | * |
||
| 1136 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to load the draft list |
||
| 1137 | * |
||
| 1138 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
| 1139 | * |
||
| 1140 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo the drafts ({@link VersionInfo}) owned by the given user |
||
| 1141 | */ |
||
| 1142 | public function loadContentDrafts(User $user = null) |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Updates the fields of a draft. |
||
| 1170 | * |
||
| 1171 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
| 1172 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1173 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
| 1174 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 1175 | * or if a required field is missing / set to an empty value. |
||
| 1176 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 1177 | * or value is set for non-translatable field in language |
||
| 1178 | * other than main. |
||
| 1179 | * |
||
| 1180 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1181 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1182 | * |
||
| 1183 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
| 1184 | */ |
||
| 1185 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Returns only updated language codes. |
||
| 1363 | * |
||
| 1364 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1365 | * |
||
| 1366 | * @return array |
||
| 1367 | */ |
||
| 1368 | View Code Duplication | private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) |
|
| 1384 | |||
| 1385 | /** |
||
| 1386 | * Returns all language codes used in given $fields. |
||
| 1387 | * |
||
| 1388 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
| 1389 | * |
||
| 1390 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1391 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 1392 | * |
||
| 1393 | * @return array |
||
| 1394 | */ |
||
| 1395 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 1410 | * |
||
| 1411 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 1412 | * or value is set for non-translatable field in language |
||
| 1413 | * other than main |
||
| 1414 | * |
||
| 1415 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1416 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 1417 | * @param string $mainLanguageCode |
||
| 1418 | * |
||
| 1419 | * @return array |
||
| 1420 | */ |
||
| 1421 | protected function mapFieldsForUpdate( |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Publishes a content version. |
||
| 1462 | * |
||
| 1463 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1464 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1465 | * |
||
| 1466 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version |
||
| 1467 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1468 | * |
||
| 1469 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1470 | * |
||
| 1471 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1472 | */ |
||
| 1473 | public function publishVersion(APIVersionInfo $versionInfo) |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Publishes a content version. |
||
| 1499 | * |
||
| 1500 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1501 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1502 | * |
||
| 1503 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1504 | * |
||
| 1505 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1506 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
| 1507 | * |
||
| 1508 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1509 | */ |
||
| 1510 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
| 1555 | |||
| 1556 | /** |
||
| 1557 | * @return int |
||
| 1558 | */ |
||
| 1559 | protected function getUnixTimestamp() |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Removes the given version. |
||
| 1566 | * |
||
| 1567 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
| 1568 | * published state or is a last version of Content in non draft state |
||
| 1569 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
| 1570 | * |
||
| 1571 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1572 | */ |
||
| 1573 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
| 1574 | { |
||
| 1575 | if ($versionInfo->isPublished()) { |
||
| 1576 | throw new BadStateException( |
||
| 1577 | '$versionInfo', |
||
| 1578 | 'Version is published and can not be removed' |
||
| 1579 | ); |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | View Code Duplication | if (!$this->repository->canUser('content', 'versionremove', $versionInfo)) { |
|
| 1583 | throw new UnauthorizedException( |
||
| 1584 | 'content', |
||
| 1585 | 'versionremove', |
||
| 1586 | array('contentId' => $versionInfo->contentInfo->id, 'versionNo' => $versionInfo->versionNo) |
||
| 1587 | ); |
||
| 1588 | } |
||
| 1589 | |||
| 1590 | $versionList = $this->persistenceHandler->contentHandler()->listVersions( |
||
| 1591 | $versionInfo->contentInfo->id, |
||
| 1592 | null, |
||
| 1593 | 2 |
||
| 1594 | ); |
||
| 1595 | |||
| 1596 | if (count($versionList) === 1 && !$versionInfo->isDraft()) { |
||
| 1597 | throw new BadStateException( |
||
| 1598 | '$versionInfo', |
||
| 1599 | 'Version is the last version of the Content and can not be removed' |
||
| 1600 | ); |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | $this->repository->beginTransaction(); |
||
| 1604 | try { |
||
| 1605 | $this->persistenceHandler->contentHandler()->deleteVersion( |
||
| 1606 | $versionInfo->getContentInfo()->id, |
||
| 1607 | $versionInfo->versionNo |
||
| 1608 | ); |
||
| 1609 | $this->repository->commit(); |
||
| 1610 | } catch (Exception $e) { |
||
| 1611 | $this->repository->rollback(); |
||
| 1612 | throw $e; |
||
| 1613 | } |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * Loads all versions for the given content. |
||
| 1618 | * |
||
| 1619 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
| 1620 | * |
||
| 1621 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1622 | * |
||
| 1623 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
| 1624 | */ |
||
| 1625 | public function loadVersions(ContentInfo $contentInfo) |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Copies the content to a new location. If no version is given, |
||
| 1648 | * all versions are copied, otherwise only the given version. |
||
| 1649 | * |
||
| 1650 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
| 1651 | * |
||
| 1652 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1653 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
| 1654 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1655 | * |
||
| 1656 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1657 | */ |
||
| 1658 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Loads all outgoing relations for the given version. |
||
| 1713 | * |
||
| 1714 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1715 | * |
||
| 1716 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1717 | * |
||
| 1718 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1719 | */ |
||
| 1720 | public function loadRelations(APIVersionInfo $versionInfo) |
||
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Loads all incoming relations for a content object. |
||
| 1758 | * |
||
| 1759 | * The relations come only from published versions of the source content objects |
||
| 1760 | * |
||
| 1761 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1762 | * |
||
| 1763 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1764 | * |
||
| 1765 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1766 | */ |
||
| 1767 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Adds a relation of type common. |
||
| 1796 | * |
||
| 1797 | * The source of the relation is the content and version |
||
| 1798 | * referenced by $versionInfo. |
||
| 1799 | * |
||
| 1800 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
| 1801 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1802 | * |
||
| 1803 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 1804 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
| 1805 | * |
||
| 1806 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
| 1807 | */ |
||
| 1808 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Removes a relation of type COMMON from a draft. |
||
| 1852 | * |
||
| 1853 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
| 1854 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1855 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
| 1856 | * |
||
| 1857 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 1858 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
| 1859 | */ |
||
| 1860 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * {@inheritdoc} |
||
| 1913 | */ |
||
| 1914 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 1922 | |||
| 1923 | /** |
||
| 1924 | * Delete Content item Translation from all Versions (including archived ones) of a Content Object. |
||
| 1925 | * |
||
| 1926 | * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it. |
||
| 1927 | * |
||
| 1928 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 1929 | * is the Main Translation of a Content Item. |
||
| 1930 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 1931 | * to delete the content (in one of the locations of the given Content Item). |
||
| 1932 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 1933 | * is invalid for the given content. |
||
| 1934 | * |
||
| 1935 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1936 | * @param string $languageCode |
||
| 1937 | * |
||
| 1938 | * @since 6.13 |
||
| 1939 | */ |
||
| 1940 | public function deleteTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 2017 | |||
| 2018 | /** |
||
| 2019 | * Delete specified Translation from a Content Draft. |
||
| 2020 | * |
||
| 2021 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 2022 | * is the only one the Content Draft has or it is the main Translation of a Content Object. |
||
| 2023 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 2024 | * to edit the Content (in one of the locations of the given Content Object). |
||
| 2025 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 2026 | * is invalid for the given Draft. |
||
| 2027 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found |
||
| 2028 | * |
||
| 2029 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft |
||
| 2030 | * @param string $languageCode Language code of the Translation to be removed |
||
| 2031 | * |
||
| 2032 | * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation |
||
| 2033 | * |
||
| 2034 | * @since 6.12 |
||
| 2035 | */ |
||
| 2036 | public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Instantiates a new content create struct object. |
||
| 2100 | * |
||
| 2101 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
| 2102 | * |
||
| 2103 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 2104 | * @param string $mainLanguageCode |
||
| 2105 | * |
||
| 2106 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
| 2107 | */ |
||
| 2108 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
| 2118 | |||
| 2119 | /** |
||
| 2120 | * Instantiates a new content meta data update struct. |
||
| 2121 | * |
||
| 2122 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
| 2123 | */ |
||
| 2124 | public function newContentMetadataUpdateStruct() |
||
| 2128 | |||
| 2129 | /** |
||
| 2130 | * Instantiates a new content update struct. |
||
| 2131 | * |
||
| 2132 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
| 2133 | */ |
||
| 2134 | public function newContentUpdateStruct() |
||
| 2138 | } |
||
| 2139 |
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.