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

TroopTransferUtility::foreignerCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
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\Ship\Lib\Crew;
6
7
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface;
8
use Stu\Module\Ship\Lib\Interaction\ShipTakeoverManagerInterface;
9
use Stu\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Entity\ShipCrewInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
use Stu\Orm\Entity\UserInterface;
13
14
final class TroopTransferUtility implements TroopTransferUtilityInterface
15
{
16
    private ShipTakeoverManagerInterface $shipTakeoverManager;
17
18
    private ShipCrewCalculatorInterface $shipCrewCalculator;
19
20 9
    public function __construct(
21
        ShipTakeoverManagerInterface $shipTakeoverManager,
22
        ShipCrewCalculatorInterface $shipCrewCalculator
23
    ) {
24 9
        $this->shipTakeoverManager = $shipTakeoverManager;
25 9
        $this->shipCrewCalculator = $shipCrewCalculator;
26
    }
27
28 2
    public function getFreeQuarters(ShipInterface $ship): int
29
    {
30 2
        $free = $this->shipCrewCalculator->getMaxCrewCountByShip($ship) - $ship->getCrewCount();
31
32 2
        return max(0, $free);
33
    }
34
35 3
    public function getBeamableTroopCount(ShipInterface $ship): int
36
    {
37 3
        $buildplan = $ship->getBuildplan();
38 3
        if ($buildplan === null) {
39 1
            return 0;
40
        }
41
42 2
        $free = $ship->getCrewCount() - $buildplan->getCrew();
43
44 2
        return max(0, $free);
45
    }
46
47 1
    public function ownCrewOnTarget(UserInterface $user, ShipInterface $ship): int
48
    {
49 1
        $count = 0;
50
51 1
        foreach ($ship->getCrewAssignments() as $shipCrew) {
52 1
            if ($shipCrew->getCrew()->getUser() === $user) {
53 1
                $count++;
54
            }
55
        }
56
57 1
        return $count;
58
    }
59
60 1
    public function foreignerCount(ShipInterface $ship): int
61
    {
62 1
        $count = 0;
63
64 1
        $user = $ship->getUser();
65
66 1
        foreach ($ship->getCrewAssignments() as $shipCrew) {
67 1
            if ($shipCrew->getCrew()->getUser() !== $user) {
68 1
                $count++;
69
            }
70
        }
71
72 1
        return $count;
73
    }
74
75 2
    public function assignCrew(ShipCrewInterface $crewAssignment, ShipInterface|ColonyInterface $target): void
76
    {
77 2
        $ship = $crewAssignment->getShip();
78 2
        if ($ship !== null) {
79 1
            $ship->getCrewAssignments()->removeElement($crewAssignment);
80
        }
81
82 2
        $colony = $crewAssignment->getColony();
83 2
        if ($colony !== null) {
84 1
            $colony->getCrewAssignments()->removeElement($crewAssignment);
85
        }
86
87 2
        $target->getCrewAssignments()->add($crewAssignment);
88
89 2
        if ($target instanceof ColonyInterface) {
90 1
            $crewAssignment
91 1
                ->setColony($target)
92 1
                ->setShip(null)
93 1
                ->setSlot(null);
94
        } else {
95
            // TODO create CrewSlotAssignment
96
97 1
            $crewAssignment
98 1
                ->setShip($target)
99 1
                ->setColony(null)
100 1
                ->setSlot(null);
101
102
            // clear any ShipTakeover
103 1
            $this->shipTakeoverManager->cancelTakeover(
104 1
                $target->getTakeoverPassive(),
105 1
                ', da das Schiff bemannt wurde'
106 1
            );
107
        }
108
    }
109
}
110