CombatProcess   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A handle() 0 8 1
A timeForCombat() 0 16 2
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\EndBlocking;
10
use Stratadox\CardGame\CommandHandler;
11
use Stratadox\CardGame\Match\Event\TriedStartingCombatOutOfTurn;
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 CombatProcess implements CommandHandler
18
{
19
    /** @var Matches */
20
    private $matches;
21
    /** @var Clock */
22
    private $clock;
23
    /** @var EventBag */
24
    private $eventBag;
25
26
    public function __construct(
27
        Matches $matches,
28
        Clock $clock,
29
        EventBag $eventBag
30
    ) {
31
        $this->matches = $matches;
32
        $this->clock = $clock;
33
        $this->eventBag = $eventBag;
34
    }
35
36
    public function handle(Command $command): void
37
    {
38
        assert($command instanceof EndBlocking);
39
40
        $this->timeForCombat(
41
            $command->player(),
42
            $this->matches->withId($command->match()),
43
            $command->correlationId()
44
        );
45
    }
46
47
    private function timeForCombat(
48
        int $defender,
49
        Match $match,
50
        CorrelationId $correlationId
51
    ): void {
52
        try {
53
            $match->letTheCombatBegin($defender, $this->clock->now());
54
        } catch (NotYourTurn $exception) {
55
            $this->eventBag->add(new TriedStartingCombatOutOfTurn(
56
                $correlationId,
57
                $exception->getMessage()
58
            ));
59
            return;
60
        }
61
62
        $this->eventBag->takeFrom($match);
63
    }
64
}
65