Code Duplication    Length = 31-31 lines in 2 locations

eZ/Publish/Core/Repository/RoleService.php 2 locations

@@ 950-980 (lines=31) @@
947
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
948
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
949
     */
950
    public function unassignRoleFromUserGroup(APIRole $role, UserGroup $userGroup)
951
    {
952
        if ($this->repository->canUser('role', 'assign', $userGroup, $role) !== true) {
953
            throw new UnauthorizedException('role', 'assign');
954
        }
955
956
        $spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId($userGroup->id);
957
        $isAssigned = false;
958
        foreach ($spiRoleAssignments as $spiRoleAssignment) {
959
            if ($spiRoleAssignment->roleId === $role->id) {
960
                $isAssigned = true;
961
                break;
962
            }
963
        }
964
965
        if (!$isAssigned) {
966
            throw new InvalidArgumentException(
967
                '$userGroup',
968
                'Role is not assigned to the given UserGroup'
969
            );
970
        }
971
972
        $this->repository->beginTransaction();
973
        try {
974
            $this->userHandler->unassignRole($userGroup->id, $role->id);
975
            $this->repository->commit();
976
        } catch (Exception $e) {
977
            $this->repository->rollback();
978
            throw $e;
979
        }
980
    }
981
982
    /**
983
     * Assigns a role to the given user.
@@ 1039-1069 (lines=31) @@
1036
     * @param \eZ\Publish\API\Repository\Values\User\Role $role
1037
     * @param \eZ\Publish\API\Repository\Values\User\User $user
1038
     */
1039
    public function unassignRoleFromUser(APIRole $role, User $user)
1040
    {
1041
        if ($this->repository->canUser('role', 'assign', $user, $role) !== true) {
1042
            throw new UnauthorizedException('role', 'assign');
1043
        }
1044
1045
        $spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId($user->id);
1046
        $isAssigned = false;
1047
        foreach ($spiRoleAssignments as $spiRoleAssignment) {
1048
            if ($spiRoleAssignment->roleId === $role->id) {
1049
                $isAssigned = true;
1050
                break;
1051
            }
1052
        }
1053
1054
        if (!$isAssigned) {
1055
            throw new InvalidArgumentException(
1056
                '$user',
1057
                'Role is not assigned to the given User'
1058
            );
1059
        }
1060
1061
        $this->repository->beginTransaction();
1062
        try {
1063
            $this->userHandler->unassignRole($user->id, $role->id);
1064
            $this->repository->commit();
1065
        } catch (Exception $e) {
1066
            $this->repository->rollback();
1067
            throw $e;
1068
        }
1069
    }
1070
1071
    /**
1072
     * Removes the given role assignment.