1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Match\Handler; |
4
|
|
|
|
5
|
|
|
use function assert; |
6
|
|
|
use Stratadox\CardGame\CorrelationId; |
7
|
|
|
use Stratadox\CardGame\EventBag; |
8
|
|
|
use Stratadox\CardGame\Match\Match; |
9
|
|
|
use Stratadox\CardGame\Match\Matches; |
10
|
|
|
use Stratadox\CardGame\Match\NotEnoughMana; |
11
|
|
|
use Stratadox\CardGame\Match\NotYourTurn; |
12
|
|
|
use Stratadox\CardGame\Match\Event\PlayerDidNotHaveTheMana; |
13
|
|
|
use Stratadox\CardGame\Match\Command\PlayTheCard; |
14
|
|
|
use Stratadox\CardGame\Match\Event\TriedPlayingCardOutOfTurn; |
15
|
|
|
use Stratadox\Clock\Clock; |
16
|
|
|
use Stratadox\CommandHandling\Handler; |
17
|
|
|
|
18
|
|
|
final class CardPlayingProcess implements Handler |
19
|
|
|
{ |
20
|
|
|
private $matches; |
21
|
|
|
private $clock; |
22
|
|
|
private $eventBag; |
23
|
|
|
|
24
|
|
|
public function __construct(Matches $matches, Clock $clock, EventBag $eventBag) |
25
|
|
|
{ |
26
|
|
|
$this->matches = $matches; |
27
|
|
|
$this->clock = $clock; |
28
|
|
|
$this->eventBag = $eventBag; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function handle(object $command): void |
32
|
|
|
{ |
33
|
|
|
assert($command instanceof PlayTheCard); |
34
|
|
|
|
35
|
|
|
$this->play( |
36
|
|
|
$this->matches->withId($command->match()), |
37
|
|
|
$command->player(), |
38
|
|
|
$command->cardNumber(), |
39
|
|
|
$command->correlationId() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function play( |
44
|
|
|
Match $theMatch, |
45
|
|
|
int $player, |
46
|
|
|
int $cardNumber, |
47
|
|
|
CorrelationId $correlationId |
48
|
|
|
): void { |
49
|
|
|
try { |
50
|
|
|
$theMatch->playTheCard($cardNumber, $player, $this->clock->now()); |
51
|
|
|
} catch (NotEnoughMana $problem) { |
52
|
|
|
$this->eventBag->add(new PlayerDidNotHaveTheMana($correlationId)); |
53
|
|
|
} catch (NotYourTurn $problem) { |
54
|
|
|
$this->eventBag->add(new TriedPlayingCardOutOfTurn($correlationId)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->eventBag->takeFrom($theMatch); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|