1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\EventHandler; |
4
|
|
|
|
5
|
|
|
use Stratadox\CardGame\Account\TriedOpeningAccountForUnknownEntity; |
6
|
|
|
use Stratadox\CardGame\DomainEvent; |
7
|
|
|
use Stratadox\CardGame\Match\Event\PlayerDidNotHaveTheMana; |
8
|
|
|
use Stratadox\CardGame\Match\Event\TriedPlayingCardOutOfTurn; |
9
|
|
|
use Stratadox\CardGame\Match\Event\TriedStartingMatchForPendingProposal; |
10
|
|
|
use Stratadox\CardGame\Proposal\TriedAcceptingExpiredProposal; |
11
|
|
|
use Stratadox\CardGame\Proposal\TriedAcceptingUnknownProposal; |
12
|
|
|
use Stratadox\CardGame\ReadModel\Refusals; |
13
|
|
|
|
14
|
|
|
final class BringerOfBadNews implements EventHandler |
15
|
|
|
{ |
16
|
|
|
private $refusals; |
17
|
|
|
|
18
|
|
|
public function __construct(Refusals $refusals) |
19
|
|
|
{ |
20
|
|
|
$this->refusals = $refusals; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function handle(DomainEvent $event): void |
24
|
|
|
{ |
25
|
|
|
if ($event instanceof TriedOpeningAccountForUnknownEntity) { |
26
|
|
|
$this->refusals->addFor( |
27
|
|
|
$event->aggregateId(), |
28
|
|
|
'Cannot open account for unknown entity' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
if ($event instanceof TriedStartingMatchForPendingProposal) { |
32
|
|
|
$this->refusals->addFor( |
33
|
|
|
$event->aggregateId(), |
34
|
|
|
'The proposal is still pending!' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
if ($event instanceof TriedAcceptingExpiredProposal) { |
38
|
|
|
$this->refusals->addFor( |
39
|
|
|
$event->aggregateId(), |
40
|
|
|
'The proposal has already expired!' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
if ($event instanceof TriedAcceptingUnknownProposal) { |
44
|
|
|
$this->refusals->addFor( |
45
|
|
|
$event->aggregateId(), |
46
|
|
|
'Proposal not found' |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
if ($event instanceof PlayerDidNotHaveTheMana) { |
50
|
|
|
$this->refusals->addFor($event->aggregateId(), 'Not enough mana!'); |
51
|
|
|
} |
52
|
|
|
if ($event instanceof TriedPlayingCardOutOfTurn) { |
53
|
|
|
$this->refusals->addFor($event->aggregateId(), 'Cannot play cards right now'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|