HandAdjuster::events()   A
last analyzed

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\CardWasDrawn;
7
use Stratadox\CardGame\Match\Event\SpellVanishedToTheVoid;
8
use Stratadox\CardGame\Match\Event\UnitMovedIntoPlay;
9
use Stratadox\CardGame\ReadModel\Match\Card;
10
use Stratadox\CardGame\ReadModel\Match\CardTemplates;
11
use Stratadox\CardGame\ReadModel\Match\CardsInHand;
12
13
final class HandAdjuster implements EventHandler
14
{
15
    private $cardsInHand;
16
    private $cardTemplates;
17
18
    public function __construct(
19
        CardsInHand $cardsInHand,
20
        CardTemplates $cardTemplates
21
    ) {
22
        $this->cardsInHand = $cardsInHand;
23
        $this->cardTemplates = $cardTemplates;
24
    }
25
26
    public function events(): iterable
27
    {
28
        return [
29
            UnitMovedIntoPlay::class,
30
            SpellVanishedToTheVoid::class,
31
            CardWasDrawn::class,
32
        ];
33
    }
34
35
    public function handle(DomainEvent $event): void
36
    {
37
        if (
38
            $event instanceof UnitMovedIntoPlay ||
39
            $event instanceof SpellVanishedToTheVoid
40
        ) {
41
            $this->cardsInHand->played(
42
                $event->offset(),
43
                $event->match(),
44
                $event->player()
45
            );
46
        } else if ($event instanceof CardWasDrawn) {
47
            $this->cardsInHand->draw(
48
                $event->match(),
49
                $event->player(),
50
                new Card(
51
                    $event->offset(),
52
                    $this->cardTemplates->ofType($event->card())
53
                )
54
            );
55
        }
56
    }
57
}
58