Passed
Push — master ( f80fed...0867c9 )
by Nico
11:47
created

ConditionCheckResult::isFlightPossible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight;
6
7
use Stu\Lib\Information\InformationWrapper;
8
use Stu\Module\Ship\Lib\Fleet\LeaveFleetInterface;
9
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\SpacecraftInterface;
12
13
class ConditionCheckResult
14
{
15
    /** @var array<int> */
16
    private array $blockedSpacecraftIds = [];
17
18
    private bool $isLeaderBlocked = false;
19
20
    private InformationWrapper $informations;
21
22 9
    public function __construct(
23
        private LeaveFleetInterface $leaveFleet,
24
        private SpacecraftWrapperInterface $leader,
25
        private bool $isFixedFleetMode
26
    ) {
27 9
        $this->informations = new InformationWrapper();
28
    }
29
30 7
    public function addBlockedShip(SpacecraftInterface $spacecraft, string $reason): void
31
    {
32 7
        if (!$this->isBlocked($spacecraft)) {
33 7
            $this->blockedSpacecraftIds[] = $spacecraft->getId();
34 7
            $this->informations->addInformation($reason);
35
36 7
            if ($this->isLeaderBlocked($spacecraft)) {
37 5
                $this->isLeaderBlocked = true;
38
            } elseif (
39 3
                $spacecraft instanceof ShipInterface
40 3
                && !$this->isFixedFleetMode
41 3
                && !$this->isLeaderBlocked
42 3
                && $spacecraft !== $this->leader->get()
43
            ) {
44 1
                $this->leaveFleet($spacecraft);
45
            }
46
        }
47
    }
48
49 7
    private function isLeaderBlocked(SpacecraftInterface $spacecraft): bool
50
    {
51 7
        return !$spacecraft instanceof ShipInterface
52 7
            || $spacecraft->isFleetLeader()
53 7
            || $spacecraft->getFleet() === null
54 7
            || $spacecraft === $this->leader->get();
55
    }
56
57 7
    public function isFlightPossible(): bool
58
    {
59 7
        if ($this->isLeaderBlocked) {
60 5
            return false;
61
        }
62
63 2
        return !$this->isFixedFleetMode || $this->blockedSpacecraftIds === [];
64
    }
65
66
    /** @return array<int> */
67
    public function getBlockedIds(): array
68
    {
69
        return $this->blockedSpacecraftIds;
70
    }
71
72 7
    public function isBlocked(SpacecraftInterface $spacecraft): bool
73
    {
74 7
        return in_array($spacecraft->getId(), $this->blockedSpacecraftIds);
75
    }
76
77
    /** @return array<string> */
78 7
    public function getInformations(): array
79
    {
80 7
        return $this->informations->getInformations();
81
    }
82
83 1
    private function leaveFleet(ShipInterface $ship): void
84
    {
85 1
        $this->leaveFleet->leaveFleet($ship);
86
87 1
        $this->informations->addInformation(sprintf(
88 1
            _('Die %s hat die Flotte verlassen (%d|%d)'),
89 1
            $ship->getName(),
90 1
            $ship->getPosX(),
91 1
            $ship->getPosY()
92 1
        ));
93
    }
94
}
95