UserReadFinder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Module\User\Read\Service;
4
5
use App\Domain\Exception\DomainRecordNotFoundException;
6
use App\Module\Authorization\Exception\ForbiddenException;
7
use App\Module\User\Authorization\Service\UserPrivilegeDeterminer;
0 ignored issues
show
Bug introduced by
The type App\Module\User\Authoriz...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...
8
use App\Module\User\Data\UserResultData;
9
use App\Module\User\Find\Repository\UserFinderRepository;
10
use App\Module\User\FindDropdownOptions\Service\AuthorizedUserRoleFilterer;
0 ignored issues
show
Bug introduced by
The type App\Module\User\FindDrop...horizedUserRoleFilterer 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...
11
12
// Class cannot be readonly as it's mocked (doubled) in tests
13
readonly class UserReadFinder
14
{
15 5
    public function __construct(
16
        private UserFinderRepository $userFinderRepository,
17
        private UserPrivilegeDeterminer $userPrivilegeDeterminer,
18
        private AuthorizedUserRoleFilterer $authorizedUserRoleFilterer,
19
        private UserReadAuthorizationChecker $userReadAuthorizationChecker,
20
    ) {
21 5
    }
22
23
    /**
24
     * Find user with authorization check and privilege attributes.
25
     *
26
     * @param int $id
27
     *
28
     * @throws \Exception
29
     *
30
     * @return UserResultData
31
     */
32 5
    public function findUserReadResult(int $id): UserResultData
33
    {
34 5
        if ($this->userReadAuthorizationChecker->isGrantedToRead($id)) {
35 4
            $userRow = $this->userFinderRepository->findUserById($id);
36 4
            if (!empty($userRow)) {
37 3
                $userResultData = new UserResultData($userRow);
38
                // Status privilege
39 3
                $userResultData->statusPrivilege = $this->userPrivilegeDeterminer->getMutationPrivilege(
40 3
                    $id,
41 3
                    'status',
42 3
                );
43
                // Available user roles for dropdown and privilege
44 3
                $userResultData->availableUserRoles = $this->authorizedUserRoleFilterer->filterAuthorizedUserRoles(
45 3
                    $userResultData->userRoleId
46 3
                );
47 3
                $userResultData->userRolePrivilege = $this->userPrivilegeDeterminer->getUserRoleAssignmentPrivilege(
48 3
                    $userResultData->availableUserRoles
49 3
                );
50
51
                // Personal info privilege like first name, email and so on
52 3
                $userResultData->generalPrivilege = $this->userPrivilegeDeterminer->getMutationPrivilege(
53 3
                    $id,
54 3
                    'personal_info',
55 3
                );
56
                // Password change without verification of old password
57 3
                $userResultData->passwordWithoutVerificationPrivilege = $this->userPrivilegeDeterminer->
58 3
                getMutationPrivilege($id, 'password_without_verification');
59
60 3
                return $userResultData;
61
            }
62
            // When user allowed to read, and it doesn't exist indicate that the resource was not found
63 1
            throw new DomainRecordNotFoundException('User not found.');
64
        }
65
        // Forbidden when not found and user is not allowed to read
66 1
        throw new ForbiddenException('Not allowed to read user.');
67
    }
68
}
69