Passed
Push — master ( 30d3d2...4e3abd )
by Jesse
01:56
created

BattlefieldUpdater::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\EventHandler;
4
5
use Stratadox\CardGame\DomainEvent;
6
use Stratadox\CardGame\Match\Event\UnitDied;
7
use Stratadox\CardGame\Match\Event\UnitMovedIntoPlay;
8
use Stratadox\CardGame\Match\Event\UnitMovedToAttack;
9
use Stratadox\CardGame\ReadModel\Match\AllCards;
10
use Stratadox\CardGame\ReadModel\Match\Battlefield;
11
12
final class BattlefieldUpdater implements EventHandler
13
{
14
    private $battlefield;
15
    private $cards;
16
17
    public function __construct(Battlefield $battlefield, AllCards $allCards)
18
    {
19
        $this->battlefield = $battlefield;
20
        $this->cards = $allCards;
21
    }
22
23
    public function events(): iterable
24
    {
25
        return [
26
            UnitMovedIntoPlay::class,
27
            UnitMovedToAttack::class,
28
            UnitDied::class
29
        ];
30
    }
31
32
    public function handle(DomainEvent $event): void
33
    {
34
        if ($event instanceof UnitMovedIntoPlay) {
35
            $this->battlefield->add(
36
                $this->cards->ofType($event->card()),
37
                $event->aggregateId()
38
            );
39
        }
40
        if ($event instanceof UnitMovedToAttack) {
41
            // @todo wont work with doubles..
42
            $this->cards->ofType($event->card())->attack();
43
        }
44
        if ($event instanceof UnitDied) {
45
            $this->battlefield->remove(
46
                $this->cards->ofType($event->card()),
47
                $event->match()
48
            );
49
        }
50
    }
51
}
52