Passed
Push — dev ( 336cf2...bc410f )
by Janko
09:30
created

SpacecraftMember::canBeAccessedFrom()   C

Complexity

Conditions 14
Paths 6

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 17.0625

Importance

Changes 0
Metric Value
cc 14
eloc 25
nc 6
nop 3
dl 0
loc 46
ccs 15
cts 20
cp 0.75
crap 17.0625
rs 6.2666
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
namespace Stu\Lib\Interaction\Member;
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\Component\Anomaly\Type\AnomalyTypeEnum;
7
use Stu\Component\Spacecraft\Nbs\NbsUtilityInterface;
8
use Stu\Lib\Interaction\InteractionCheckType;
9
use Stu\Lib\Transfer\CommodityTransferInterface;
10
use Stu\Module\Ship\Lib\TholianWebUtilInterface;
11
use Stu\Orm\Entity\MapInterface;
12
use Stu\Orm\Entity\SpacecraftInterface;
13
use Stu\Orm\Entity\StarSystemMapInterface;
14
use Stu\Orm\Entity\UserInterface;
15
16
class SpacecraftMember implements InteractionMemberInterface
17
{
18 15
    public function __construct(
19
        private NbsUtilityInterface $nbsUtility,
20
        private TholianWebUtilInterface $tholianWebUtil,
21
        private CommodityTransferInterface $commodityTransfer,
22
        private SpacecraftInterface $spacecraft
23 15
    ) {}
24
25 10
    #[Override]
26
    public function get(): SpacecraftInterface
27
    {
28 10
        return $this->spacecraft;
29
    }
30
31 13
    #[Override]
32
    public function canAccess(
33
        InteractionMemberInterface $other,
34
        callable $shouldCheck
35
    ): ?InteractionCheckType {
36
37
        if (
38 13
            $shouldCheck(InteractionCheckType::EXPECT_SOURCE_UNSHIELDED)
39 13
            && $this->spacecraft->getShieldState()
40
        ) {
41
            return InteractionCheckType::EXPECT_SOURCE_UNSHIELDED;
42
        }
43
44
        if (
45 13
            $shouldCheck(InteractionCheckType::EXPECT_SOURCE_UNCLOAKED)
46 13
            && $this->spacecraft->isCloaked()
47
        ) {
48
            return InteractionCheckType::EXPECT_SOURCE_UNCLOAKED;
49
        }
50
51
        if (
52 13
            $shouldCheck(InteractionCheckType::EXPECT_SOURCE_TACHYON)
53 13
            && $other instanceof SpacecraftMember
54 13
            && $other->get()->isCloaked()
55 13
            && !$this->nbsUtility->isTachyonActive($this->spacecraft)
56
        ) {
57
            return InteractionCheckType::EXPECT_SOURCE_TACHYON;
58
        }
59
60 13
        return null;
61
    }
62
63 12
    #[Override]
64
    public function canBeAccessedFrom(
65
        InteractionMemberInterface $other,
66
        bool $isFriend,
67
        callable $shouldCheck
68
    ): ?InteractionCheckType {
69
70 12
        $otherEntity = $other->get();
71
        if (
72 12
            $otherEntity instanceof SpacecraftInterface
73 12
            && $shouldCheck(InteractionCheckType::EXPECT_TARGET_DOCKED_OR_NO_ION_STORM)
74 12
            && $this->spacecraft->getLocation()->hasAnomaly(AnomalyTypeEnum::ION_STORM)
75 12
            && !$this->commodityTransfer->isDockTransfer($this->spacecraft, $otherEntity)
76
        ) {
77
            return InteractionCheckType::EXPECT_TARGET_DOCKED_OR_NO_ION_STORM;
78
        }
79
80
        if (
81 12
            $shouldCheck(InteractionCheckType::EXPECT_TARGET_SAME_USER)
82 12
            && $this->spacecraft->getUser() !== $other->getUser()
83
        ) {
84
            return InteractionCheckType::EXPECT_TARGET_SAME_USER;
85
        }
86
87
        if (
88 12
            $shouldCheck(InteractionCheckType::EXPECT_TARGET_UNSHIELDED)
89 12
            && $this->spacecraft->getShieldState() && !$isFriend
90
        ) {
91
            return InteractionCheckType::EXPECT_TARGET_UNSHIELDED;
92
        }
93
94
        if (
95 12
            $shouldCheck(InteractionCheckType::EXPECT_TARGET_UNCLOAKED)
96 12
            && $this->spacecraft->isCloaked()
97
        ) {
98
            return InteractionCheckType::EXPECT_TARGET_UNCLOAKED;
99
        }
100
101
        if (
102 12
            $shouldCheck(InteractionCheckType::EXPECT_TARGET_ALSO_IN_FINISHED_WEB)
103 12
            && $this->tholianWebUtil->isTargetOutsideFinishedTholianWeb($other->get(), $this->spacecraft)
104
        ) {
105
            return InteractionCheckType::EXPECT_TARGET_ALSO_IN_FINISHED_WEB;
106
        }
107
108 12
        return null;
109
    }
110
111 15
    #[Override]
112
    public function getLocation(): MapInterface|StarSystemMapInterface
113
    {
114 15
        return $this->spacecraft->getLocation();
115
    }
116
117 15
    #[Override]
118
    public function getUser(): ?UserInterface
119
    {
120 15
        return $this->spacecraft->getUser();
121
    }
122
}
123