Passed
Push — master ( 30d3d2...4e3abd )
by Jesse
01:56
created

TurnSwitcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\EventHandler;
4
5
use function assert;
6
use Stratadox\CardGame\DomainEvent;
7
use Stratadox\CardGame\Match\Event\NextTurnBegan;
8
use Stratadox\CardGame\ReadModel\Match\OngoingMatch;
9
use Stratadox\CardGame\ReadModel\Match\OngoingMatches;
10
11
final class TurnSwitcher implements EventHandler
12
{
13
    /** @var OngoingMatches */
14
    private $ongoingMatches;
15
16
    public function __construct(OngoingMatches $ongoingMatches)
17
    {
18
        $this->ongoingMatches = $ongoingMatches;
19
    }
20
21
    public function events(): iterable
22
    {
23
        return [NextTurnBegan::class];
24
    }
25
26
    public function handle(DomainEvent $nextTurn): void
27
    {
28
        assert($nextTurn instanceof NextTurnBegan);
29
30
        $this->becameTheTurnOf(
31
            $nextTurn->player(),
32
            $this->ongoingMatches->withId($nextTurn->match())
33
        );
34
    }
35
36
    private function becameTheTurnOf(
37
        int $theNextPlayer,
38
        OngoingMatch $inTheMatch
39
    ): void {
40
        $inTheMatch->beganTheTurnOf($theNextPlayer);
41
    }
42
}
43