Passed
Push — master ( de9c67...a3e5b5 )
by Petr
06:02
created

Map::getPreviousLocations()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 5.3846
cc 8
eloc 18
nc 1
nop 2
crap 8
1
<?php
2
3
namespace Vehsamrak\Terraformator\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Vehsamrak\Terraformator\Exception\InvalidTypeException;
7
8
/**
9
 * @author Vehsamrak
10
 */
11
class Map extends ArrayCollection
12
{
13
14
    /**
15
     * {@inheritDoc}
16
     */
17 16
    public function __construct(array $locations = [])
18
    {
19 16
        parent::__construct($locations);
20 16
    }
21
22
    /** {@inheritDoc} */
23 12
    public function add($element): bool
24
    {
25 12
        if (!$element instanceof Location) {
26 1
            throw new InvalidTypeException();
27
        }
28
29 11
        return parent::add($element);
30
    }
31
32
    /** {@inheritDoc} */
33 9
    public function first(): ?Location
34
    {
35 9
        $firstLocation = parent::first();
36
37 9
        if (!$firstLocation) {
38 4
            return null;
39
        }
40
41 7
        return $firstLocation;
42
    }
43
44 2
    public function getPreviousLocations(int $x, int $y): PreviousLocationMap
45
    {
46
        // filtering north, north-west, north-east and west locations by X and Y
47 2
        $previousLocations = $this->filter(
48 2
            function (Location $location) use ($x, $y) {
49 2
                $westLocationX = $x - 1;
50 2
                $westLocationY = $y;
51 2
                $northWestLocationX = $x - 1;
52 2
                $northWestLocationY = $y - 1;
53 2
                $northLocationX = $x;
54 2
                $northLocationY = $y - 1;
55 2
                $northEastLocationX = $x + 1;
56 2
                $northEastLocationY = $y - 1;
57
58 2
                $locationX = $location->getX();
59 2
                $locationY = $location->getY();
60
61 2
                return ($locationX === $westLocationX && $locationY === $westLocationY) ||
62 2
                    ($locationX === $northWestLocationX && $locationY === $northWestLocationY) ||
63 2
                    ($locationX === $northLocationX && $locationY === $northLocationY) ||
64 2
                    ($locationX === $northEastLocationX && $locationY === $northEastLocationY);
65
66 2
            }
67
        );
68
69 2
        return new PreviousLocationMap($previousLocations->getValues());
70
    }
71
}
72
73