Test Failed
Pull Request — master (#33)
by Gordon
14:47
created

GeoJsonService::index()   B

Complexity

Conditions 9
Paths 24

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.111

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 55
ccs 16
cts 18
cp 0.8889
crap 9.111
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
use Smindel\GIS\Interfaces\HeaderAltererInterface;
10
11
class GeoJsonService extends AbstractGISWebServiceController
12
{
13
    private static $url_handlers = [
14
        '$Model' => 'handleAction',
15
    ];
16 2
17
    /**
18 2
     * @var HeaderAltererInterface
19 2
     */
20
    private $headerAlterer;
21
22
23 2
    public function __construct()
24
    {
25 2
        parent::__construct();
26
        $this->headerAlterer = Injector::inst()->get('Smindel\GIS\Helper\HeaderAlterer');
27
    }
28 2
29
    public function getConfig($model)
30 2
    {
31 2
        $modelConfig = parent::getConfig($model);
32 2
        if (!$modelConfig) {
33
            return false;
34 2
        }
35
        $defaults = [
36
            'property_map' => singleton($model)->summaryFields(),
37
        ];
38
        return is_array($modelConfig) ? array_merge($defaults, $modelConfig) : $defaults;
39 2
    }
40
41
    public function index($request)
42 2
    {
43
        $model = $this->getModel($request);
44 2
        $config = $this->getConfig($model);
45 2
        $list = $this->getRecords($request);
46
47
        $propertyMap = $config['property_map'];
48
49 2
50
        $this->headerAlterer->setHeader('Access-Control-Allow-Origin: ' . $config['access_control_allow_origin']);
51 2
52
        // The HTTP kernel keeps a copy of the response body, which
53 2
        // can exhaust the memory limit for large data sets. So we
54 2
        // opt out and flush the buffer after processing each feature.
55 2
        if (!($is_test = headers_sent())) {
56
            $this->headerAlterer->setHeader('Content-Type: application/geo+json');
57
        }
58 2
        echo '{"type":"FeatureCollection","features":[';
59 2
60
        foreach ($list as $item) {
61 2
            if (!$item->canView()) {
62 2
                continue;
63
            }
64 2
65
            echo isset($geo) ? ',' : '';
66
67
            $geo = GIS::create($item->{$config['geometry_field']})->reproject(4326);
68 2
69 2
            $properties = [];
70
            foreach ($propertyMap as $fieldName => $propertyName) {
71
                $properties[$propertyName] = $item->$fieldName;
72 2
            }
73
74
            $feature = [
75
                'type' => 'Feature',
76
                'geometry' => ['type' => $geo->type],
77
                'properties' => $properties,
78
            ];
79
            if ($geo->type == GIS::TYPES['geometrycollection']) {
80
                $geometries = [];
81
                foreach ($geo->geometries as $geometry) {
82
                    $geometries[] = [
83
                        'type' => $geometry->type,
84
                        'coordinates' => $geometry->coordinates,
85
                    ];
86
                }
87
                $feature['geometry']['geometries'] =  $geometries;
88
            } else {
89
                $feature['geometry']['coordinates'] =  $geo->coordinates;
90
            }
91
            echo json_encode($feature);
92
        }
93
        echo ']}';
94
        if (!$is_test) {
95
            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...
96
        }
97
    }
98
}
99