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

RoundBasedBattleParty::isDone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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 RuntimeException;
8
use Stu\Module\Ship\Lib\ShipWrapperInterface;
9
use Stu\Orm\Repository\ShipRepositoryInterface;
10
11
class RoundBasedBattleParty
12
{
13
    /** @var Collection<int, int> */
14
    private Collection $unUsedIds;
15
16 7
    public function __construct(
17
        private BattlePartyInterface $battleParty,
18
        private ShipRepositoryInterface $shipRepository
19
    ) {
20 7
        $this->unUsedIds = new ArrayCollection($battleParty->getActiveMembers(true)->getKeys());
21
    }
22
23 4
    public function get(): BattlePartyInterface
24
    {
25 4
        return $this->battleParty;
26
    }
27
28 3
    public function use(int $spacecraftId): void
29
    {
30 3
        $this->unUsedIds->removeElement($spacecraftId);
31
    }
32
33 4
    public function isDone(): bool
34
    {
35 4
        return $this->isUsed() || $this->getAllUnusedThatCanFire()->isEmpty();
36
    }
37
38 4
    private function isUsed(): bool
39
    {
40 4
        return $this->unUsedIds->isEmpty();
41
    }
42
43
    /** @return Collection<int, ShipWrapperInterface> */
44 2
    public function getAllUnusedThatCanFire(): Collection
45
    {
46 2
        return $this->get()
47 2
            ->getActiveMembers(true)
48 2
            ->filter(fn (ShipWrapperInterface $wrapper) => $this->unUsedIds->contains($wrapper->get()->getId()));
49
    }
50
51 1
    public function getRandomUnused(): ShipWrapperInterface
52
    {
53 1
        $allUnusedThatCanFire = $this->getAllUnusedThatCanFire();
54
55
        /** @var ShipWrapperInterface|null */
56 1
        $random = $allUnusedThatCanFire->get(array_rand($allUnusedThatCanFire->toArray()));
0 ignored issues
show
Bug introduced by
It seems like array_rand($allUnusedThatCanFire->toArray()) can also be of type array; however, parameter $key of Doctrine\Common\Collecti...adableCollection::get() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $random = $allUnusedThatCanFire->get(/** @scrutinizer ignore-type */ array_rand($allUnusedThatCanFire->toArray()));
Loading history...
57 1
        if ($random === null) {
58
            throw new RuntimeException('isDone shoule be called first!');
59
        }
60
61 1
        $this->use($random->get()->getId());
62
63 1
        return $random;
64
    }
65
66 1
    public function saveActiveMembers(): void
67
    {
68 1
        foreach ($this->get()->getActiveMembers(false, false) as $wrapper) {
69
70 1
            $this->shipRepository->save($wrapper->get());
71
        }
72
    }
73
}
74