1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Proposal; |
4
|
|
|
|
5
|
|
|
use function assert; |
6
|
|
|
use DateInterval; |
7
|
|
|
use Stratadox\CardGame\Command; |
8
|
|
|
use Stratadox\CardGame\EventBag; |
9
|
|
|
use Stratadox\CardGame\Account\PlayerAccount; |
10
|
|
|
use Stratadox\CardGame\Account\PlayerBase; |
11
|
|
|
use Stratadox\CardGame\Account\AccountId; |
12
|
|
|
use Stratadox\CardGame\CommandHandler; |
13
|
|
|
use Stratadox\Clock\Clock; |
14
|
|
|
use Stratadox\Clock\RewindableClock; |
15
|
|
|
|
16
|
|
|
final class MatchPropositionProcess implements CommandHandler |
17
|
|
|
{ |
18
|
|
|
private $newIdentity; |
19
|
|
|
private $clock; |
20
|
|
|
private $proposals; |
21
|
|
|
private $players; |
22
|
|
|
private $eventBag; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
ProposalIdGenerator $identityGenerator, |
26
|
|
|
RewindableClock $clock, |
27
|
|
|
ProposedMatches $proposals, |
28
|
|
|
PlayerBase $players, |
29
|
|
|
EventBag $eventBag |
30
|
|
|
) { |
31
|
|
|
$this->newIdentity = $identityGenerator; |
32
|
|
|
$this->clock = $clock; |
33
|
|
|
$this->proposals = $proposals; |
34
|
|
|
$this->players = $players; |
35
|
|
|
$this->eventBag = $eventBag; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function handle(Command $proposition): void |
39
|
|
|
{ |
40
|
|
|
assert($proposition instanceof ProposeMatch); |
41
|
|
|
|
42
|
|
|
$proposal = $this->proposeMatch( |
43
|
|
|
$this->players->withId($proposition->proposedBy()), |
44
|
|
|
$proposition->proposedTo(), |
45
|
|
|
$this->clock->fastForward(new DateInterval('PT30S')), |
46
|
|
|
$this->newIdentity->generate() |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->proposals->add($proposal); |
50
|
|
|
|
51
|
|
|
$this->eventBag->takeFrom($proposal); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function proposeMatch( |
55
|
|
|
PlayerAccount $proposingPlayer, |
56
|
|
|
AccountId $otherPlayer, |
57
|
|
|
Clock $validUntil, |
58
|
|
|
ProposalId $id |
59
|
|
|
): MatchProposal { |
60
|
|
|
return $proposingPlayer->proposeMatchTo( |
61
|
|
|
$otherPlayer, |
62
|
|
|
$validUntil->now(), |
63
|
|
|
$id |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|