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 |
||
| 31 | class UserService implements APIUserService, Sessionable |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var \eZ\Publish\Core\REST\Client\HttpClient |
||
| 35 | */ |
||
| 36 | private $client; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher |
||
| 40 | */ |
||
| 41 | private $inputDispatcher; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
| 45 | */ |
||
| 46 | private $outputVisitor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
| 50 | */ |
||
| 51 | private $requestParser; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client |
||
| 55 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher |
||
| 56 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor |
||
| 57 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function __construct(HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Set session ID. |
||
| 69 | * |
||
| 70 | * Only for testing |
||
| 71 | * |
||
| 72 | * @param mixed tringid |
||
| 73 | * |
||
| 74 | * @private |
||
| 75 | */ |
||
| 76 | public function setSession($id) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Creates a new user group using the data provided in the ContentCreateStruct parameter. |
||
| 85 | * |
||
| 86 | * In 4.x in the content type parameter in the profile is ignored |
||
| 87 | * - the content type is determined via configuration and can be set to null. |
||
| 88 | * The returned version is published. |
||
| 89 | * |
||
| 90 | * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $userGroupCreateStruct a structure for setting all necessary data to create this user group |
||
| 91 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $parentGroup |
||
| 92 | * |
||
| 93 | * @return \eZ\Publish\API\Repository\Values\User\UserGroup |
||
| 94 | * |
||
| 95 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group |
||
| 96 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the input structure has invalid data |
||
| 97 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupCreateStruct is not valid |
||
| 98 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing |
||
| 99 | */ |
||
| 100 | public function createUserGroup(UserGroupCreateStruct $userGroupCreateStruct, UserGroup $parentGroup) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function loadUserGroup($id, array $prioritizedLanguages = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function loadSubUserGroups(UserGroup $userGroup, $offset = 0, $limit = 25, array $prioritizedLanguages = []) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Removes a user group. |
||
| 123 | * |
||
| 124 | * the users which are not assigned to other groups will be deleted. |
||
| 125 | * |
||
| 126 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 127 | * |
||
| 128 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group |
||
| 129 | */ |
||
| 130 | public function deleteUserGroup(UserGroup $userGroup) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Moves the user group to another parent. |
||
| 137 | * |
||
| 138 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 139 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $newParent |
||
| 140 | * |
||
| 141 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 142 | */ |
||
| 143 | public function moveUserGroup(UserGroup $userGroup, UserGroup $newParent) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Updates the group profile with fields and meta data. |
||
| 150 | * |
||
| 151 | * 4.x: If the versionUpdateStruct is set in $userGroupUpdateStruct, this method internally creates a content draft, updates ts with the provided data |
||
| 152 | * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods. |
||
| 153 | * |
||
| 154 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 155 | * @param \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct $userGroupUpdateStruct |
||
| 156 | * |
||
| 157 | * @return \eZ\Publish\API\Repository\Values\User\UserGroup |
||
| 158 | * |
||
| 159 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 160 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupUpdateStruct is not valid |
||
| 161 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty |
||
| 162 | */ |
||
| 163 | public function updateUserGroup(UserGroup $userGroup, UserGroupUpdateStruct $userGroupUpdateStruct) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Create a new user. The created user is published by this method. |
||
| 170 | * |
||
| 171 | * @param \eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreateStruct the data used for creating the user |
||
| 172 | * @param array $parentGroups the groups of type {@link \eZ\Publish\API\Repository\Values\User\UserGroup} which are assigned to the user after creation |
||
| 173 | * |
||
| 174 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 175 | * |
||
| 176 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user group was not found |
||
| 178 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userCreateStruct is not valid |
||
| 179 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing |
||
| 180 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a user with provided login already exists |
||
| 181 | */ |
||
| 182 | public function createUser(UserCreateStruct $userCreateStruct, array $parentGroups) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function loadUser($userId, array $prioritizedLanguages = []) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Loads anonymous user. |
||
| 197 | * |
||
| 198 | * @deprecated since 5.3, use loadUser( $anonymousUserId ) instead |
||
| 199 | * |
||
| 200 | * |
||
| 201 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 202 | */ |
||
| 203 | public function loadAnonymousUser() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function loadUserByCredentials($login, $password, array $prioritizedLanguages = []) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function loadUserByLogin($login, array $prioritizedLanguages = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function loadUsersByEmail($email, array $prioritizedLanguages = []) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Loads a user with user hash key. |
||
| 234 | * |
||
| 235 | * @param string $hash |
||
| 236 | * @param array $prioritizedLanguages |
||
| 237 | * |
||
| 238 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 239 | */ |
||
| 240 | public function loadUserByToken($hash, array $prioritizedLanguages = []) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * This method deletes a user. |
||
| 247 | * |
||
| 248 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 249 | * |
||
| 250 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete the user |
||
| 251 | */ |
||
| 252 | public function deleteUser(User $user) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Updates a user. |
||
| 259 | * |
||
| 260 | * 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 |
||
| 261 | * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods. |
||
| 262 | * |
||
| 263 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 264 | * @param \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct |
||
| 265 | * |
||
| 266 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update the user |
||
| 267 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userUpdateStruct is not valid |
||
| 268 | * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty |
||
| 269 | * |
||
| 270 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 271 | */ |
||
| 272 | public function updateUser(User $user, UserUpdateStruct $userUpdateStruct) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Update the user token information specified by the user token struct. |
||
| 279 | * |
||
| 280 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 281 | * @param \eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct $userTokenUpdateStruct |
||
| 282 | * |
||
| 283 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 284 | */ |
||
| 285 | public function updateUserToken(User $user, UserTokenUpdateStruct $userTokenUpdateStruct) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Expires user token with user hash. |
||
| 292 | * |
||
| 293 | * @param string $hash |
||
| 294 | */ |
||
| 295 | public function expireUserToken($hash) |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Assigns a new user group to the user. |
||
| 303 | * |
||
| 304 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 305 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 306 | * |
||
| 307 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign the user group to the user |
||
| 308 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is already in the given user group |
||
| 309 | */ |
||
| 310 | public function assignUserToUserGroup(User $user, UserGroup $userGroup) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Removes a user group from the user. |
||
| 317 | * |
||
| 318 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
| 319 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
| 320 | * |
||
| 321 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove the user group from the user |
||
| 322 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is not in the given user group |
||
| 323 | */ |
||
| 324 | public function unAssignUserFromUserGroup(User $user, UserGroup $userGroup) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function loadUserGroupsOfUser(User $user, $offset = 0, $limit = 25, array $prioritizedLanguages = []) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function loadUsersOfUserGroup( |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Instantiate a user create class. |
||
| 351 | * |
||
| 352 | * @param string $login the login of the new user |
||
| 353 | * @param string $email the email of the new user |
||
| 354 | * @param string $password the plain password of the new user |
||
| 355 | * @param string $mainLanguageCode the main language for the underlying content object |
||
| 356 | * @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 |
||
| 357 | * |
||
| 358 | * @return \eZ\Publish\API\Repository\Values\User\UserCreateStruct |
||
| 359 | */ |
||
| 360 | public function newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType = null) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Instantiate a user group create class. |
||
| 367 | * |
||
| 368 | * @param string $mainLanguageCode The main language for the underlying content object |
||
| 369 | * @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 |
||
| 370 | * |
||
| 371 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct |
||
| 372 | */ |
||
| 373 | public function newUserGroupCreateStruct($mainLanguageCode, $contentType = null) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Instantiate a new user update struct. |
||
| 380 | * |
||
| 381 | * @return \eZ\Publish\API\Repository\Values\User\UserUpdateStruct |
||
| 382 | */ |
||
| 383 | public function newUserUpdateStruct() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Instantiate a new user group update struct. |
||
| 390 | * |
||
| 391 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct |
||
| 392 | */ |
||
| 393 | public function newUserGroupUpdateStruct() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | */ |
||
| 401 | public function validatePassword(string $password, PasswordValidationContext $context = null): array |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Checks if Content is a user. |
||
| 408 | */ |
||
| 409 | public function isUser(Content $content): bool |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Checks if Content is a user group. |
||
| 416 | */ |
||
| 417 | public function isUserGroup(Content $content): bool |
||
| 421 | } |
||
| 422 |