TurnSwitcher::events()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\EventHandler;
4
5
use function assert;
6
use Stratadox\CardGame\DomainEvent;
7
use Stratadox\CardGame\Match\Event\AttackPhaseStarted;
8
use Stratadox\CardGame\Match\Event\DefendPhaseStarted;
9
use Stratadox\CardGame\Match\Event\NextTurnStarted;
10
use Stratadox\CardGame\Match\Event\PlayPhaseStarted;
11
use Stratadox\CardGame\Match\MatchEvent;
12
use Stratadox\CardGame\ReadModel\Match\OngoingMatches;
13
14
final class TurnSwitcher implements EventHandler
15
{
16
    /** @var OngoingMatches */
17
    private $ongoingMatches;
18
19
    public function __construct(OngoingMatches $ongoingMatches)
20
    {
21
        $this->ongoingMatches = $ongoingMatches;
22
    }
23
24
    public function events(): iterable
25
    {
26
        return [
27
            NextTurnStarted::class,
28
            DefendPhaseStarted::class,
29
            PlayPhaseStarted::class,
30
            AttackPhaseStarted::class,
31
        ];
32
    }
33
34
    public function handle(DomainEvent $turn): void
35
    {
36
        assert($turn instanceof MatchEvent);
37
        $match = $this->ongoingMatches->withId($turn->aggregateId());
38
39
        if ($turn instanceof NextTurnStarted) {
40
            $match->startTurnOf($turn->player());
41
        } elseif ($turn instanceof DefendPhaseStarted) {
42
            $match->startDefendPhase();
43
        } elseif ($turn instanceof PlayPhaseStarted) {
44
            $match->startPlayPhase();
45
        } elseif ($turn instanceof AttackPhaseStarted) {
46
            $match->startAttackPhase();
47
        }
48
    }
49
}
50