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

AttackingProcess   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 8 1
A sendIntoBattle() 0 12 2
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