1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Match\Handler; |
4
|
|
|
|
5
|
|
|
use Stratadox\CardGame\EventBag; |
6
|
|
|
use Stratadox\CardGame\Match\DeckForAccount; |
7
|
|
|
use Stratadox\CardGame\Match\Decks; |
8
|
|
|
use Stratadox\CardGame\Match\Matches; |
9
|
|
|
use Stratadox\CardGame\Match\MatchIdGenerator; |
10
|
|
|
use Stratadox\CardGame\Match\Command\StartTheMatch; |
11
|
|
|
use Stratadox\CardGame\Match\Event\TriedStartingMatchForPendingProposal; |
12
|
|
|
use Stratadox\CardGame\Proposal\ProposalHasNotBeenAccepted; |
13
|
|
|
use Stratadox\CardGame\Proposal\ProposedMatches; |
14
|
|
|
use Stratadox\Clock\Clock; |
15
|
|
|
use Stratadox\CommandHandling\Handler; |
16
|
|
|
use function assert; |
17
|
|
|
|
18
|
|
|
final class MatchStartingProcess implements Handler |
19
|
|
|
{ |
20
|
|
|
private $proposals; |
21
|
|
|
private $newMatchId; |
22
|
|
|
private $matches; |
23
|
|
|
private $decks; |
24
|
|
|
private $clock; |
25
|
|
|
private $eventBag; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
ProposedMatches $proposals, |
29
|
|
|
MatchIdGenerator $newMatchId, |
30
|
|
|
Matches $matches, |
31
|
|
|
DeckForAccount $deckForAccount, |
32
|
|
|
Clock $clock, |
33
|
|
|
EventBag $eventBag |
34
|
|
|
) { |
35
|
|
|
$this->proposals = $proposals; |
36
|
|
|
$this->newMatchId = $newMatchId; |
37
|
|
|
$this->matches = $matches; |
38
|
|
|
$this->decks = $deckForAccount; |
39
|
|
|
$this->clock = $clock; |
40
|
|
|
$this->eventBag = $eventBag; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function handle(object $command): void |
44
|
|
|
{ |
45
|
|
|
assert($command instanceof StartTheMatch); |
46
|
|
|
|
47
|
|
|
$proposal = $this->proposals->withId($command->proposal()); |
48
|
|
|
assert($proposal !== null); // @todo error event instead? |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$match = $proposal->start( |
52
|
|
|
$this->newMatchId->generate(), |
53
|
|
|
new Decks( |
54
|
|
|
$this->decks->deckFor($proposal->proposedBy()), |
55
|
|
|
$this->decks->deckFor($proposal->proposedBy()) |
56
|
|
|
), |
57
|
|
|
$this->clock->now(), |
58
|
|
|
0, |
59
|
|
|
1 |
60
|
|
|
); |
61
|
|
|
} catch (ProposalHasNotBeenAccepted $cannotStartYet) { |
62
|
|
|
$this->eventBag->add( |
63
|
|
|
new TriedStartingMatchForPendingProposal($command->correlationId()) |
64
|
|
|
); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$match->drawOpeningHands(); |
69
|
|
|
|
70
|
|
|
$this->matches->add($match); |
71
|
|
|
$this->eventBag->takeFrom($match); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|