Completed
Push — master ( c23c95...90e58a )
by Jesse
02:01
created

Battlefield::cardsInPlay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 Stratadox\CardGame\Match\MatchId;
8
9
class Battlefield
10
{
11
    private $cards = [];
12
13
    public function add(Card $card, MatchId $match, int $owner): void
14
    {
15
        $this->cards[$match->id()][$owner][] = $card;
16
    }
17
18
    public function remove(Card $cardToRemove, MatchId $match, int $owner): void
19
    {
20
        $this->cards[$match->id()][$owner] = array_filter(
21
            $this->cards[$match->id()][$owner],
22
            function (Card $card) use ($cardToRemove): bool {
23
                return !$card->is($cardToRemove);
24
            }
25
        );
26
    }
27
28
    /** @return Card[] */
29
    public function cardsInPlay(MatchId $match): array
30
    {
31
        return array_merge(...$this->cards[$match->id()] ?? [[]]);
32
    }
33
34
    /** @return Card[] */
35
    public function cardsInPlayFor(int $player, MatchId $match): array
36
    {
37
        return $this->cards[$match->id()][$player] ?? [];
38
    }
39
40
    /** @return Card[] */
41
    public function attackers(MatchId $match): array
42
    {
43
        return array_filter(
44
            $this->cardsInPlay($match),
45
            function (Card $card): bool {
46
                return $card->isAttacking();
47
            }
48
        );
49
    }
50
}
51