Polygon::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
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