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:
| 1 | <?php |
||
| 29 | class UserService implements APIUserService, Sessionable |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var \eZ\Publish\Core\REST\Client\HttpClient |
||
| 33 | */ |
||
| 34 | private $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher |
||
| 38 | */ |
||
| 39 | private $inputDispatcher; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
| 43 | */ |
||
| 44 | private $outputVisitor; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
| 48 | */ |
||
| 49 | private $requestParser; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client |
||
| 53 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher |
||
| 54 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor |
||
| 55 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function __construct(HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Set session ID. |
||
| 67 | * |
||
| 68 | * Only for testing |
||
| 69 | * |
||
| 70 | * @param mixed tringid |
||
| 71 | * |
||
| 72 | * @private |
||
| 73 | */ |
||
| 74 | public function setSession($id) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates a new user group using the data provided in the ContentCreateStruct parameter. |
||
| 83 | * |
||
| 84 | * In 4.x in the content type parameter in the profile is ignored |
||
| 85 | * - the content type is determined via configuration and can be set to null. |
||
| 86 | * The returned version is published. |
||
| 87 | * |
||
| 88 | * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $userGroupCreateStruct a structure for setting all necessary data to create this user group |
||
| 89 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $parentGroup |
||
| 90 | * |
||
| 91 | * @return \eZ\Publish\API\Repository\Values\User\UserGroup |
||
| 92 | * |
||
| 93 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group |
||
| 94 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the input structure has invalid data |
||
| 95 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupCreateStruct is not valid |
||
| 96 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing |
||
| 97 | */ |
||
| 98 | public function createUserGroup(UserGroupCreateStruct $userGroupCreateStruct, UserGroup $parentGroup) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | public function loadUserGroup($id, array $prioritizedLanguages = []) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | public function loadSubUserGroups(UserGroup $userGroup, $offset = 0, $limit = 25, array $prioritizedLanguages = []) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Removes a user group. |
||
| 121 | * |
||
| 122 | * the users which are not assigned to other groups will be deleted. |
||
| 123 | * |
||
| 124 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 125 | * |
||
| 126 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group |
||
| 127 | */ |
||
| 128 | public function deleteUserGroup(UserGroup $userGroup) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Moves the user group to another parent. |
||
| 135 | * |
||
| 136 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 137 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $newParent |
||
| 138 | * |
||
| 139 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 140 | */ |
||
| 141 | public function moveUserGroup(UserGroup $userGroup, UserGroup $newParent) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Updates the group profile with fields and meta data. |
||
| 148 | * |
||
| 149 | * 4.x: If the versionUpdateStruct is set in $userGroupUpdateStruct, this method internally creates a content draft, updates ts with the provided data |
||
| 150 | * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods. |
||
| 151 | * |
||
| 152 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 153 | * @param \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct $userGroupUpdateStruct |
||
| 154 | * |
||
| 155 | * @return \eZ\Publish\API\Repository\Values\User\UserGroup |
||
| 156 | * |
||
| 157 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 158 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupUpdateStruct is not valid |
||
| 159 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty |
||
| 160 | */ |
||
| 161 | public function updateUserGroup(UserGroup $userGroup, UserGroupUpdateStruct $userGroupUpdateStruct) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Create a new user. The created user is published by this method. |
||
| 168 | * |
||
| 169 | * @param \eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreateStruct the data used for creating the user |
||
| 170 | * @param array $parentGroups the groups of type {@link \eZ\Publish\API\Repository\Values\User\UserGroup} which are assigned to the user after creation |
||
| 171 | * |
||
| 172 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 173 | * |
||
| 174 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 175 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user group was not found |
||
| 176 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userCreateStruct is not valid |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing |
||
| 178 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a user with provided login already exists |
||
| 179 | */ |
||
| 180 | public function createUser(UserCreateStruct $userCreateStruct, array $parentGroups) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function loadUser($userId, array $prioritizedLanguages = []) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Loads anonymous user. |
||
| 195 | * |
||
| 196 | * @deprecated since 5.3, use loadUser( $anonymousUserId ) instead |
||
| 197 | * |
||
| 198 | * |
||
| 199 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 200 | */ |
||
| 201 | public function loadAnonymousUser() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function loadUserByCredentials($login, $password, array $prioritizedLanguages = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function loadUserByLogin($login, array $prioritizedLanguages = []) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function loadUsersByEmail($email, array $prioritizedLanguages = []) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Loads a user with user hash key. |
||
| 232 | * |
||
| 233 | * @param string $hash |
||
| 234 | * @param array $prioritizedLanguages |
||
| 235 | * |
||
| 236 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 237 | */ |
||
| 238 | public function loadUserByToken($hash, array $prioritizedLanguages = []) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * This method deletes a user. |
||
| 245 | * |
||
| 246 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 247 | * |
||
| 248 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete the user |
||
| 249 | */ |
||
| 250 | public function deleteUser(User $user) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Updates a user. |
||
| 257 | * |
||
| 258 | * 4.x: If the versionUpdateStruct is set in the user update structure, this method internally creates a content draft, updates ts with the provided data |
||
| 259 | * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods. |
||
| 260 | * |
||
| 261 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 262 | * @param \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct |
||
| 263 | * |
||
| 264 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update the user |
||
| 265 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userUpdateStruct is not valid |
||
| 266 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty |
||
| 267 | * |
||
| 268 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 269 | */ |
||
| 270 | public function updateUser(User $user, UserUpdateStruct $userUpdateStruct) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Update the user token information specified by the user token struct. |
||
| 277 | * |
||
| 278 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 279 | * @param \eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct $userTokenUpdateStruct |
||
| 280 | * |
||
| 281 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 282 | */ |
||
| 283 | public function updateUserToken(User $user, UserTokenUpdateStruct $userTokenUpdateStruct) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Expires user token with user hash. |
||
| 290 | * |
||
| 291 | * @param string $hash |
||
| 292 | */ |
||
| 293 | public function expireUserToken($hash) |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Assigns a new user group to the user. |
||
| 301 | * |
||
| 302 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 303 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 304 | * |
||
| 305 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign the user group to the user |
||
| 306 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is already in the given user group |
||
| 307 | */ |
||
| 308 | public function assignUserToUserGroup(User $user, UserGroup $userGroup) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Removes a user group from the user. |
||
| 315 | * |
||
| 316 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 317 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 318 | * |
||
| 319 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove the user group from the user |
||
| 320 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is not in the given user group |
||
| 321 | */ |
||
| 322 | public function unAssignUserFromUserGroup(User $user, UserGroup $userGroup) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function loadUserGroupsOfUser(User $user, $offset = 0, $limit = 25, array $prioritizedLanguages = []) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritdoc} |
||
| 337 | */ |
||
| 338 | public function loadUsersOfUserGroup( |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Instantiate a user create class. |
||
| 349 | * |
||
| 350 | * @param string $login the login of the new user |
||
| 351 | * @param string $email the email of the new user |
||
| 352 | * @param string $password the plain password of the new user |
||
| 353 | * @param string $mainLanguageCode the main language for the underlying content object |
||
| 354 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType 5.x the content type for the underlying content object. In 4.x it is ignored and taken from the configuration |
||
| 355 | * |
||
| 356 | * @return \eZ\Publish\API\Repository\Values\User\UserCreateStruct |
||
| 357 | */ |
||
| 358 | public function newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Instantiate a user group create class. |
||
| 365 | * |
||
| 366 | * @param string $mainLanguageCode The main language for the underlying content object |
||
| 367 | * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType 5.x the content type for the underlying content object. In 4.x it is ignored and taken from the configuration |
||
| 368 | * |
||
| 369 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct |
||
| 370 | */ |
||
| 371 | public function newUserGroupCreateStruct($mainLanguageCode, $contentType = null) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Instantiate a new user update struct. |
||
| 378 | * |
||
| 379 | * @return \eZ\Publish\API\Repository\Values\User\UserUpdateStruct |
||
| 380 | */ |
||
| 381 | public function newUserUpdateStruct() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Instantiate a new user group update struct. |
||
| 388 | * |
||
| 389 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct |
||
| 390 | */ |
||
| 391 | public function newUserGroupUpdateStruct() |
||
| 395 | } |
||
| 396 |