Conditions | 11 |
Paths | 194 |
Total Lines | 96 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
117 | public function init() |
||
118 | { |
||
119 | parent::init(); |
||
120 | |||
121 | // prevent init of map if no query |
||
122 | $request = Controller::curr()->getRequest(); |
||
123 | if (!$this->getTrigger($request)) { |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $locations = $this->getLocations(); |
||
128 | |||
129 | // prevent init of map if there are no locations |
||
130 | if (!$locations) { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | // google maps api key |
||
135 | $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
||
136 | Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
||
137 | |||
138 | $mapSettings = [ |
||
139 | 'fullMapStart' => true, |
||
140 | 'maxDistance' => true, |
||
141 | 'storeLimit' => -1, |
||
142 | 'originMarker' => true, |
||
143 | 'slideMap' => false, |
||
144 | 'distanceAlert' => -1, |
||
145 | 'featuredLocations' => false, |
||
146 | 'defaultLat' => 0, |
||
147 | 'defaultLng' => 0, |
||
148 | 'mapSettings' => [ |
||
149 | 'zoom' => Config::inst()->get(LocatorController::class, 'zoom'), |
||
150 | 'minZoom' => Config::inst()->get(LocatorController::class, 'min_zoom'), |
||
151 | 'maxZoom' => Config::inst()->get(LocatorController::class, 'max_zoom'), |
||
152 | 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
||
153 | 'disableDoubleClickZoom' => Config::inst()->get(LocatorController::class, 'disable_double_click_zoom'), |
||
154 | 'scrollwheel' => Config::inst()->get(LocatorController::class, 'scrollwheel'), |
||
155 | 'navigationControl' => Config::inst()->get(LocatorController::class, 'navigation_control'), |
||
156 | 'draggable' => Config::inst()->get(LocatorController::class, 'draggable'), |
||
157 | ], |
||
158 | ]; |
||
159 | |||
160 | $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
||
161 | $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
||
162 | $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
||
163 | $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
||
164 | $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
||
165 | |||
166 | if ($locations->filter('Featured', true)->count() > 0) { |
||
167 | $mapSettings['featuredLocations'] = true; |
||
168 | } |
||
169 | |||
170 | // map config based on user input in Settings tab |
||
171 | $limit = Config::inst()->get(LocatorController::class, 'limit'); |
||
172 | if (0 < $limit) { |
||
173 | $mapSettings['storeLimit'] = $limit; |
||
174 | } |
||
175 | |||
176 | if ($coords = $this->getAddressSearchCoords()) { |
||
177 | $mapSettings['defaultLat'] = $coords->getField("Lat"); |
||
178 | $mapSettings['defaultLat'] = $coords->getField("Lng"); |
||
179 | } |
||
180 | |||
181 | // pass GET variables to xml action |
||
182 | $vars = $this->request->getVars(); |
||
183 | unset($vars['url']); |
||
184 | $url = ''; |
||
185 | if (count($vars)) { |
||
186 | $url .= '?' . http_build_query($vars); |
||
187 | } |
||
188 | $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
||
189 | |||
190 | if ($stylePath = $this->getMapStyleJSONPath()) { |
||
191 | if ($style = file_get_contents($stylePath)) { |
||
192 | $mapSettings['mapSettings']['styles'] = json_decode($style); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | if ($imagePath = $this->getMarkerIcon()) { |
||
197 | $mapSettings['markerImg'] = $imagePath; |
||
198 | } |
||
199 | |||
200 | $this->extend('modifyMapSettings', $mapSettings); |
||
201 | |||
202 | $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
||
203 | // mapTypeId is a javascript object |
||
204 | $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
||
205 | |||
206 | $this->extend('modifyMapSettingsEncoded', $encoded); |
||
207 | // init map |
||
208 | Requirements::customScript(" |
||
209 | $(function(){ |
||
210 | $('#map-container').storeLocator({$encoded}); |
||
211 | }); |
||
212 | ", 'jquery-locator'); |
||
213 | } |
||
401 |