Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

AttackedBattleParty::addDockedTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Battle\Party;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Stu\Module\Ship\Lib\ShipWrapperInterface;
8
9
class AttackedBattleParty extends AbstractBattleParty
10
{
11 7
    public function __construct(
12
        ShipWrapperInterface $attackedWrapper
13
    ) {
14 7
        parent::__construct(
15 7
            $attackedWrapper
16 7
        );
17
    }
18
19 7
    protected function initMembers(): Collection
20
    {
21 7
        $fleetWrapper = $this->leader->getFleetWrapper();
22
23 7
        if ($fleetWrapper !== null) {
24
25 3
            $spacecrafts = $fleetWrapper->getShipWrappers();
26
27 3
            $this->addDockedTo($spacecrafts);
28
29
            // only uncloaked defenders fight
30 3
            $uncloakedDefenders = $spacecrafts
31 3
                ->filter(fn (ShipWrapperInterface $wrapper) => !$wrapper->get()->getCloakState());
32
33
            // if all defenders were cloaked, they obviously were scanned and enter the fight as a whole fleet
34 3
            return $uncloakedDefenders->isEmpty()
35 1
                ? $fleetWrapper->getShipWrappers()
36 3
                : $uncloakedDefenders;
37
        } else {
38
39 4
            $spacecrafts = new ArrayCollection([$this->leader->get()->getId() => $this->leader]);
40
41 4
            $this->addDockedTo($spacecrafts);
42
43 4
            return $spacecrafts;
44
        }
45
    }
46
47
    /** @param Collection<int, ShipWrapperInterface> $spacecrafts */
48 7
    private function addDockedTo(Collection $spacecrafts): void
49
    {
50 7
        $dockedTo = $spacecrafts
51 7
            ->filter(fn (ShipWrapperInterface $wrapper) => $wrapper->getDockedToShipWrapper() !== null)
52 7
            ->map(fn (ShipWrapperInterface $wrapper) => $wrapper->getDockedToShipWrapper())
53 7
            ->filter(fn (ShipWrapperInterface $dockedTo) => !$dockedTo->get()->getUser()->isNpc());
54
55
        /** @var ShipWrapperInterface $wrapper */
56 7
        foreach ($dockedTo as $wrapper) {
57 3
            if (!$spacecrafts->contains($wrapper)) {
58 3
                $spacecrafts->set($wrapper->get()->getId(), $wrapper);
59
            }
60
        }
61
    }
62
}
63