GeoJsonImporter::import()   B
last analyzed

Complexity

Conditions 8
Paths 22

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.0877

Importance

Changes 2
Bugs 1 Features 2
Metric Value
cc 8
eloc 17
c 2
b 1
f 2
nc 22
nop 5
dl 0
loc 26
ccs 16
cts 18
cp 0.8889
crap 8.0877
rs 8.4444
1
<?php
2
3
namespace Smindel\GIS\Service;
4
5
use Smindel\GIS\GIS;
6
7
class GeoJsonImporter
8
{
9 1
    public static function import($class, $geoJson, $propertyMap = null, $geoProperty = null, $featureCallback = null)
10
    {
11 1
        $geoProperty = $geoProperty ?: GIS::of($class);
12 1
        $features = (is_array($geoJson) ? $geoJson : json_decode($geoJson, true))['features'];
13 1
        foreach ($features as $feature) {
14 1
            if (is_callable($featureCallback)) {
15
                $feature = $featureCallback($feature);
16
            }
17
18 1
            if ($feature['type'] != 'Feature') {
19
                continue;
20
            }
21
22 1
            if ($propertyMap === null) {
23 1
                $propertyMap = array_intersect(array_keys($class::config()->get('db')), array_keys($feature['properties']));
24 1
                $propertyMap = array_combine($propertyMap, $propertyMap);
25
            }
26
27 1
            $obj = $class::create();
28 1
            $array = $feature['geometry'];
29 1
            $array['srid'] = 4326;
30 1
            $obj->$geoProperty = (string)GIS::create($array);
31 1
            foreach ($propertyMap as $doProperty => $jsonProperty) {
32 1
                $obj->$doProperty = $feature['properties'][$jsonProperty];
33
            }
34 1
            $obj->write();
35
        }
36 1
    }
37
}
38