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 |
||
| 54 | class ContentService implements ContentServiceInterface |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 58 | */ |
||
| 59 | protected $repository; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 63 | */ |
||
| 64 | protected $persistenceHandler; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $settings; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 73 | */ |
||
| 74 | protected $domainMapper; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
| 78 | */ |
||
| 79 | protected $relationProcessor; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 83 | */ |
||
| 84 | protected $nameSchemaService; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
| 88 | */ |
||
| 89 | protected $fieldTypeRegistry; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 93 | * |
||
| 94 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 95 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 96 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 97 | * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor |
||
| 98 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 99 | * @param \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry $fieldTypeRegistry, |
||
|
|
|||
| 100 | * @param array $settings |
||
| 101 | */ |
||
| 102 | public function __construct( |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Loads a content info object. |
||
| 126 | * |
||
| 127 | * To load fields use loadContent |
||
| 128 | * |
||
| 129 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 130 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 131 | * |
||
| 132 | * @param int $contentId |
||
| 133 | * |
||
| 134 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function loadContentInfo($contentId) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Loads a content info object. |
||
| 148 | * |
||
| 149 | * To load fields use loadContent |
||
| 150 | * |
||
| 151 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given id does not exist |
||
| 152 | * |
||
| 153 | * @param mixed $id |
||
| 154 | * @param bool $isRemoteId |
||
| 155 | * |
||
| 156 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 157 | */ |
||
| 158 | public function internalLoadContentInfo($id, $isRemoteId = false) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Loads a content info object for the given remoteId. |
||
| 177 | * |
||
| 178 | * To load fields use loadContent |
||
| 179 | * |
||
| 180 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the content |
||
| 181 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content with the given remote id does not exist |
||
| 182 | * |
||
| 183 | * @param string $remoteId |
||
| 184 | * |
||
| 185 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Loads a version info of the given content object. |
||
| 200 | * |
||
| 201 | * If no version number is given, the method returns the current version |
||
| 202 | * |
||
| 203 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 204 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 205 | * |
||
| 206 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 207 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 208 | * |
||
| 209 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 210 | */ |
||
| 211 | public function loadVersionInfo(ContentInfo $contentInfo, $versionNo = null) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Loads a version info of the given content object id. |
||
| 218 | * |
||
| 219 | * If no version number is given, the method returns the current version |
||
| 220 | * |
||
| 221 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the version with the given number does not exist |
||
| 222 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to load this version |
||
| 223 | * |
||
| 224 | * @param mixed $contentId |
||
| 225 | * @param int $versionNo the version number. If not given the current version is returned. |
||
| 226 | * |
||
| 227 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo |
||
| 228 | */ |
||
| 229 | public function loadVersionInfoById($contentId, $versionNo = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function loadContentByContentInfo(ContentInfo $contentInfo, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function loadContentByVersionInfo(APIVersionInfo $versionInfo, array $languages = null, $useAlwaysAvailable = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | View Code Duplication | public function loadContent($contentId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Loads content in a version of the given content object. |
||
| 320 | * |
||
| 321 | * If no version number is given, the method returns the current version |
||
| 322 | * |
||
| 323 | * @internal |
||
| 324 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content or version with the given id and languages does not exist |
||
| 325 | * |
||
| 326 | * @param mixed $id |
||
| 327 | * @param array|null $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 328 | * returned value object. If not given all languages are returned. |
||
| 329 | * @param int|null $versionNo the version number. If not given the current version is returned |
||
| 330 | * @param bool $isRemoteId |
||
| 331 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 332 | * |
||
| 333 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 334 | */ |
||
| 335 | public function internalLoadContent($id, array $languages = null, $versionNo = null, $isRemoteId = false, $useAlwaysAvailable = true) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Loads content in a version for the content object reference by the given remote id. |
||
| 388 | * |
||
| 389 | * If no version is given, the method returns the current version |
||
| 390 | * |
||
| 391 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the content or version with the given remote id does not exist |
||
| 392 | * @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 |
||
| 393 | * |
||
| 394 | * @param string $remoteId |
||
| 395 | * @param array $languages A language filter for fields. If not given all languages are returned |
||
| 396 | * @param int $versionNo the version number. If not given the current version is returned |
||
| 397 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true |
||
| 398 | * |
||
| 399 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function loadContentByRemoteId($remoteId, array $languages = null, $versionNo = null, $useAlwaysAvailable = true) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Bulk-load Content items by the list of ContentInfo Value Objects. |
||
| 421 | * |
||
| 422 | * Note: it does not throw exceptions on load, just ignores erroneous Content item. |
||
| 423 | * Moreover, since the method works on pre-loaded ContentInfo list, it is assumed that user is |
||
| 424 | * allowed to access every Content on the list. |
||
| 425 | * |
||
| 426 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo[] $contentInfoList |
||
| 427 | * @param string[] $languages A language priority, filters returned fields and is used as prioritized language code on |
||
| 428 | * returned value object. If not given all languages are returned. |
||
| 429 | * @param bool $useAlwaysAvailable Add Main language to \$languages if true (default) and if alwaysAvailable is true, |
||
| 430 | * unless all languages have been asked for. |
||
| 431 | * |
||
| 432 | * @return \eZ\Publish\API\Repository\Values\Content\Content[] list of Content items with Content Ids as keys |
||
| 433 | */ |
||
| 434 | public function loadContentListByContentInfo( |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Creates a new content draft assigned to the authenticated user. |
||
| 472 | * |
||
| 473 | * If a different userId is given in $contentCreateStruct it is assigned to the given user |
||
| 474 | * but this required special rights for the authenticated user |
||
| 475 | * (this is useful for content staging where the transfer process does not |
||
| 476 | * have to authenticate with the user which created the content object in the source server). |
||
| 477 | * The user has to publish the draft if it should be visible. |
||
| 478 | * In 4.x at least one location has to be provided in the location creation array. |
||
| 479 | * |
||
| 480 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create the content in the given location |
||
| 481 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the provided remoteId exists in the system, required properties on |
||
| 482 | * struct are missing or invalid, or if multiple locations are under the |
||
| 483 | * same parent. |
||
| 484 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 485 | * or if a required field is missing / set to an empty value. |
||
| 486 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 487 | * or value is set for non-translatable field in language |
||
| 488 | * other than main. |
||
| 489 | * |
||
| 490 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 491 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs For each location parent under which a location should be created for the content |
||
| 492 | * |
||
| 493 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 494 | */ |
||
| 495 | public function createContent(APIContentCreateStruct $contentCreateStruct, array $locationCreateStructs = []) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns an array of default content states with content state group id as key. |
||
| 696 | * |
||
| 697 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 698 | */ |
||
| 699 | protected function getDefaultObjectStates() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Returns all language codes used in given $fields. |
||
| 717 | * |
||
| 718 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value is set in main language |
||
| 719 | * |
||
| 720 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 721 | * |
||
| 722 | * @return string[] |
||
| 723 | */ |
||
| 724 | protected function getLanguageCodesForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 751 | * |
||
| 752 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 753 | * or value is set for non-translatable field in language |
||
| 754 | * other than main |
||
| 755 | * |
||
| 756 | * @param \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct $contentCreateStruct |
||
| 757 | * |
||
| 758 | * @return array |
||
| 759 | */ |
||
| 760 | protected function mapFieldsForCreate(APIContentCreateStruct $contentCreateStruct) |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Clones $field with overriding specific properties from given $overrides array. |
||
| 796 | * |
||
| 797 | * @param Field $field |
||
| 798 | * @param array $overrides |
||
| 799 | * |
||
| 800 | * @return Field |
||
| 801 | */ |
||
| 802 | private function cloneField(Field $field, array $overrides = []) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 820 | * |
||
| 821 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct[] $locationCreateStructs |
||
| 822 | * |
||
| 823 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct[] |
||
| 824 | */ |
||
| 825 | protected function buildSPILocationCreateStructs(array $locationCreateStructs) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Updates the metadata. |
||
| 870 | * |
||
| 871 | * (see {@link ContentMetadataUpdateStruct}) of a content object - to update fields use updateContent |
||
| 872 | * |
||
| 873 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update the content meta data |
||
| 874 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the remoteId in $contentMetadataUpdateStruct is set but already exists |
||
| 875 | * |
||
| 876 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 877 | * @param \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct $contentMetadataUpdateStruct |
||
| 878 | * |
||
| 879 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content with the updated attributes |
||
| 880 | */ |
||
| 881 | public function updateContentMetadata(ContentInfo $contentInfo, ContentMetadataUpdateStruct $contentMetadataUpdateStruct) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Publishes URL aliases for all locations of a given content. |
||
| 970 | * |
||
| 971 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 972 | * @param bool $updatePathIdentificationString this parameter is legacy storage specific for updating |
||
| 973 | * ezcontentobject_tree.path_identification_string, it is ignored by other storage engines |
||
| 974 | */ |
||
| 975 | protected function publishUrlAliasesForContent(APIContent $content, $updatePathIdentificationString = true) |
||
| 976 | { |
||
| 977 | $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
||
| 978 | $locations = $this->repository->getLocationService()->loadLocations( |
||
| 979 | $content->getVersionInfo()->getContentInfo() |
||
| 980 | ); |
||
| 981 | foreach ($locations as $location) { |
||
| 982 | View Code Duplication | foreach ($urlAliasNames as $languageCode => $name) { |
|
| 983 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 984 | $location->id, |
||
| 985 | $location->parentLocationId, |
||
| 986 | $name, |
||
| 987 | $languageCode, |
||
| 988 | $content->contentInfo->alwaysAvailable, |
||
| 989 | $updatePathIdentificationString ? $languageCode === $content->contentInfo->mainLanguageCode : false |
||
| 990 | ); |
||
| 991 | } |
||
| 992 | // archive URL aliases of Translations that got deleted |
||
| 993 | $this->persistenceHandler->urlAliasHandler()->archiveUrlAliasesForDeletedTranslations( |
||
| 994 | $location->id, |
||
| 995 | $location->parentLocationId, |
||
| 996 | $content->versionInfo->languageCodes |
||
| 997 | ); |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Deletes a content object including all its versions and locations including their subtrees. |
||
| 1003 | * |
||
| 1004 | * @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) |
||
| 1005 | * |
||
| 1006 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1007 | * |
||
| 1008 | * @return mixed[] Affected Location Id's |
||
| 1009 | */ |
||
| 1010 | public function deleteContent(ContentInfo $contentInfo) |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Creates a draft from a published or archived version. |
||
| 1039 | * |
||
| 1040 | * If no version is given, the current published version is used. |
||
| 1041 | * 4.x: The draft is created with the initialLanguage code of the source version or if not present with the main language. |
||
| 1042 | * It can be changed on updating the version. |
||
| 1043 | * |
||
| 1044 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to create the draft |
||
| 1045 | * |
||
| 1046 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1047 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1048 | * @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 |
||
| 1049 | * |
||
| 1050 | * @return \eZ\Publish\API\Repository\Values\Content\Content - the newly created content draft |
||
| 1051 | */ |
||
| 1052 | public function createContentDraft(ContentInfo $contentInfo, APIVersionInfo $versionInfo = null, User $creator = null) |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Loads drafts for a user. |
||
| 1117 | * |
||
| 1118 | * If no user is given the drafts for the authenticated user a returned |
||
| 1119 | * |
||
| 1120 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to load the draft list |
||
| 1121 | * |
||
| 1122 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
| 1123 | * |
||
| 1124 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo the drafts ({@link VersionInfo}) owned by the given user |
||
| 1125 | */ |
||
| 1126 | public function loadContentDrafts(User $user = null) |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Translate a version. |
||
| 1154 | * |
||
| 1155 | * updates the destination version given in $translationInfo with the provided translated fields in $translationValues |
||
| 1156 | * |
||
| 1157 | * @example Examples/translation_5x.php |
||
| 1158 | * |
||
| 1159 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the current-user is not allowed to update this version |
||
| 1160 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the given destination version is not a draft |
||
| 1161 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $translationValues is not valid, or if a required field is missing or is set to an empty value. |
||
| 1162 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 1163 | * or value is set for non-translatable field in language |
||
| 1164 | * other than main. |
||
| 1165 | * |
||
| 1166 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo |
||
| 1167 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationValues $translationValues |
||
| 1168 | * @param \eZ\Publish\API\Repository\Values\User\User $modifier If set, this user is taken as modifier of the version |
||
| 1169 | * |
||
| 1170 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the translated fields |
||
| 1171 | * |
||
| 1172 | * @since 5.0 |
||
| 1173 | */ |
||
| 1174 | public function translateVersion(TranslationInfo $translationInfo, APITranslationValues $translationValues, User $modifier = null) |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Updates the fields of a draft. |
||
| 1181 | * |
||
| 1182 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update this version |
||
| 1183 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1184 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a property on the struct is invalid. |
||
| 1185 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $contentCreateStruct is not valid, |
||
| 1186 | * or if a required field is missing / set to an empty value. |
||
| 1187 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType, |
||
| 1188 | * or value is set for non-translatable field in language |
||
| 1189 | * other than main. |
||
| 1190 | * |
||
| 1191 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1192 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1193 | * |
||
| 1194 | * @return \eZ\Publish\API\Repository\Values\Content\Content the content draft with the updated fields |
||
| 1195 | */ |
||
| 1196 | public function updateContent(APIVersionInfo $versionInfo, APIContentUpdateStruct $contentUpdateStruct) |
||
| 1370 | |||
| 1371 | /** |
||
| 1372 | * Returns only updated language codes. |
||
| 1373 | * |
||
| 1374 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1375 | * |
||
| 1376 | * @return array |
||
| 1377 | */ |
||
| 1378 | View Code Duplication | private function getUpdatedLanguageCodes(APIContentUpdateStruct $contentUpdateStruct) |
|
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Returns all language codes used in given $fields. |
||
| 1397 | * |
||
| 1398 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if no field value exists in initial language |
||
| 1399 | * |
||
| 1400 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1401 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 1402 | * |
||
| 1403 | * @return array |
||
| 1404 | */ |
||
| 1405 | protected function getLanguageCodesForUpdate(APIContentUpdateStruct $contentUpdateStruct, APIContent $content) |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Returns an array of fields like $fields[$field->fieldDefIdentifier][$field->languageCode]. |
||
| 1420 | * |
||
| 1421 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException If field definition does not exist in the ContentType |
||
| 1422 | * or value is set for non-translatable field in language |
||
| 1423 | * other than main |
||
| 1424 | * |
||
| 1425 | * @param \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct $contentUpdateStruct |
||
| 1426 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 1427 | * @param string $mainLanguageCode |
||
| 1428 | * |
||
| 1429 | * @return array |
||
| 1430 | */ |
||
| 1431 | protected function mapFieldsForUpdate( |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * Publishes a content version. |
||
| 1472 | * |
||
| 1473 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1474 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1475 | * |
||
| 1476 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to publish this version |
||
| 1477 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1478 | * |
||
| 1479 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1480 | * |
||
| 1481 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1482 | */ |
||
| 1483 | public function publishVersion(APIVersionInfo $versionInfo) |
||
| 1506 | |||
| 1507 | /** |
||
| 1508 | * Publishes a content version. |
||
| 1509 | * |
||
| 1510 | * Publishes a content version and deletes archive versions if they overflow max archive versions. |
||
| 1511 | * Max archive versions are currently a configuration, but might be moved to be a param of ContentType in the future. |
||
| 1512 | * |
||
| 1513 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1514 | * |
||
| 1515 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1516 | * @param int|null $publicationDate If null existing date is kept if there is one, otherwise current time is used. |
||
| 1517 | * |
||
| 1518 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1519 | */ |
||
| 1520 | protected function internalPublishVersion(APIVersionInfo $versionInfo, $publicationDate = null) |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Removes the given version. |
||
| 1568 | * |
||
| 1569 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is in |
||
| 1570 | * published state or is a last version of Content in non draft state |
||
| 1571 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove this version |
||
| 1572 | * |
||
| 1573 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1574 | */ |
||
| 1575 | public function deleteVersion(APIVersionInfo $versionInfo) |
||
| 1617 | |||
| 1618 | /** |
||
| 1619 | * Loads all versions for the given content. |
||
| 1620 | * |
||
| 1621 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to list versions |
||
| 1622 | * |
||
| 1623 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1624 | * |
||
| 1625 | * @return \eZ\Publish\API\Repository\Values\Content\VersionInfo[] Sorted by creation date |
||
| 1626 | */ |
||
| 1627 | public function loadVersions(ContentInfo $contentInfo) |
||
| 1647 | |||
| 1648 | /** |
||
| 1649 | * Copies the content to a new location. If no version is given, |
||
| 1650 | * all versions are copied, otherwise only the given version. |
||
| 1651 | * |
||
| 1652 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to copy the content to the given location |
||
| 1653 | * |
||
| 1654 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1655 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $destinationLocationCreateStruct the target location where the content is copied to |
||
| 1656 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1657 | * |
||
| 1658 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 1659 | */ |
||
| 1660 | public function copyContent(ContentInfo $contentInfo, LocationCreateStruct $destinationLocationCreateStruct, APIVersionInfo $versionInfo = null) |
||
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Loads all outgoing relations for the given version. |
||
| 1714 | * |
||
| 1715 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1716 | * |
||
| 1717 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo |
||
| 1718 | * |
||
| 1719 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1720 | */ |
||
| 1721 | public function loadRelations(APIVersionInfo $versionInfo) |
||
| 1756 | |||
| 1757 | /** |
||
| 1758 | * Loads all incoming relations for a content object. |
||
| 1759 | * |
||
| 1760 | * The relations come only from published versions of the source content objects |
||
| 1761 | * |
||
| 1762 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read this version |
||
| 1763 | * |
||
| 1764 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1765 | * |
||
| 1766 | * @return \eZ\Publish\API\Repository\Values\Content\Relation[] |
||
| 1767 | */ |
||
| 1768 | public function loadReverseRelations(ContentInfo $contentInfo) |
||
| 1794 | |||
| 1795 | /** |
||
| 1796 | * Adds a relation of type common. |
||
| 1797 | * |
||
| 1798 | * The source of the relation is the content and version |
||
| 1799 | * referenced by $versionInfo. |
||
| 1800 | * |
||
| 1801 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit this version |
||
| 1802 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1803 | * |
||
| 1804 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 1805 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent the destination of the relation |
||
| 1806 | * |
||
| 1807 | * @return \eZ\Publish\API\Repository\Values\Content\Relation the newly created relation |
||
| 1808 | */ |
||
| 1809 | public function addRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 1850 | |||
| 1851 | /** |
||
| 1852 | * Removes a relation of type COMMON from a draft. |
||
| 1853 | * |
||
| 1854 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed edit this version |
||
| 1855 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the version is not a draft |
||
| 1856 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is no relation of type COMMON for the given destination |
||
| 1857 | * |
||
| 1858 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $sourceVersion |
||
| 1859 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContent |
||
| 1860 | */ |
||
| 1861 | public function deleteRelation(APIVersionInfo $sourceVersion, ContentInfo $destinationContent) |
||
| 1911 | |||
| 1912 | /** |
||
| 1913 | * Adds translation information to the content object. |
||
| 1914 | * |
||
| 1915 | * @example Examples/translation_5x.php |
||
| 1916 | * |
||
| 1917 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed add a translation info |
||
| 1918 | * |
||
| 1919 | * @param \eZ\Publish\API\Repository\Values\Content\TranslationInfo $translationInfo |
||
| 1920 | * |
||
| 1921 | * @since 5.0 |
||
| 1922 | */ |
||
| 1923 | public function addTranslationInfo(TranslationInfo $translationInfo) |
||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * lists the translations done on this content object. |
||
| 1930 | * |
||
| 1931 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed read translation infos |
||
| 1932 | * |
||
| 1933 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1934 | * @param array $filter |
||
| 1935 | * |
||
| 1936 | * @todo TBD - filter by source version destination version and languages |
||
| 1937 | * |
||
| 1938 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo[] |
||
| 1939 | * |
||
| 1940 | * @since 5.0 |
||
| 1941 | */ |
||
| 1942 | public function loadTranslationInfos(ContentInfo $contentInfo, array $filter = []) |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * {@inheritdoc} |
||
| 1949 | */ |
||
| 1950 | public function removeTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Delete Content item Translation from all Versions (including archived ones) of a Content Object. |
||
| 1961 | * |
||
| 1962 | * NOTE: this operation is risky and permanent, so user interface should provide a warning before performing it. |
||
| 1963 | * |
||
| 1964 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 1965 | * is the Main Translation of a Content Item. |
||
| 1966 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 1967 | * to delete the content (in one of the locations of the given Content Item). |
||
| 1968 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 1969 | * is invalid for the given content. |
||
| 1970 | * |
||
| 1971 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 1972 | * @param string $languageCode |
||
| 1973 | * |
||
| 1974 | * @since 6.13 |
||
| 1975 | */ |
||
| 1976 | public function deleteTranslation(ContentInfo $contentInfo, $languageCode) |
||
| 1977 | { |
||
| 1978 | if ($contentInfo->mainLanguageCode === $languageCode) { |
||
| 1979 | throw new BadStateException( |
||
| 1980 | '$languageCode', |
||
| 1981 | 'Specified translation is the main translation of the Content Object' |
||
| 1982 | ); |
||
| 1983 | } |
||
| 1984 | |||
| 1985 | $translationWasFound = false; |
||
| 1986 | $this->repository->beginTransaction(); |
||
| 1987 | try { |
||
| 1988 | foreach ($this->loadVersions($contentInfo) as $versionInfo) { |
||
| 1989 | View Code Duplication | if (!$this->repository->canUser('content', 'remove', $versionInfo)) { |
|
| 1990 | throw new UnauthorizedException( |
||
| 1991 | 'content', |
||
| 1992 | 'remove', |
||
| 1993 | ['contentId' => $contentInfo->id, 'versionNo' => $versionInfo->versionNo] |
||
| 1994 | ); |
||
| 1995 | } |
||
| 1996 | |||
| 1997 | if (!in_array($languageCode, $versionInfo->languageCodes)) { |
||
| 1998 | continue; |
||
| 1999 | } |
||
| 2000 | |||
| 2001 | $translationWasFound = true; |
||
| 2002 | |||
| 2003 | // If the translation is the version's only one, delete the version |
||
| 2004 | if (count($versionInfo->languageCodes) < 2) { |
||
| 2005 | $this->persistenceHandler->contentHandler()->deleteVersion( |
||
| 2006 | $versionInfo->getContentInfo()->id, |
||
| 2007 | $versionInfo->versionNo |
||
| 2008 | ); |
||
| 2009 | } |
||
| 2010 | } |
||
| 2011 | |||
| 2012 | if (!$translationWasFound) { |
||
| 2013 | throw new InvalidArgumentException( |
||
| 2014 | '$languageCode', |
||
| 2015 | sprintf( |
||
| 2016 | '%s does not exist in the Content item(id=%d)', |
||
| 2017 | $languageCode, |
||
| 2018 | $contentInfo->id |
||
| 2019 | ) |
||
| 2020 | ); |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | $this->persistenceHandler->contentHandler()->deleteTranslationFromContent( |
||
| 2024 | $contentInfo->id, |
||
| 2025 | $languageCode |
||
| 2026 | ); |
||
| 2027 | $locationIds = array_map( |
||
| 2028 | function (Location $location) { |
||
| 2029 | return $location->id; |
||
| 2030 | }, |
||
| 2031 | $this->repository->getLocationService()->loadLocations($contentInfo) |
||
| 2032 | ); |
||
| 2033 | $this->persistenceHandler->urlAliasHandler()->translationRemoved( |
||
| 2034 | $locationIds, |
||
| 2035 | $languageCode |
||
| 2036 | ); |
||
| 2037 | $this->repository->commit(); |
||
| 2038 | } catch (InvalidArgumentException $e) { |
||
| 2039 | $this->repository->rollback(); |
||
| 2040 | throw $e; |
||
| 2041 | } catch (BadStateException $e) { |
||
| 2042 | $this->repository->rollback(); |
||
| 2043 | throw $e; |
||
| 2044 | } catch (UnauthorizedException $e) { |
||
| 2045 | $this->repository->rollback(); |
||
| 2046 | throw $e; |
||
| 2047 | } catch (Exception $e) { |
||
| 2048 | $this->repository->rollback(); |
||
| 2049 | // cover generic unexpected exception to fulfill API promise on @throws |
||
| 2050 | throw new BadStateException('$contentInfo', 'Translation removal failed', $e); |
||
| 2051 | } |
||
| 2052 | } |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Delete specified Translation from a Content Draft. |
||
| 2056 | * |
||
| 2057 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the specified Translation |
||
| 2058 | * is the only one the Content Draft has or it is the main Translation of a Content Object. |
||
| 2059 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed |
||
| 2060 | * to edit the Content (in one of the locations of the given Content Object). |
||
| 2061 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
| 2062 | * is invalid for the given Draft. |
||
| 2063 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if specified Version was not found |
||
| 2064 | * |
||
| 2065 | * @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo Content Version Draft |
||
| 2066 | * @param string $languageCode Language code of the Translation to be removed |
||
| 2067 | * |
||
| 2068 | * @return \eZ\Publish\API\Repository\Values\Content\Content Content Draft w/o the specified Translation |
||
| 2069 | * |
||
| 2070 | * @since 6.12 |
||
| 2071 | */ |
||
| 2072 | public function deleteTranslationFromDraft(APIVersionInfo $versionInfo, $languageCode) |
||
| 2133 | |||
| 2134 | /** |
||
| 2135 | * Instantiates a new content create struct object. |
||
| 2136 | * |
||
| 2137 | * alwaysAvailable is set to the ContentType's defaultAlwaysAvailable |
||
| 2138 | * |
||
| 2139 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType |
||
| 2140 | * @param string $mainLanguageCode |
||
| 2141 | * |
||
| 2142 | * @return \eZ\Publish\API\Repository\Values\Content\ContentCreateStruct |
||
| 2143 | */ |
||
| 2144 | public function newContentCreateStruct(ContentType $contentType, $mainLanguageCode) |
||
| 2154 | |||
| 2155 | /** |
||
| 2156 | * Instantiates a new content meta data update struct. |
||
| 2157 | * |
||
| 2158 | * @return \eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct |
||
| 2159 | */ |
||
| 2160 | public function newContentMetadataUpdateStruct() |
||
| 2164 | |||
| 2165 | /** |
||
| 2166 | * Instantiates a new content update struct. |
||
| 2167 | * |
||
| 2168 | * @return \eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct |
||
| 2169 | */ |
||
| 2170 | public function newContentUpdateStruct() |
||
| 2174 | |||
| 2175 | /** |
||
| 2176 | * Instantiates a new TranslationInfo object. |
||
| 2177 | * |
||
| 2178 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationInfo |
||
| 2179 | */ |
||
| 2180 | public function newTranslationInfo() |
||
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Instantiates a Translation object. |
||
| 2187 | * |
||
| 2188 | * @return \eZ\Publish\API\Repository\Values\Content\TranslationValues |
||
| 2189 | */ |
||
| 2190 | public function newTranslationValues() |
||
| 2194 | } |
||
| 2195 |
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.