Completed
Push — rover-kata ( e33e35...c1a929 )
by Marcos
02:43
created

Direction::inverseDirection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.4555
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 5.025
1
<?php
2
3
namespace Kata\Rover;
4
5
final class Direction
6
{
7
    public const NORTH = 'n';
8
    public const SOUTH = 's';
9
    public const EAST = 'e';
10
    public const WEST = 'w';
11
12
    public const DIRECTIONS = [
13
        self::NORTH => [
14
            'right' => self::EAST,
15
            'left' => self::WEST
16
        ],
17
        self::EAST => [
18
            'right' => self::SOUTH,
19
            'left' => self::NORTH
20
        ],
21
        self::SOUTH => [
22
            'right' => self::WEST,
23
            'left' => self::EAST
24
        ],
25
        self::WEST => [
26
            'right' => self::NORTH,
27
            'left' => self::SOUTH
28
        ]
29
    ];
30
31
    private $direction;
32
33 21
    private function __construct(string $direction)
34
    {
35 21
        $this->direction = $direction;
36 21
    }
37
38 21
    public static function north(): Direction
39
    {
40 21
        return new self(self::NORTH);
41
    }
42
43 8
    public static function south(): Direction
44
    {
45 8
        return new self(self::SOUTH);
46
    }
47
48 7
    public static function east(): Direction
49
    {
50 7
        return new self(self::EAST);
51
    }
52
53 6
    public static function west(): Direction
54
    {
55 6
        return new self(self::WEST);
56
    }
57
58 11
    public function turnRight(): Direction
59
    {
60 11
        return new Direction(self::DIRECTIONS[$this->direction]['right']);
61
    }
62
63 7
    public function turnLeft(): Direction
64
    {
65 7
        return new Direction(self::DIRECTIONS[$this->direction]['left']);
66
    }
67
68 10
    public function direction(): string
69
    {
70 10
        return $this->direction;
71
    }
72
73 6
    public function inverseDirection(): Direction
74
    {
75 6
        if ($this->equals(self::north())) {
76 3
            return self::south();
77
        }
78 3
        if ($this->equals(self::east())) {
79 1
            return self::west();
80
        }
81 2
        if ($this->equals(self::south())) {
82 1
            return self::north();
83
        }
84 1
        if ($this->equals(self::west())) {
85 1
            return self::east();
86
        }
87
    }
88
89 6
    private function equals(Direction $a_direction): bool
90
    {
91 6
        if ($this->direction === $a_direction->direction()) {
92 6
            return true;
93
        }
94
95 3
        return false;
96
    }
97
}
98