Passed
Push — main ( 94723e...387a9f )
by Frank
03:26 queued 01:22
created

GeometryObjectFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 25.64%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 33
c 4
b 0
f 0
dl 0
loc 92
ccs 10
cts 39
cp 0.2564
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createForPolygonCoordinates() 0 13 3
A createForMultiPointCoordinates() 0 3 1
A createForPointCoordinates() 0 3 1
A createForMultiLineStringCoordinates() 0 8 2
A createForLineStringCoordinates() 0 8 2
A createForMultiPolygonCoordinates() 0 13 3
A createForGeoJsonFeatureGeometry() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Geometry\GeometryObject;
6
7
use PrinsFrank\PhpGeoSVG\Exception\InvalidPositionException;
8
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException;
9
use PrinsFrank\PhpGeoSVG\Geometry\Position\Position;
10
11
class GeometryObjectFactory
12
{
13
    /**
14
     * @throws NotImplementedException
15
     * @throws InvalidPositionException
16
     */
17 2
    public static function createForGeoJsonFeatureGeometry(array $featureGeometry): ?GeometryObject
18
    {
19 2
        return match ($featureGeometry['type']) {
20 1
            'LineString' => self::createForLineStringCoordinates($featureGeometry['coordinates']),
21 1
            'MultiLineString' => self::createForMultiLineStringCoordinates($featureGeometry['coordinates']),
22
            'MultiPoint' => self::createForMultiPointCoordinates($featureGeometry['coordinates']),
23 1
            'MultiPolygon' => self::createForMultiPolygonCoordinates($featureGeometry['coordinates']),
24 1
            'Point' => self::createForPointCoordinates($featureGeometry['coordinates']),
25 1
            'Polygon' => self::createForPolygonCoordinates($featureGeometry['coordinates']),
26 2
            default => throw new NotImplementedException('Feature geometries of type "' . $featureGeometry['type'] . '" are currently not supported')
27
        };
28
    }
29
30
    /**
31
     * @throws InvalidPositionException
32
     */
33
    public static function createForLineStringCoordinates(array $coordinates): LineString
34
    {
35
        $lineString = new LineString();
36
        foreach ($coordinates as $coordinate) {
37
            $lineString->addPosition(new Position($coordinate[0], $coordinate[1], $coordinate[2] ?? null));
38
        }
39
40
        return $lineString;
41
    }
42
43
    /**
44
     * @throws InvalidPositionException
45
     */
46
    public static function createForMultiLineStringCoordinates(array $coordinates): MultiLineString
47
    {
48
        $multiLineString = new MultiLineString();
49
        foreach ($coordinates as $lineStringCoordinates) {
50
            $multiLineString->addLineString(self::createForLineStringCoordinates($lineStringCoordinates));
51
        }
52
53
        return $multiLineString;
54
    }
55
56
    public static function createForMultiPointCoordinates(array $coordinates): MultiPoint
0 ignored issues
show
Unused Code introduced by
The parameter $coordinates is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
    public static function createForMultiPointCoordinates(/** @scrutinizer ignore-unused */ array $coordinates): MultiPoint

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        throw new NotImplementedException('Creating multiPoints is not supported yet');
59
    }
60
61
    /**
62
     * @throws InvalidPositionException
63
     */
64
    public static function createForMultiPolygonCoordinates(array $coordinates): MultiPolygon
65
    {
66
        $multiPolygon = new MultiPolygon();
67
        foreach ($coordinates as $polygonCoordinates) {
68
            $polygon = self::createForPolygonCoordinates($polygonCoordinates);
69
            if (null === $polygon) {
70
                continue;
71
            }
72
73
            $multiPolygon->addPolygon($polygon);
74
        }
75
76
        return $multiPolygon;
77
    }
78
79
    /**
80
     * @throws InvalidPositionException
81
     */
82 1
    public static function createForPointCoordinates(array $coordinates): Point
83
    {
84 1
        return new Point(new Position($coordinates[0], $coordinates[1], $coordinates[2] ?? null));
85
    }
86
87
    /**
88
     * @throws InvalidPositionException
89
     */
90
    public static function createForPolygonCoordinates(array $coordinates): ?Polygon
91
    {
92
        $exteriorCoordinates = array_shift($coordinates);
93
        if (null === $exteriorCoordinates) {
94
            return null;
95
        }
96
97
        $polygon = new Polygon(self::createForLineStringCoordinates($exteriorCoordinates));
98
        foreach ($coordinates as $lineStringData) {
99
            $polygon->addInteriorRing(self::createForLineStringCoordinates($lineStringData));
100
        }
101
102
        return $polygon;
103
    }
104
}
105