Passed
Pull Request — master (#173)
by Matthew
13:34
created

LocatorController::getLocations()   C

Complexity

Conditions 10
Paths 48

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 3
b 0
f 0
nc 48
nop 1
dl 0
loc 54
rs 6.8372

How to fix   Long Method    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
namespace Dynamic\Locator;
4
5
use Dynamic\SilverStripeGeocoder\GoogleGeocoder;
6
use muskie9\DataToArrayList\ORM\DataToArrayListHelper;
0 ignored issues
show
Bug introduced by
The type muskie9\DataToArrayList\ORM\DataToArrayListHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataList;
10
use SilverStripe\View\ArrayData;
11
use SilverStripe\View\Requirements;
12
use SilverStripe\Control\HTTPRequest;
13
14
/**
15
 * Class LocatorController
16
 */
17
class LocatorController extends \PageController
0 ignored issues
show
Bug introduced by
The type PageController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
{
19
20
    /**
21
     * @var array
22
     */
23
    private static $base_filter = [];
0 ignored issues
show
Unused Code introduced by
The property $base_filter is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    /**
26
     * @var array
27
     */
28
    private static $base_exclude = [
0 ignored issues
show
Unused Code introduced by
The property $base_exclude is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
        'Lat' => 0,
30
        'Lng' => 0,
31
    ];
32
33
    /**
34
     * @var array
35
     */
36
    private static $base_filter_any = [];
0 ignored issues
show
Unused Code introduced by
The property $base_filter_any is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    private static $allowed_actions = array(
39
        'json',
40
        'settings'
41
    );
42
43
    /**
44
     * @var DataList|ArrayList
45
     */
46
    protected $locations;
47
48
    /**
49
     * Set Requirements based on input from CMS
50
     */
51
    public function init()
52
    {
53
        parent::init();
54
55
        $key = Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key');
56
        Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key);
57
    }
58
59
    public function json()
60
    {
61
        $this->getResponse()->addHeader("Content-Type", "application/json");
62
63
        $data = new ArrayData(array(
64
            "Locations" => $this->getLocations($this->getRequest()),
65
        ));
66
67
        return $data->renderWith('Dynamic/Locator/locations');
68
    }
69
70
    public function settings()
71
    {
72
        $this->getResponse()->addHeader("Content-Type", "application/json");
73
74
        $data = new ArrayData(array(
75
            // TODO
76
            "Radii" => $this->getRadii(),
77
            "Limit" => $this->getLimit(),
78
            "ShowRadius" => $this->getShowRadius(),
79
            "InfoWindowTemplate" => $this->getInfoWindowTemplate(),
80
            "ListTemplate" => $this->getListTemplate(),
81
            "Categories" => $this->getCategories(),
82
            "Unit" => $this->Unit,
83
            "Clusters" => $this->Clusters,
84
        ));
85
86
        $this->extend('updateSettings', $data);
87
88
        return $data->renderWith('Dynamic/Locator/settings');
89
    }
90
91
    /**
92
     * @param HTTPRequest|null $request
93
     *
94
     * @return ArrayList|DataList|static
95
     */
96
    public function getLocations(HTTPRequest $request = null)
97
    {
98
        if ($request === null) {
99
            $request = $this->request;
100
        }
101
102
        $filter = $this->config()->get('base_filter');
103
104
        if ($request->getVar('category')) {
105
            $filter['CategoryID'] = $request->getVar('category');
106
        } else {
107
            if ($this->getCategories()->exists()) {
108
                foreach ($this->getCategories() as $category) {
109
                    $filter['CategoryID'][] = $category->ID;
110
                }
111
            }
112
        }
113
114
        $this->extend('updateLocatorFilter', $filter, $request);
115
116
        $filterAny = $this->config()->get('base_filter_any');
117
        $this->extend('updateLocatorFilterAny', $filterAny, $request);
118
119
        $exclude = $this->config()->get('base_exclude');
120
        $this->extend('updateLocatorExclude', $exclude, $request);
121
122
        $locations = Locator::get_locations($filter, $filterAny, $exclude);
123
124
        $locations = DataToArrayListHelper::to_array_list($locations);
125
126
        //allow for adjusting list post possible distance calculation
127
        $this->extend('updateLocationList', $locations);
128
        if ($locations->canSortBy('distance')) {
129
            $locations = $locations->sort('distance');
130
        }
131
132
        if ($request->getVar('address') && $request->getVar('radius')) {
133
            $radius = $request->getVar('radius');
134
            $locations = $locations->filterByCallback(function ($location) use (&$radius) {
135
                if ($radius === -1) {
136
                    return true;
137
                }
138
                return $location->Distance <= $radius;
139
            });
140
        }
141
142
        //allow for returning list to be set as
143
        $this->extend('updateListType', $locations);
144
        $limit = Config::inst()->get(LocatorController::class, 'limit');
145
        if ($limit > 0) {
146
            $locations = $locations->limit($limit);
147
        }
148
149
        return $locations;
150
    }
151
}
152