Passed
Pull Request — master (#17)
by Sergei
02:29
created

EnumAccessChecker   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 4
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 4
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A userHasPermission() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Access;
6
7
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum 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 InvalidArgumentException;
9
10
/**
11
 * Access checker interface wrapper for check permission in the form of enumerations instance.
12
 */
13
final class EnumAccessChecker
14
{
15
    private AccessCheckerInterface $accessChecker;
16
17
    public function __construct(AccessCheckerInterface $accessChecker)
18
    {
19
        $this->accessChecker = $accessChecker;
20
    }
21
22
    /**
23
     * Checks if the user with the ID given has the specified permission.
24
     *
25
     * @param int|string|null $userId The user ID representing the unique identifier of a user. If ID is `null`,
26
     * it means user is a guest.
27
     * @param BackedEnum $permission The permission enumeration instance to be checked against.
28
     * @param array $parameters Name-value pairs that will be used to determine if access is granted.
29
     *
30
     * @throws InvalidArgumentException If any of argument is not of the expected type or does not refer to
31
     * an existing value.
32
     *
33
     * @return bool Whether the user has the specified permission.
34
     */
35
    public function userHasPermission(int|string|null $userId, BackedEnum $permission, array $parameters = []): bool
36
    {
37
        return $this->accessChecker->userHasPermission($userId, (string) $permission->value, $parameters);
38
    }
39
}
40