Passed
Push — dev ( 6e0cf6...2a5475 )
by Janko
14:36
created

ConditionCheckResult::addBlockedShip()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 4
nop 2
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
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\Movement\FlightCompany;
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 7
    public function __construct(
23
        private LeaveFleetInterface $leaveFleet,
24
        private FlightCompany $flightCompany
25
    ) {
26 7
        $this->informations = new InformationWrapper();
27
    }
28
29 7
    public function addBlockedShip(SpacecraftInterface $spacecraft, string $reason): void
30
    {
31 7
        if (!$this->isBlocked($spacecraft)) {
32 7
            $this->blockedSpacecraftIds[] = $spacecraft->getId();
33 7
            $this->informations->addInformation($reason);
34
35 7
            $isLeader = $spacecraft === $this->flightCompany->getLeader();
36 7
            if ($isLeader) {
37 4
                $this->isLeaderBlocked = true;
38
            } elseif (
39 4
                $spacecraft instanceof ShipInterface
40 4
                && !$this->flightCompany->isFixedFleetMode()
41 4
                && !$this->isLeaderBlocked
42
            ) {
43 1
                $this->leaveFleet($spacecraft);
44
            }
45
        }
46
    }
47
48 7
    public function isFlightPossible(): bool
49
    {
50 7
        if ($this->isLeaderBlocked) {
51 4
            return false;
52
        }
53
54 3
        return !$this->flightCompany->isFixedFleetMode() || $this->blockedSpacecraftIds === [];
55
    }
56
57
    /** @return array<int> */
58
    public function getBlockedIds(): array
59
    {
60
        return $this->blockedSpacecraftIds;
61
    }
62
63 7
    public function isBlocked(SpacecraftInterface $spacecraft): bool
64
    {
65 7
        return in_array($spacecraft->getId(), $this->blockedSpacecraftIds);
66
    }
67
68
    /** @return array<string> */
69 7
    public function getInformations(): array
70
    {
71 7
        return $this->informations->getInformations();
72
    }
73
74 1
    private function leaveFleet(ShipInterface $ship): void
75
    {
76 1
        $this->leaveFleet->leaveFleet($ship);
77
78 1
        $this->informations->addInformation(sprintf(
79 1
            _('Die %s hat die Flotte verlassen (%d|%d)'),
80 1
            $ship->getName(),
81 1
            $ship->getPosX(),
82 1
            $ship->getPosY()
83 1
        ));
84
    }
85
}
86