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
|
|
|
|