Test Setup Failed
Push — master ( 647a95...23a558 )
by Jesse
02:13
created

Battlefield::regroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\ReadModel\Match;
4
5
use function array_filter;
6
use function array_merge;
7
use function current;
8
use Stratadox\CardGame\Match\MatchId;
9
10
class Battlefield
11
{
12
    private $cards = [];
13
14
    public function add(Card $card, MatchId $match, int $owner): void
15
    {
16
        $this->cards[$match->id()][$owner][] = $card;
17
    }
18
19
    public function remove(int $cardToRemove, MatchId $match, int $owner): void
20
    {
21
        $this->cards[$match->id()][$owner] = array_filter(
22
            $this->cards[$match->id()][$owner],
23
            static function (Card $card) use ($cardToRemove): bool {
24
                return $card->offset() !== $cardToRemove;
25
            }
26
        );
27
    }
28
29
    /** @return Card[] */
30
    public function cardsInPlay(MatchId $match): array
31
    {
32
        return array_merge(...$this->cards[$match->id()] ?? [[]]);
33
    }
34
35
    /** @return Card[] */
36
    public function cardsInPlayFor(int $player, MatchId $match): array
37
    {
38
        return $this->cards[$match->id()][$player] ?? [];
39
    }
40
41
    public function sendIntoBattle(
42
        int $offset,
43
        MatchId $match,
44
        int $player
45
    ): void {
46
        $this->card($offset, $match, $player)->attack();
47
    }
48
49
    public function regroup(
50
        int $offset,
51
        MatchId $match,
52
        int $player
53
    ): void {
54
        $this->card($offset, $match, $player)->regroup();
55
    }
56
57
    /** @return Card[] */
58
    public function attackers(MatchId $match): array
59
    {
60
        return array_filter(
61
            $this->cardsInPlay($match),
62
            static function (Card $card): bool {
63
                return $card->isAttacking();
64
            }
65
        );
66
    }
67
68
    private function card(int $offset, MatchId $match, int $player): Card
69
    {
70
        return current(array_filter(
71
            $this->cardsInPlayFor($player, $match),
72
            static function (Card $card) use ($offset): bool {
73
                return $card->offset() === $offset;
74
            }
75
        ));
76
    }
77
}
78