Passed
Pull Request — master (#2138)
by Janko
27:56 queued 16:09
created

AccessCheck::isFeatureGranted()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace Stu\Module\Control;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
6
use Stu\Lib\AccountNotVerifiedException;
7
use Stu\Module\Config\StuConfigInterface;
8
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
9
10
class AccessCheck implements AccessCheckInterface
11
{
12 11
    public function __construct(
13
        private StuConfigInterface $stuConfig
14 11
    ) {}
15
16 9
    #[Override]
17
    public function checkUserAccess(
18
        ControllerInterface $controller,
19
        GameControllerInterface $game
20
    ): bool {
21
22 9
        if ($controller instanceof NoAccessCheckControllerInterface) {
23 1
            return true;
24
        }
25
26 8
        if ($game->getUser()->getState() === UserEnum::USER_STATE_ACCOUNT_VERIFICATION) {
27 1
            throw new AccountNotVerifiedException();
28
        }
29
30 7
        if (!$controller instanceof AccessCheckControllerInterface) {
31 1
            return true;
32
        }
33
34 6
        $feature = $controller->getFeatureIdentifier();
35 6
        if ($this->isFeatureGranted($game->getUser()->getId(), $feature, $game)) {
36 2
            return true;
37
        }
38
39 4
        $game->addInformation('[b][color=#ff2626]Aktion nicht möglich, Spieler ist nicht berechtigt![/color][/b]');
40
41 4
        return false;
42
    }
43
44 7
    #[Override]
45
    public function isFeatureGranted(int $userId, AccessGrantedFeatureEnum $feature, GameControllerInterface $game): bool
46
    {
47 7
        if ($game->isAdmin()) {
48 1
            return true;
49
        }
50
51 6
        $grantedFeatures = $this->stuConfig->getGameSettings()->getGrantedFeatures();
52 6
        foreach ($grantedFeatures as $entry) {
53
            if (
54 5
                $entry['feature'] === $feature->name
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Module\Control\AccessGrantedFeatureEnum.
Loading history...
55 5
                && in_array($userId, $entry['userIds'])
56
            ) {
57 1
                return true;
58
            }
59
        }
60
61 5
        return false;
62
    }
63
}
64