Passed
Push — dev ( 587b46...930c46 )
by Janko
16:07
created

DockPrivilegeUtility::checkPrivilegeFor()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
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\DockingPrivilegeInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
use Stu\Orm\Entity\StationInterface;
13
use Stu\Orm\Entity\UserInterface;
14
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface;
15
16
final class DockPrivilegeUtility implements DockPrivilegeUtilityInterface
17
{
18 28
    public function __construct(private readonly DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository) {}
19
20 27
    #[Override]
21
    public function checkPrivilegeFor(StationInterface $station, UserInterface|ShipInterface $source): bool
22
    {
23
        try {
24 27
            return array_reduce(
25 27
                $this->dockingPrivilegeRepository->getByStation($station),
26 27
                fn(bool $isAllowed, DockingPrivilegeInterface $privilege): bool => $isAllowed || $this->isAllowed($privilege, $source),
27 27
                false
28 27
            );
29 7
        } catch (DockingUnallowedException) {
30 7
            return false;
31
        }
32
    }
33
34 27
    private function isAllowed(DockingPrivilegeInterface $privilege, UserInterface|ShipInterface $source): bool
35
    {
36 27
        $user = $source instanceof UserInterface ? $source : $source->getUser();
37
38 27
        $isMatch = match ($privilege->getPrivilegeType()) {
39 27
            DockTypeEnum::USER => $privilege->getTargetId() === $user->getId(),
40 20
            DockTypeEnum::ALLIANCE => $user->getAlliance() !== null && $privilege->getTargetId() === $user->getAlliance()->getId(),
41 13
            DockTypeEnum::FACTION => $privilege->getTargetId() == $user->getFactionId(),
42 6
            DockTypeEnum::SHIP => $source instanceof ShipInterface && $privilege->getTargetId() == $source->getId(),
43 27
        };
44
45 27
        if (!$isMatch) {
46 11
            return false;
47
        }
48
49 18
        if ($privilege->getPrivilegeMode() === DockModeEnum::DENY) {
50 7
            throw new DockingUnallowedException();
51
        }
52
53 11
        return true;
54
    }
55
}
56