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

AttackingProcess::handle()   A

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\EventBag;
7
use Stratadox\CardGame\Match\Command\AttackWithCard;
8
use Stratadox\CardGame\Match\Match;
9
use Stratadox\CardGame\Match\Matches;
10
use Stratadox\CardGame\Match\NoSuchCard;
11
use Stratadox\Clock\Clock;
12
use Stratadox\CommandHandling\Handler;
13
14
final class AttackingProcess implements Handler
15
{
16
    private $matches;
17
    private $clock;
18
    private $eventBag;
19
20
    public function __construct(Matches $matches, Clock $clock, EventBag $eventBag)
21
    {
22
        $this->matches = $matches;
23
        $this->clock = $clock;
24
        $this->eventBag = $eventBag;
25
    }
26
27
    public function handle(object $command): void
28
    {
29
        assert($command instanceof AttackWithCard);
30
31
        $this->sendIntoBattle(
32
            $this->matches->withId($command->match()),
33
            $command->player(),
34
            $command->cardNumber()
35
        );
36
    }
37
38
    public function sendIntoBattle(
39
        Match $theMatch,
40
        int $player,
41
        int $cardNumber
42
    ): void {
43
        try {
44
            $theMatch->attackWithCard($cardNumber, $player, $this->clock->now());
45
        } catch (NoSuchCard $noSuchCard) {
46
            //@todo this happened: tried to attack with unknown card
47
            return;
48
        }
49
        $this->eventBag->takeFrom($theMatch);
50
    }
51
}
52