Passed
Push — master ( 8ff4e3...ffee1f )
by Samuel
03:00
created

UserFinder::findUserReadResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 35
ccs 24
cts 24
cp 1
rs 9.6333
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace App\Domain\User\Service;
4
5
use App\Domain\Authentication\Exception\ForbiddenException;
6
use App\Domain\Exception\DomainRecordNotFoundException;
7
use App\Domain\User\Data\UserData;
8
use App\Domain\User\Data\UserResultData;
9
use App\Domain\User\Repository\UserFinderRepository;
10
use App\Domain\User\Service\Authorization\AuthorizedUserRoleFilterer;
11
use App\Domain\User\Service\Authorization\UserPermissionVerifier;
12
use App\Domain\User\Service\Authorization\UserPrivilegeDeterminer;
0 ignored issues
show
Bug introduced by
The type App\Domain\User\Service\...UserPrivilegeDeterminer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
// Class cannot be readonly as it's mocked (doubled) in tests
15
class UserFinder
16
{
17 198
    public function __construct(
18
        private readonly UserFinderRepository $userFinderRepository,
19
        private readonly UserPrivilegeDeterminer $userPrivilegeDeterminer,
20
        private readonly AuthorizedUserRoleFilterer $authorizedUserRoleFilterer,
21
        private readonly UserPermissionVerifier $userPermissionVerifier,
22
    ) {
23 198
    }
24
25
    /**
26
     * @return UserResultData[]
27
     */
28 4
    public function findAllUsersResultDataForList(): array
29
    {
30 4
        $userResultArray = $this->userFinderRepository->findAllUsersForList();
31
32 4
        foreach ($userResultArray as $key => $userResultData) {
33
            // Check if authenticated user is allowed to read user
34 4
            if ($this->userPermissionVerifier->isGrantedToRead($userResultData->id)) {
35
                // Authorization limits which entries are in the user role dropdown
36 4
                $userResultData->availableUserRoles = $this->authorizedUserRoleFilterer->filterAuthorizedUserRoles(
37 4
                    $userResultData->userRoleId
38 4
                );
39 4
                $userResultData->userRolePrivilege = $this->userPrivilegeDeterminer->getUserRoleAssignmentPrivilege(
40 4
                    $userResultData->availableUserRoles
41 4
                );
42
43
                // Check if user is allowed to change status
44 4
                $userResultData->statusPrivilege = $this->userPrivilegeDeterminer->getMutationPrivilege(
45 4
                    (int)$userResultData->id,
46 4
                    'status',
47 4
                );
48
                // Personal info privilege like first name, email and so on no needed for list
49
                // $userResultData->generalPrivilege = $this->userPermissionVerifier->getUpdatePrivilegeForUserColumn(
50
                // 'personal_info', $userResultData->id );
51
            } else {
52 1
                unset($userResultArray[$key]);
53
            }
54
        }
55
56 4
        return $userResultArray;
57
    }
58
59
    /**
60
     * @param string|int|null $id
61
     *
62
     * @return UserData
63
     */
64 152
    public function findUserById(string|int|null $id): UserData
65
    {
66
        // Find user in database and return object
67 152
        return $id ? new UserData($this->userFinderRepository->findUserById((int)$id)) : new UserData();
68
    }
69
70
    /**
71
     * Find user with authorization check and privilege attributes.
72
     *
73
     * @param int $id
74
     *
75
     * @throws \Exception
76
     *
77
     * @return UserResultData
78
     */
79 5
    public function findUserReadResult(int $id): UserResultData
80
    {
81 5
        if ($this->userPermissionVerifier->isGrantedToRead($id)) {
82 4
            $userRow = $this->userFinderRepository->findUserById($id);
83 4
            if (!empty($userRow)) {
84 3
                $userResultData = new UserResultData($userRow);
85
                // Status privilege
86 3
                $userResultData->statusPrivilege = $this->userPrivilegeDeterminer->getMutationPrivilege(
87 3
                    $id,
88 3
                    'status',
89 3
                );
90
                // Available user roles for dropdown and privilege
91 3
                $userResultData->availableUserRoles = $this->authorizedUserRoleFilterer->filterAuthorizedUserRoles(
92 3
                    $userResultData->userRoleId
93 3
                );
94 3
                $userResultData->userRolePrivilege = $this->userPrivilegeDeterminer->getUserRoleAssignmentPrivilege(
95 3
                    $userResultData->availableUserRoles
96 3
                );
97
98
                // Personal info privilege like first name, email and so on
99 3
                $userResultData->generalPrivilege = $this->userPrivilegeDeterminer->getMutationPrivilege(
100 3
                    $id,
101 3
                    'personal_info',
102 3
                );
103
                // Password change without verification of old password
104 3
                $userResultData->passwordWithoutVerificationPrivilege = $this->userPrivilegeDeterminer->
105 3
                getMutationPrivilege($id, 'password_without_verification');
106
107 3
                return $userResultData;
108
            }
109
            // When user allowed to read, and it doesn't exist indicate that the resource was not found
110 1
            throw new DomainRecordNotFoundException('User not found.');
111
        }
112
        // Forbidden when not found and user is not allowed to read
113 1
        throw new ForbiddenException('Not allowed to read user.');
114
    }
115
116
    /**
117
     * Find user via email.
118
     *
119
     * @param string $email
120
     *
121
     * @return UserData
122
     */
123
    public function findUserByEmail(string $email): UserData
124
    {
125
        return $this->userFinderRepository->findUserByEmail($email);
126
    }
127
}
128