Passed
Push — dev ( 59981f...f2247e )
by Janko
47:08
created

RoundBasedBattleParty::use()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Collection;
6
use RuntimeException;
7
use Stu\Module\Ship\Lib\ShipWrapperInterface;
8
use Stu\Orm\Repository\ShipRepositoryInterface;
9
10
class RoundBasedBattleParty
11
{
12
    /** @var array<int> */
13
    private array $unUsedIds;
14
15 7
    public function __construct(
16
        private BattlePartyInterface $battleParty,
17
        private ShipRepositoryInterface $shipRepository
18
    ) {
19 7
        $this->unUsedIds = $battleParty->getActiveMembers()->getKeys();
20
    }
21
22 5
    public function get(): BattlePartyInterface
23
    {
24 5
        return $this->battleParty;
25
    }
26
27 2
    public function use(int $spacecraftId): void
28
    {
29 2
        unset($this->unUsedIds[$spacecraftId]);
30
    }
31
32 4
    public function isDone(): bool
33
    {
34 4
        return $this->isUsed() || $this->getAllUnusedThatCanFire()->isEmpty();
35
    }
36
37 4
    private function isUsed(): bool
38
    {
39 4
        return empty($this->unUsedIds);
40
    }
41
42
    /** @return Collection<int, ShipWrapperInterface> */
43 3
    public function getAllUnusedThatCanFire(): Collection
44
    {
45 3
        return $this->get()
46 3
            ->getActiveMembers(true)
47 3
            ->filter(fn (ShipWrapperInterface $wrapper) => in_array($wrapper->get()->getId(), $this->unUsedIds));
48
    }
49
50 1
    public function getRandomUnused(): ShipWrapperInterface
51
    {
52 1
        $allUnusedThatCanFire = $this->getAllUnusedThatCanFire();
53
54
        /** @var ShipWrapperInterface|null */
55 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

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