GeometryParser   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 19
ccs 7
cts 11
cp 0.6364
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\Melvin\Parsers;
6
7
use GeoJson\Exception\Exception as GeoJsonException;
8
use GeoJson\GeoJson;
9
use GeoJson\Geometry\Geometry;
10
use Swis\Melvin\Exceptions\ParseException;
11
12
class GeometryParser
13
{
14 6
    public function parse(\stdClass $object): Geometry
15
    {
16 6
        if ($object->type === 'MultiLineString') {
17 6
            $object->coordinates = array_values(
18 6
                array_filter($object->coordinates, fn ($array) => !empty($array))
19 3
            );
20
21 6
            if (count($object->coordinates) === 1) {
22
                $object->type = 'LineString';
23
                $object->coordinates = $object->coordinates[0];
24
            }
25
        }
26
27
        try {
28 6
            return GeoJson::jsonUnserialize($object);
29
        } catch (GeoJsonException|\InvalidArgumentException $e) {
30
            throw new ParseException('Failed to parse geometry', 0, $e);
31
        }
32
    }
33
}
34