Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

DockPrivilegeUtility::checkPrivilegeFor()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 12
nop 2
dl 0
loc 36
ccs 0
cts 26
cp 0
crap 182
rs 6.6166
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\Module\Ship\Lib\Interaction;
6
7
use Stu\Component\Ship\ShipEnum;
8
use Stu\Orm\Entity\UserInterface;
9
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface;
10
11
final class DockPrivilegeUtility implements DockPrivilegeUtilityInterface
12
{
13
    private DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository;
14
15
    public function __construct(
16
        DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository
17
    ) {
18
        $this->dockingPrivilegeRepository = $dockingPrivilegeRepository;
19
    }
20
21
    public function checkPrivilegeFor(int $shipId, UserInterface $user): bool
22
    {
23
        $privileges = $this->dockingPrivilegeRepository->getByShip($shipId);
24
        if ($privileges === []) {
25
            return false;
26
        }
27
        $allowed = false;
28
        foreach ($privileges as $priv) {
29
            switch ($priv->getPrivilegeType()) {
30
                case ShipEnum::DOCK_PRIVILEGE_USER:
31
                    if ($priv->getTargetId() === $user->getId()) {
32
                        if ($priv->getPrivilegeMode() == ShipEnum::DOCK_PRIVILEGE_MODE_DENY) {
33
                            return false;
34
                        }
35
                        $allowed = true;
36
                    }
37
                    break;
38
                case ShipEnum::DOCK_PRIVILEGE_ALLIANCE:
39
                    if ($user->getAlliance() !== null && $priv->getTargetId() === $user->getAlliance()->getId()) {
40
                        if ($priv->getPrivilegeMode() == ShipEnum::DOCK_PRIVILEGE_MODE_DENY) {
41
                            return false;
42
                        }
43
                        $allowed = true;
44
                    }
45
                    break;
46
                case ShipEnum::DOCK_PRIVILEGE_FACTION:
47
                    if ($priv->getTargetId() == $user->getFactionId()) {
48
                        if ($priv->getPrivilegeMode() == ShipEnum::DOCK_PRIVILEGE_MODE_DENY) {
49
                            return false;
50
                        }
51
                        $allowed = true;
52
                    }
53
                    break;
54
            }
55
        }
56
        return $allowed;
57
    }
58
}
59