Completed
Push — master ( 4b1e3c...30d3d2 )
by Jesse
02:57
created

TurnEndingProcess   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 1
A endTurn() 0 4 1
A __construct() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match\Handler;
4
5
use function assert;
6
use Stratadox\CardGame\EventBag;
7
use Stratadox\CardGame\Match\Command\EndTheTurn;
8
use Stratadox\CardGame\Match\Match;
9
use Stratadox\CardGame\Match\Matches;
10
use Stratadox\Clock\Clock;
11
use Stratadox\CommandHandling\Handler;
12
13
final class TurnEndingProcess implements Handler
14
{
15
    /** @var Matches */
16
    private $matches;
17
    /** @var Clock */
18
    private $clock;
19
    /** @var EventBag */
20
    private $eventBag;
21
22
    public function __construct(Matches $matches, Clock $clock, EventBag $eventBag)
23
    {
24
        $this->matches = $matches;
25
        $this->clock = $clock;
26
        $this->eventBag = $eventBag;
27
    }
28
29
    public function handle(object $command): void
30
    {
31
        assert($command instanceof EndTheTurn);
32
33
        $this->endTurn(
34
            $this->matches->withId($command->match()),
35
            $command->player()
36
        );
37
    }
38
39
    private function endTurn(Match $theMatch, int $player): void
40
    {
41
        $theMatch->endTurnOf($player, $this->clock->now());
42
        $this->eventBag->takeFrom($theMatch);
43
    }
44
}
45