MatchStartingProcess::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match\Handler;
4
5
use Stratadox\CardGame\Command;
6
use Stratadox\CardGame\EventBag;
7
use Stratadox\CardGame\CommandHandler;
8
use Stratadox\CardGame\Match\DeckForAccount;
9
use Stratadox\CardGame\Match\Decks;
10
use Stratadox\CardGame\Match\Event\TriedStartingMatchWithoutProposal;
11
use Stratadox\CardGame\Match\Matches;
12
use Stratadox\CardGame\Match\MatchIdGenerator;
13
use Stratadox\CardGame\Match\Command\StartTheMatch;
14
use Stratadox\CardGame\Match\Event\TriedStartingMatchForPendingProposal;
15
use Stratadox\CardGame\Proposal\ProposalHasNotBeenAccepted;
16
use Stratadox\CardGame\Proposal\ProposedMatches;
17
use Stratadox\Clock\Clock;
18
use function assert;
19
20
final class MatchStartingProcess implements CommandHandler
21
{
22
    private $proposals;
23
    private $newMatchId;
24
    private $matches;
25
    private $decks;
26
    private $clock;
27
    private $eventBag;
28
29
    public function __construct(
30
        ProposedMatches $proposals,
31
        MatchIdGenerator $newMatchId,
32
        Matches $matches,
33
        DeckForAccount $deckForAccount,
34
        Clock $clock,
35
        EventBag $eventBag
36
    ) {
37
        $this->proposals = $proposals;
38
        $this->newMatchId = $newMatchId;
39
        $this->matches = $matches;
40
        $this->decks = $deckForAccount;
41
        $this->clock = $clock;
42
        $this->eventBag = $eventBag;
43
    }
44
45
    public function handle(Command $command): void
46
    {
47
        assert($command instanceof StartTheMatch);
48
49
        $proposal = $this->proposals->withId($command->proposal());
50
        if ($proposal === null) {
51
            $this->eventBag->add(
52
                new TriedStartingMatchWithoutProposal(
53
                    $command->correlationId(),
54
                    'Proposal not found'
55
                )
56
            );
57
            return;
58
        }
59
60
        try {
61
            $match = $proposal->start(
62
                $this->newMatchId->generate(),
63
                new Decks(
64
                    $this->decks->deckFor($proposal->proposedBy()),
65
                    $this->decks->deckFor($proposal->proposedTo())
66
                ),
67
                $this->clock->now()
68
            );
69
        } catch (ProposalHasNotBeenAccepted $cannotStartYet) {
70
            $this->eventBag->add(
71
                new TriedStartingMatchForPendingProposal(
72
                    $command->correlationId(),
73
                    $cannotStartYet->getMessage()
74
                )
75
            );
76
            return;
77
        }
78
79
        $match->drawOpeningHands();
80
81
        $this->matches->add($match);
82
        $this->eventBag->takeFrom($match);
83
    }
84
}
85