Completed
Push — master ( ecb243...bb1b22 )
by Pablo
03:58
created

Polygon::guard()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 6
nc 7
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpValueObjects\Spatial;
6
7
use PhpValueObjects\Spatial\Exception\InvalidPolygonException;
8
9
class Polygon
10
{
11
    private $value;
12 21
    public function __construct(array $value)
13
    {
14 21
        $this->guard($value);
15 6
        $this->value = $value;
16 6
    }
17
18 6
    public function value(): array
19
    {
20 6
        return $this->value;
21
    }
22
23 21
    protected function guard($value): void
24
    {
25 21
        if (3 > count($value)) {
26 6
            throw new InvalidPolygonException();
27
        }
28
29 15
        $first = $value[0];
30 15
        $end = end($value);
31
32 15
        foreach ($value as $point) {
33 15
            if (false === is_array($point)) {
34 3
                throw new InvalidPolygonException();
35
            }
36
37 15
            if (2 !== count($point)) {
38 15
                throw new InvalidPolygonException();
39
            }
40
        }
41
42 9
        if (false === ($first == $end)) {
43 3
            throw new InvalidPolygonException();
44
        }
45 6
    }
46
}
47