Completed
Push — master ( 144ff2...6ebbcb )
by Jesse
02:12
created

Turn::prohibitsAttacking()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CardGame\Match;
4
5
use DateTimeInterface;
6
7
final class Turn
8
{
9
    private $currentPlayer;
10
    private $since;
11
    private $canPlay;
12
    private $canDefend;
13
14
    public function __construct(int $player, DateTimeInterface $since, bool $play = true)
15
    {
16
        $this->currentPlayer = $player;
17
        $this->since = $since;
18
        $this->canPlay = $play;
19
        $this->canDefend = !$play;
20
    }
21
22
    public function prohibitsPlaying(int $player, DateTimeInterface $when): bool
23
    {
24
        return $this->currentPlayer !== $player ||
25
            !$this->canPlay ||
26
            $when->getTimestamp() - $this->since->getTimestamp() >= 20;
27
    }
28
29
    public function prohibitsAttacking(int $player, DateTimeInterface $when): bool
30
    {
31
        return $this->currentPlayer !== $player ||
32
            $this->canPlay ||
33
            $when->getTimestamp() - $this->since->getTimestamp() >= 10;
34
    }
35
36
    public function prohibitsDefending(int $player, DateTimeInterface $when): bool
37
    {
38
        // @todo check if turn phase allows for starting combat
39
        return $this->currentPlayer !== $player ||
40
            $when->getTimestamp() - $this->since->getTimestamp() >= 20;
41
    }
42
43
    public function prohibitsStartingCombat(int $player, DateTimeInterface $when): bool
44
    {
45
        return $this->currentPlayer !== $player ||
46
            $when->getTimestamp() - $this->since->getTimestamp() >= 20;
47
    }
48
49
    /** @throws NotYourTurn */
50
    public function endCardPlayingPhaseFor(int $player, DateTimeInterface $when): Turn
0 ignored issues
show
Unused Code introduced by
The parameter $when is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public function endCardPlayingPhaseFor(int $player, /** @scrutinizer ignore-unused */ DateTimeInterface $when): Turn

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        // @todo check if time ran out
53
        if ($this->currentPlayer !== $player) {
54
            throw NotYourTurn::cannotEndCardPlayingPhase();
55
        }
56
        // @todo make immutable
57
        $this->canPlay = false;
58
        return $this;
59
    }
60
61
    public function endCombatPhase(): Turn
62
    {
63
        // @todo add time
64
        // @todo make immutable
65
        $this->canDefend = false;
66
        $this->canPlay = true;
67
        return $this;
68
    }
69
70
    /** @throws NotYourTurn */
71
    public function beginTheTurnOf(int $player, DateTimeInterface $since, int $previousPlayer): Turn
72
    {
73
        if (
74
            $this->currentPlayer !== $previousPlayer ||
75
            $since->getTimestamp() - $this->since->getTimestamp() >= 10
76
        ) {
77
            throw NotYourTurn::cannotEndTurn();
78
        }
79
        return new Turn($player, $since, false);
80
    }
81
}
82