Completed
Push — rover-kata ( 5671db...2c2792 )
by Marcos
14:05
created

Map::obstacles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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