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

Battlefield   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cardsInPlay() 0 3 1
A attackers() 0 6 1
A cardsInPlayFor() 0 3 1
A remove() 0 6 1
A add() 0 3 1
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