Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

DockPrivilegeUtility::checkPrivilegeFor()   D

Complexity

Conditions 18
Paths 29

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
cc 18
eloc 35
nc 29
nop 2
dl 0
loc 50
ccs 0
cts 34
cp 0
crap 342
rs 4.8666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Station\Dock;
6
7
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...
8
use Stu\Component\Station\Dock\DockModeEnum;
9
use Stu\Component\Station\Dock\DockTypeEnum;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface;
13
14
final class DockPrivilegeUtility implements DockPrivilegeUtilityInterface
15
{
16 1
    public function __construct(private DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository) {}
17
18
    #[Override]
19
    public function checkPrivilegeFor(int $stationId, UserInterface|ShipInterface $source): bool
20
    {
21
        $privileges = $this->dockingPrivilegeRepository->getByStation($stationId);
22
        if ($privileges === []) {
23
            return false;
24
        }
25
26
        $allowed = false;
27
        $user = $source instanceof UserInterface ? $source : $source->getUser();
28
        foreach ($privileges as $priv) {
29
            switch ($priv->getPrivilegeType()) {
30
                case DockTypeEnum::USER:
31
                    if ($priv->getTargetId() === $user->getId()) {
32
                        if ($priv->getPrivilegeMode() == DockModeEnum::DENY) {
33
                            return false;
34
                        }
35
                        $allowed = true;
36
                    }
37
                    break;
38
                case DockTypeEnum::ALLIANCE:
39
                    if ($user->getAlliance() !== null && $priv->getTargetId() === $user->getAlliance()->getId()) {
40
                        if ($priv->getPrivilegeMode() == DockModeEnum::DENY) {
41
                            return false;
42
                        }
43
                        $allowed = true;
44
                    }
45
                    break;
46
                case DockTypeEnum::FACTION:
47
                    if ($priv->getTargetId() == $user->getFactionId()) {
48
                        if ($priv->getPrivilegeMode() == DockModeEnum::DENY) {
49
                            return false;
50
                        }
51
                        $allowed = true;
52
                    }
53
                    break;
54
                case DockTypeEnum::SHIP:
55
                    if (
56
                        $source instanceof ShipInterface
57
                        && $priv->getTargetId() == $source->getId()
58
                    ) {
59
                        if ($priv->getPrivilegeMode() == DockModeEnum::DENY) {
60
                            return false;
61
                        }
62
                        $allowed = true;
63
                    }
64
                    break;
65
            }
66
        }
67
        return $allowed;
68
    }
69
}
70