GeoJsonService::index()   B
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9.1399

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 33
c 2
b 0
f 0
nc 24
nop 1
dl 0
loc 54
ccs 22
cts 25
cp 0.88
crap 9.1399
rs 8.0555

How to fix   Long Method   

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
namespace Smindel\GIS\Control;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Config\Config;
8
use Smindel\GIS\GIS;
9
10
class GeoJsonService extends AbstractGISWebServiceController
11
{
12
    private static $url_handlers = array(
13
        '$Model' => 'handleAction',
14
    );
15
16 2
    public function getConfig($model)
17
    {
18 2
        $modelConfig = parent::getConfig($model);
19 2
        if (!$modelConfig) {
20
            return false;
21
        }
22
        $defaults = [
23 2
            'property_map' => singleton($model)->summaryFields(),
24
        ];
25 2
        return is_array($modelConfig) ? array_merge($defaults, $modelConfig) : $defaults;
26
    }
27
28 2
    public function index($request)
29
    {
30 2
        $model = $this->getModel($request);
31 2
        $config = $this->getConfig($model);
32 2
        $list = $this->getRecords($request);
33
34 2
        $propertyMap = $config['property_map'];
35
36
        header('Access-Control-Allow-Origin: ' . $config['access_control_allow_origin']);
37
38
        // The HTTP kernel keeps a copy of the response body, which
39 2
        // can exhaust the memory limit for large data sets. So we
40
        // opt out and flush the buffer after processing each feature.
41
        if (!($is_test = headers_sent())) {
42 2
            header('Content-Type: application/geo+json');
43
        }
44 2
        echo '{"type":"FeatureCollection","features":[';
45 2
46
        foreach ($list as $item) {
47
            if (!$item->canView()) {
48
                continue;
49 2
            }
50
51 2
            echo isset($geo) ? ',' : '';
52
53 2
            $geo = GIS::create($item->{$config['geometry_field']})->reproject(4326);
54 2
55 2
            $properties = [];
56
            foreach ($propertyMap as $fieldName => $propertyName) {
57
                $properties[$propertyName] = $item->$fieldName;
58 2
            }
59 2
60
            $feature = [
61 2
                'type' => 'Feature',
62 2
                'properties' => $properties,
63
                'geometry' => ['type' => $geo->type]
64 2
            ];
65
            if ($geo->type == GIS::TYPES['geometrycollection']) {
66
                $geometries = [];
67
                foreach ($geo->geometries as $geometry) {
68 2
                    $geometries[] = [
69 2
                        'type' => $geometry->type,
70
                        'coordinates' => $geometry->coordinates,
71
                    ];
72 2
                }
73
                $feature['geometry']['geometries'] =  $geometries;
74
            } else {
75
                $feature['geometry']['coordinates'] =  $geo->coordinates;
76
            }
77
            echo json_encode($feature);
78
        }
79
        echo ']}';
80
        if (!$is_test) {
81
            die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
82
        }
83
    }
84
}
85