EndPlayPhaseProcess::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match\Handler;
4
5
use function assert;
6
use Stratadox\CardGame\Command;
7
use Stratadox\CardGame\CorrelationId;
8
use Stratadox\CardGame\EventBag;
9
use Stratadox\CardGame\Match\Command\EndCardPlaying;
10
use Stratadox\CardGame\CommandHandler;
11
use Stratadox\CardGame\Match\Event\TriedEndingPlayPhaseOutOfTurn;
12
use Stratadox\CardGame\Match\Match;
13
use Stratadox\CardGame\Match\Matches;
14
use Stratadox\CardGame\Match\NotYourTurn;
15
use Stratadox\Clock\Clock;
16
17
final class EndPlayPhaseProcess implements CommandHandler
18
{
19
    private $matches;
20
    private $eventBag;
21
    private $clock;
22
23
    public function __construct(
24
        Matches $matches,
25
        Clock $clock,
26
        EventBag $eventBag
27
    ) {
28
        $this->matches = $matches;
29
        $this->eventBag = $eventBag;
30
        $this->clock = $clock;
31
    }
32
33
    public function handle(Command $command): void
34
    {
35
        assert($command instanceof EndCardPlaying);
36
37
        $this->endPlayPhase(
38
            $command->player(),
39
            $this->matches->withId($command->match()),
40
            $command->correlationId()
41
        );
42
    }
43
44
    private function endPlayPhase(
45
        int $playerNumber,
46
        Match $match,
47
        CorrelationId $correlationId
48
    ): void {
49
        try {
50
            $match->endCardPlayingPhaseFor($playerNumber, $this->clock->now());
51
        } catch (NotYourTurn $refusal) {
52
            $this->eventBag->add(new TriedEndingPlayPhaseOutOfTurn(
53
                $correlationId,
54
                $refusal->getMessage()
55
            ));
56
            return;
57
        }
58
59
        $this->eventBag->takeFrom($match);
60
    }
61
}
62