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 RoleService 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 RoleService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class RoleService implements RoleServiceInterface |
||
47 | { |
||
48 | /** @var \eZ\Publish\API\Repository\Repository */ |
||
49 | protected $repository; |
||
50 | |||
51 | /** @var \eZ\Publish\SPI\Persistence\User\Handler */ |
||
52 | protected $userHandler; |
||
53 | |||
54 | /** @var \eZ\Publish\Core\Repository\Helper\LimitationService */ |
||
55 | protected $limitationService; |
||
56 | |||
57 | /** @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper */ |
||
58 | protected $roleDomainMapper; |
||
59 | |||
60 | /** @var array */ |
||
61 | protected $settings; |
||
62 | |||
63 | /** @var \eZ\Publish\API\Repository\PermissionResolver */ |
||
64 | private $permissionResolver; |
||
65 | |||
66 | /** |
||
67 | * Setups service with reference to repository object that created it & corresponding handler. |
||
68 | * |
||
69 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
70 | * @param \eZ\Publish\SPI\Persistence\User\Handler $userHandler |
||
71 | * @param \eZ\Publish\Core\Repository\Helper\LimitationService $limitationService |
||
72 | * @param \eZ\Publish\Core\Repository\Helper\RoleDomainMapper $roleDomainMapper |
||
73 | * @param array $settings |
||
74 | */ |
||
75 | public function __construct( |
||
89 | |||
90 | /** |
||
91 | * Creates a new RoleDraft. |
||
92 | * |
||
93 | * @since 6.0 |
||
94 | * |
||
95 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft |
||
96 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
97 | * if the name of the role already exists or if limitation of the same type |
||
98 | * is repeated in the policy create struct or if limitation is not allowed on module/function |
||
99 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid |
||
100 | * |
||
101 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
102 | * |
||
103 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
104 | */ |
||
105 | public function createRole(APIRoleCreateStruct $roleCreateStruct) |
||
145 | |||
146 | /** |
||
147 | * Creates a new RoleDraft for an existing Role. |
||
148 | * |
||
149 | * @since 6.0 |
||
150 | * |
||
151 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft |
||
152 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the Role already has a RoleDraft that will need to be removed first |
||
153 | * |
||
154 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
155 | * |
||
156 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
157 | */ |
||
158 | View Code Duplication | public function createRoleDraft(APIRole $role) |
|
185 | |||
186 | /** |
||
187 | * Copies an existing Role. |
||
188 | * |
||
189 | * @since 7.5 |
||
190 | * |
||
191 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to copy a Role |
||
192 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
193 | * if the name of the role already exists or if limitation of the same type |
||
194 | * is repeated in the policy create struct or if limitation is not allowed on module/function |
||
195 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCopyStruct is not valid |
||
196 | * |
||
197 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
198 | * @param \eZ\Publish\API\Repository\Values\User\RoleCopyStruct $roleCopyStruct |
||
199 | * |
||
200 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
201 | */ |
||
202 | public function copyRole(APIRole $role, APIRoleCopyStruct $roleCopyStruct) |
||
253 | |||
254 | /** |
||
255 | * Loads a RoleDraft for the given id. |
||
256 | * |
||
257 | * @since 6.0 |
||
258 | * |
||
259 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this RoleDraft |
||
260 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a RoleDraft with the given id was not found |
||
261 | * |
||
262 | * @param mixed $id |
||
263 | * |
||
264 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
265 | */ |
||
266 | View Code Duplication | public function loadRoleDraft($id) |
|
278 | |||
279 | /** |
||
280 | * Loads a RoleDraft by the ID of the role it was created from. |
||
281 | * |
||
282 | * @param mixed $roleId ID of the role the draft was created from. |
||
283 | * |
||
284 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
285 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a RoleDraft with the given id was not found |
||
286 | * |
||
287 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
288 | */ |
||
289 | View Code Duplication | public function loadRoleDraftByRoleId($roleId) |
|
301 | |||
302 | /** |
||
303 | * Updates the properties of a RoleDraft. |
||
304 | * |
||
305 | * @since 6.0 |
||
306 | * |
||
307 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a RoleDraft |
||
308 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the identifier of the RoleDraft already exists |
||
309 | * |
||
310 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
311 | * @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct |
||
312 | * |
||
313 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
314 | */ |
||
315 | public function updateRoleDraft(APIRoleDraft $roleDraft, RoleUpdateStruct $roleUpdateStruct) |
||
365 | |||
366 | /** |
||
367 | * Adds a new policy to the RoleDraft. |
||
368 | * |
||
369 | * @since 6.0 |
||
370 | * |
||
371 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy |
||
372 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create |
||
373 | * struct or if limitation is not allowed on module/function |
||
374 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid |
||
375 | * |
||
376 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
377 | * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct |
||
378 | * |
||
379 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
380 | */ |
||
381 | View Code Duplication | public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct) |
|
428 | |||
429 | /** |
||
430 | * Removes a policy from a RoleDraft. |
||
431 | * |
||
432 | * @since 6.0 |
||
433 | * |
||
434 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy |
||
435 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if policy does not belong to the given RoleDraft |
||
436 | * |
||
437 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
438 | * @param PolicyDraft $policyDraft the policy to remove from the RoleDraft |
||
439 | * |
||
440 | * @return APIRoleDraft if the authenticated user is not allowed to remove a policy |
||
441 | */ |
||
442 | public function removePolicyByRoleDraft(APIRoleDraft $roleDraft, PolicyDraft $policyDraft) |
||
456 | |||
457 | /** |
||
458 | * Updates the limitations of a policy. The module and function cannot be changed and |
||
459 | * the limitations are replaced by the ones in $roleUpdateStruct. |
||
460 | * |
||
461 | * @since 6.0 |
||
462 | * |
||
463 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy |
||
464 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update |
||
465 | * struct or if limitation is not allowed on module/function |
||
466 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid |
||
467 | * |
||
468 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
469 | * @param \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy |
||
470 | * @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct |
||
471 | * |
||
472 | * @return \eZ\Publish\API\Repository\Values\User\PolicyDraft |
||
473 | */ |
||
474 | public function updatePolicyByRoleDraft(APIRoleDraft $roleDraft, PolicyDraft $policy, APIPolicyUpdateStruct $policyUpdateStruct) |
||
522 | |||
523 | /** |
||
524 | * Deletes the given RoleDraft. |
||
525 | * |
||
526 | * @since 6.0 |
||
527 | * |
||
528 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this RoleDraft |
||
529 | * |
||
530 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
531 | */ |
||
532 | View Code Duplication | public function deleteRoleDraft(APIRoleDraft $roleDraft) |
|
545 | |||
546 | /** |
||
547 | * Publishes a given RoleDraft. |
||
548 | * |
||
549 | * @since 6.0 |
||
550 | * |
||
551 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to publish this RoleDraft |
||
552 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the role draft cannot be loaded |
||
553 | * |
||
554 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
555 | */ |
||
556 | public function publishRoleDraft(APIRoleDraft $roleDraft) |
||
581 | |||
582 | /** |
||
583 | * Updates the name of the role. |
||
584 | * |
||
585 | * @deprecated since 6.0, use {@see updateRoleDraft} |
||
586 | * |
||
587 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a role |
||
588 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists |
||
589 | * |
||
590 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
591 | * @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct |
||
592 | * |
||
593 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
594 | */ |
||
595 | public function updateRole(APIRole $role, RoleUpdateStruct $roleUpdateStruct) |
||
640 | |||
641 | /** |
||
642 | * Adds a new policy to the role. |
||
643 | * |
||
644 | * @deprecated since 6.0, use {@see addPolicyByRoleDraft} |
||
645 | * |
||
646 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy |
||
647 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create |
||
648 | * struct or if limitation is not allowed on module/function |
||
649 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid |
||
650 | * |
||
651 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
652 | * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct |
||
653 | * |
||
654 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
655 | */ |
||
656 | View Code Duplication | public function addPolicy(APIRole $role, APIPolicyCreateStruct $policyCreateStruct) |
|
703 | |||
704 | /** |
||
705 | * Deletes a policy. |
||
706 | * |
||
707 | * @deprecated since 6.0, use {@link removePolicyByRoleDraft()} instead. |
||
708 | * |
||
709 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy |
||
710 | * |
||
711 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy the policy to delete |
||
712 | */ |
||
713 | View Code Duplication | public function deletePolicy(APIPolicy $policy) |
|
724 | |||
725 | /** |
||
726 | * Deletes a policy. |
||
727 | * |
||
728 | * Used by {@link removePolicy()} and {@link deletePolicy()} |
||
729 | * |
||
730 | * @param APIPolicy $policy |
||
731 | * |
||
732 | * @throws \Exception |
||
733 | */ |
||
734 | View Code Duplication | protected function internalDeletePolicy(APIPolicy $policy) |
|
745 | |||
746 | /** |
||
747 | * Updates the limitations of a policy. The module and function cannot be changed and |
||
748 | * the limitations are replaced by the ones in $roleUpdateStruct. |
||
749 | * |
||
750 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy |
||
751 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update |
||
752 | * struct or if limitation is not allowed on module/function |
||
753 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid |
||
754 | * |
||
755 | * @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct |
||
756 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy |
||
757 | * |
||
758 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
759 | */ |
||
760 | public function updatePolicy(APIPolicy $policy, APIPolicyUpdateStruct $policyUpdateStruct) |
||
803 | |||
804 | /** |
||
805 | * Loads a role for the given id. |
||
806 | * |
||
807 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
808 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given id was not found |
||
809 | * |
||
810 | * @param mixed $id |
||
811 | * |
||
812 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
813 | */ |
||
814 | View Code Duplication | public function loadRole($id) |
|
826 | |||
827 | /** |
||
828 | * Loads a role for the given identifier. |
||
829 | * |
||
830 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
831 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given name was not found |
||
832 | * |
||
833 | * @param string $identifier |
||
834 | * |
||
835 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
836 | */ |
||
837 | View Code Duplication | public function loadRoleByIdentifier($identifier) |
|
853 | |||
854 | /** |
||
855 | * Loads all roles, excluding the ones the current user is not allowed to read. |
||
856 | * |
||
857 | * @return \eZ\Publish\API\Repository\Values\User\Role[] |
||
858 | */ |
||
859 | public function loadRoles() |
||
877 | |||
878 | /** |
||
879 | * Deletes the given role. |
||
880 | * |
||
881 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role |
||
882 | * |
||
883 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
884 | */ |
||
885 | View Code Duplication | public function deleteRole(APIRole $role) |
|
902 | |||
903 | /** |
||
904 | * Loads all policies from roles which are assigned to a user or to user groups to which the user belongs. |
||
905 | * |
||
906 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given id was not found |
||
907 | * |
||
908 | * @param mixed $userId |
||
909 | * |
||
910 | * @return \eZ\Publish\API\Repository\Values\User\Policy[] |
||
911 | */ |
||
912 | public function loadPoliciesByUserId($userId) |
||
927 | |||
928 | /** |
||
929 | * Assigns a role to the given user group. |
||
930 | * |
||
931 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
932 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
933 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If assignment already exists |
||
934 | * |
||
935 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
936 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
937 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
938 | */ |
||
939 | View Code Duplication | public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null) |
|
975 | |||
976 | /** |
||
977 | * removes a role from the given user group. |
||
978 | * |
||
979 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role |
||
980 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the given user group |
||
981 | * |
||
982 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
983 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
984 | */ |
||
985 | View Code Duplication | public function unassignRoleFromUserGroup(APIRole $role, UserGroup $userGroup) |
|
1016 | |||
1017 | /** |
||
1018 | * Assigns a role to the given user. |
||
1019 | * |
||
1020 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
1021 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
1022 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If assignment already exists |
||
1023 | * |
||
1024 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
1025 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
1026 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
1027 | */ |
||
1028 | View Code Duplication | public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null) |
|
1064 | |||
1065 | /** |
||
1066 | * removes a role from the given user. |
||
1067 | * |
||
1068 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role |
||
1069 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the user |
||
1070 | * |
||
1071 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
1072 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
1073 | */ |
||
1074 | View Code Duplication | public function unassignRoleFromUser(APIRole $role, User $user) |
|
1105 | |||
1106 | /** |
||
1107 | * Removes the given role assignment. |
||
1108 | * |
||
1109 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role assignment |
||
1110 | * |
||
1111 | * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment $roleAssignment |
||
1112 | */ |
||
1113 | View Code Duplication | public function removeRoleAssignment(RoleAssignment $roleAssignment) |
|
1130 | |||
1131 | /** |
||
1132 | * Loads a role assignment for the given id. |
||
1133 | * |
||
1134 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
1135 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the role assignment was not found |
||
1136 | * |
||
1137 | * @param mixed $roleAssignmentId |
||
1138 | * |
||
1139 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment |
||
1140 | */ |
||
1141 | public function loadRoleAssignment($roleAssignmentId) |
||
1177 | |||
1178 | /** |
||
1179 | * Returns the assigned user and user groups to this role. |
||
1180 | * |
||
1181 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role |
||
1182 | * |
||
1183 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
1184 | * |
||
1185 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[] |
||
1186 | */ |
||
1187 | public function getRoleAssignments(APIRole $role) |
||
1223 | |||
1224 | /** |
||
1225 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
||
1226 | */ |
||
1227 | public function getRoleAssignmentsForUser(User $user, $inherited = false) |
||
1256 | |||
1257 | /** |
||
1258 | * Returns the roles assigned to the given user group, excluding the ones the current user is not allowed to read. |
||
1259 | * |
||
1260 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
1261 | * |
||
1262 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment[] |
||
1263 | */ |
||
1264 | public function getRoleAssignmentsForUserGroup(UserGroup $userGroup) |
||
1282 | |||
1283 | /** |
||
1284 | * Instantiates a role create class. |
||
1285 | * |
||
1286 | * @param string $name |
||
1287 | * |
||
1288 | * @return \eZ\Publish\API\Repository\Values\User\RoleCreateStruct |
||
1289 | */ |
||
1290 | public function newRoleCreateStruct($name) |
||
1299 | |||
1300 | /** |
||
1301 | * Instantiates a role copy class. |
||
1302 | * |
||
1303 | * @param string $name |
||
1304 | * |
||
1305 | * @return \eZ\Publish\API\Repository\Values\User\RoleCopyStruct |
||
1306 | */ |
||
1307 | public function newRoleCopyStruct($name) |
||
1316 | |||
1317 | /** |
||
1318 | * Instantiates a policy create class. |
||
1319 | * |
||
1320 | * @param string $module |
||
1321 | * @param string $function |
||
1322 | * |
||
1323 | * @return \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct |
||
1324 | */ |
||
1325 | public function newPolicyCreateStruct($module, $function) |
||
1335 | |||
1336 | /** |
||
1337 | * Instantiates a policy update class. |
||
1338 | * |
||
1339 | * @return \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct |
||
1340 | */ |
||
1341 | public function newPolicyUpdateStruct() |
||
1349 | |||
1350 | /** |
||
1351 | * Instantiates a policy update class. |
||
1352 | * |
||
1353 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
1354 | */ |
||
1355 | public function newRoleUpdateStruct() |
||
1359 | |||
1360 | /** |
||
1361 | * Returns the LimitationType registered with the given identifier. |
||
1362 | * |
||
1363 | * Returns the correct implementation of API Limitation value object |
||
1364 | * based on provided identifier |
||
1365 | * |
||
1366 | * @param string $identifier |
||
1367 | * |
||
1368 | * @return \eZ\Publish\SPI\Limitation\Type |
||
1369 | * |
||
1370 | * @throws \RuntimeException if there is no LimitationType with $identifier |
||
1371 | */ |
||
1372 | public function getLimitationType($identifier) |
||
1376 | |||
1377 | /** |
||
1378 | * Returns the LimitationType's assigned to a given module/function. |
||
1379 | * |
||
1380 | * Typically used for: |
||
1381 | * - Internal validation limitation value use on Policies |
||
1382 | * - Role admin gui for editing policy limitations incl list limitation options via valueSchema() |
||
1383 | * |
||
1384 | * @param string $module Legacy name of "controller", it's a unique identifier like "content" |
||
1385 | * @param string $function Legacy name of a controller "action", it's a unique within the controller like "read" |
||
1386 | * |
||
1387 | * @return \eZ\Publish\SPI\Limitation\Type[] |
||
1388 | * |
||
1389 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If module/function to limitation type mapping |
||
1390 | * refers to a non existing identifier. |
||
1391 | */ |
||
1392 | public function getLimitationTypesByModuleFunction($module, $function) |
||
1413 | |||
1414 | /** |
||
1415 | * Validates Policies and Limitations in Role create struct. |
||
1416 | * |
||
1417 | * @uses ::validatePolicy() |
||
1418 | * |
||
1419 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
1420 | * |
||
1421 | * @return \eZ\Publish\Core\FieldType\ValidationError[][][] |
||
1422 | */ |
||
1423 | View Code Duplication | protected function validateRoleCreateStruct(APIRoleCreateStruct $roleCreateStruct) |
|
1440 | |||
1441 | /** |
||
1442 | * Validates Policies and Limitations in Role copy struct. |
||
1443 | * |
||
1444 | * @uses ::validatePolicy() |
||
1445 | * |
||
1446 | * @param \eZ\Publish\API\Repository\Values\User\RoleCopyStruct $roleCopyStruct |
||
1447 | * |
||
1448 | * @return \eZ\Publish\Core\FieldType\ValidationError[][][] |
||
1449 | */ |
||
1450 | View Code Duplication | protected function validateRoleCopyStruct(APIRoleCopyStruct $roleCopyStruct) |
|
1467 | |||
1468 | /** |
||
1469 | * Validates Policy context: Limitations on a module and function. |
||
1470 | * |
||
1471 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If the same limitation is repeated or if |
||
1472 | * limitation is not allowed on module/function |
||
1473 | * |
||
1474 | * @param string $module |
||
1475 | * @param string $function |
||
1476 | * @param \eZ\Publish\API\Repository\Values\User\Limitation[] $limitations |
||
1477 | * |
||
1478 | * @return \eZ\Publish\Core\FieldType\ValidationError[][] |
||
1479 | */ |
||
1480 | protected function validatePolicy($module, $function, array $limitations) |
||
1505 | |||
1506 | /** |
||
1507 | * Validate that assignments not already exists and filter validations against existing. |
||
1508 | * |
||
1509 | * @param mixed $contentId |
||
1510 | * @param SPIRole $spiRole |
||
1511 | * @param array|null $limitation |
||
1512 | * |
||
1513 | * @return array[]|null Filtered version of $limitation |
||
1514 | * |
||
1515 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If assignment already exists |
||
1516 | */ |
||
1517 | protected function checkAssignmentAndFilterLimitationValues($contentId, SPIRole $spiRole, array $limitation = null) |
||
1559 | } |
||
1560 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.