Passed
Pull Request — main (#10)
by
unknown
02:31
created

createFromGeoJSONArray()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.017

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 34
ccs 17
cts 18
cp 0.9444
rs 7.6666
c 2
b 0
f 0
cc 10
nc 16
nop 2
crap 10.017

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PrinsFrank\PhpGeoSVG\Geometry;
6
7
use JsonException;
8
use PrinsFrank\PhpGeoSVG\Exception\InvalidPositionException;
9
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException;
10
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\GeometryObjectFactory;
11
12
class GeometryCollectionFactory
13
{
14
    /**
15
     * @throws NotImplementedException|InvalidPositionException
16
     */
17 5
    public static function createFromGeoJSONArray(array $geoJSONArray, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection
18
    {
19 5
        $geometryCollection = new GeometryCollection();
20
21 5
        if(!is_null($geometryObjectCallback)) {
22
            $geometryCollection->setGeometryObjectCallback($geometryObjectCallback);
23
        }
24
25 5
        if ('FeatureCollection' !== $geoJSONArray['type']) {
26 1
            throw new NotImplementedException('Only FeatureCollections are currently supported');
27
        }
28
29 4
        foreach ($geoJSONArray['features'] ?? [] as $feature) {
30 4
            if ('Feature' !== $feature['type']) {
31 1
                throw new NotImplementedException('Only features of type "Feature" are supported.');
32
            }
33
34 3
            $geometryObject = GeometryObjectFactory::createForGeoJsonFeatureGeometry($feature['geometry']);
35 3
            if (null === $geometryObject) {
36 1
                continue;
37
            }
38
39 2
            if (array_key_exists('properties', $feature) && array_key_exists('featurecla', $feature['properties'])) {
40 1
                $geometryObject->setFeatureClass($feature['properties']['featurecla']);
41
            }
42
43 2
            if(array_key_exists('properties', $feature) && !empty($feature['properties'])) {
44 1
                $geometryObject->setProperties($feature['properties']);
45
            }
46
47 2
            $geometryCollection->addGeometryObject($geometryObject);
48
        }
49
50 3
        return $geometryCollection;
51
    }
52
53
    /**
54
     * @throws JsonException|NotImplementedException|InvalidPositionException
55
     */
56 1
    public static function createFromGeoJsonString(string $geoJsonString, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection
57
    {
58 1
        return self::createFromGeoJSONArray(json_decode($geoJsonString, true, 512, JSON_THROW_ON_ERROR), $geometryObjectCallback);
59
    }
60
61
    /**
62
     * @throws JsonException|NotImplementedException|InvalidPositionException
63
     */
64 1
    public static function createFromGeoJSONFilePath(string $path, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection
65
    {
66 1
        return self::createFromGeoJsonString(file_get_contents($path), $geometryObjectCallback);
67
    }
68
}
69