Completed
Push — rover-kata ( c1a929...5eb0d1 )
by Marcos
02:12
created

Map::isObstacle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Kata\Rover;
4
5
final class Map
6
{
7
    private $max_coordinates;
8
    private $obstacles;
9
10 11
    public function __construct(Coordinates $max_coordinates, array $some_obstacles)
11
    {
12 11
        $this->max_coordinates = $max_coordinates;
13 11
        $this->obstacles = $this->normalize($some_obstacles);
14 11
    }
15
16 2
    public function obstacles(): array
17
    {
18 2
        return $this->obstacles;
19
    }
20
21 11
    private function normalize(array $some_obstacles): array
22
    {
23
        \array_walk($some_obstacles, function (&$current_coordinate) {
24 11
            $current_coordinate = $this->normalizeCoordinate($current_coordinate);
25 11
        });
26 11
        return $some_obstacles;
27
    }
28
29 11
    private function normalizeCoordinate(Coordinates $a_coordinate): Coordinates
30
    {
31 11
        if ($a_coordinate->coordinateX() > $this->max_coordinates->coordinateX()) {
32 1
            return $this->normalizeCoordinate(
33 1
                new Coordinates(
34 1
                    $a_coordinate->coordinateX() - $this->max_coordinates->coordinateX(),
35 1
                    $a_coordinate->coordinateY()
36
                )
37
            );
38
        }
39 11
        if ($a_coordinate->coordinateY() > $this->max_coordinates->coordinateY()) {
40 1
            return $this->normalizeCoordinate(
41 1
                new Coordinates(
42 1
                    $a_coordinate->coordinateX(),
43 1
                    $a_coordinate->coordinateY() - $this->max_coordinates->coordinateY()
44
                )
45
            );
46
        }
47
48 11
        return $a_coordinate;
49
    }
50
51 6
    public function isObstacle(Coordinates $a_coordinate): bool
52
    {
53 6
        foreach ($this->obstacles as $current_obstacle) {
54 6
            if ($a_coordinate->equals($current_obstacle)) {
55 6
                return true;
56
            }
57
        }
58
59 6
        return false;
60
    }
61
}
62