isGrantedToDelete()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 26
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
namespace App\Module\Client\Delete\Service;
4
5
use App\Application\Data\UserNetworkSessionData;
6
use App\Module\Authorization\Repository\AuthorizationUserRoleFinderRepository;
0 ignored issues
show
Bug introduced by
The type App\Module\Authorization...serRoleFinderRepository 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...
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Check if the authenticated user is permitted to do actions.
11
 * Roles: newcomer < advisor < managing_advisor < administrator.
12
 */
13
final class ClientDeleteAuthorizationChecker
14
{
15
    private ?int $loggedInUserId = null;
16
17 34
    public function __construct(
18
        private readonly AuthorizationUserRoleFinderRepository $authorizationUserRoleFinderRepository,
19
        private readonly UserNetworkSessionData $userNetworkSessionData,
20
        private readonly LoggerInterface $logger,
21
    ) {
22 34
        $this->loggedInUserId = $this->userNetworkSessionData->userId;
23
    }
24
25
    /**
26
     * Check if the authenticated user is allowed to delete client.
27
     *
28
     * @param int|null $ownerId
29
     * @param bool $log log if forbidden (expected false when function is called for privilege setting)
30
     *
31
     * @return bool
32
     */
33 21
    public function isGrantedToDelete(?int $ownerId, bool $log = true): bool
0 ignored issues
show
Unused Code introduced by
The parameter $ownerId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
    public function isGrantedToDelete(/** @scrutinizer ignore-unused */ ?int $ownerId, bool $log = true): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35 21
        if ($this->loggedInUserId === null) {
36
            $this->logger->error('loggedInUserId not set while isGrantedToDelete authorization check');
37
38
            return false;
39
        }
40 21
        $authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId(
41 21
            $this->loggedInUserId
42 21
        );
43
        // Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int]
44
        // * Lower hierarchy number means higher privileged role
45 21
        $userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies();
46
47
        // Only managing_advisor and higher are allowed to delete client so user has to at least have this role
48 21
        if ($authenticatedUserRoleHierarchy <= $userRoleHierarchies['managing_advisor']) {
49 8
            return true;
50
        }
51
52 13
        if ($log === true) {
53 3
            $this->logger->notice(
54 3
                'User ' . $this->loggedInUserId . ' tried to delete client but isn\'t allowed.'
55 3
            );
56
        }
57
58 13
        return false;
59
    }
60
}
61