| Conditions | 11 |
| Paths | 194 |
| Total Lines | 94 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 84 | public function init() |
||
| 85 | { |
||
| 86 | parent::init(); |
||
| 87 | |||
| 88 | // prevent init of map if no query |
||
| 89 | $request = Controller::curr()->getRequest(); |
||
| 90 | if (!$this->getTrigger($request)) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | $locations = $this->getLocations(); |
||
| 95 | |||
| 96 | // prevent init of map if there are no locations |
||
| 97 | if (!$locations) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | // google maps api key |
||
| 102 | $key = Config::inst()->get(GoogleGeocoder::class, 'map_api_key'); |
||
| 103 | Requirements::javascript('https://maps.google.com/maps/api/js?key=' . $key); |
||
| 104 | |||
| 105 | $mapSettings = [ |
||
| 106 | 'fullMapStart' => true, |
||
| 107 | 'maxDistance' => true, |
||
| 108 | 'storeLimit' => -1, |
||
| 109 | 'originMarker' => true, |
||
| 110 | 'slideMap' => false, |
||
| 111 | 'distanceAlert' => -1, |
||
| 112 | 'featuredLocations' => false, |
||
| 113 | 'defaultLat' => 0, |
||
| 114 | 'defaultLng' => 0, |
||
| 115 | 'mapSettings' => [ |
||
| 116 | 'zoom' => 12, |
||
| 117 | 'mapTypeId' => 'google.maps.MapTypeId.ROADMAP', |
||
| 118 | 'disableDoubleClickZoom' => true, |
||
| 119 | 'scrollwheel' => false, |
||
| 120 | 'navigationControl' => false, |
||
| 121 | 'draggable' => false, |
||
| 122 | ], |
||
| 123 | ]; |
||
| 124 | |||
| 125 | $mapSettings['listTemplatePath'] = $this->getListTemplate(); |
||
| 126 | $mapSettings['infowindowTemplatePath'] = $this->getInfoWindowTemplate(); |
||
| 127 | $mapSettings['lengthUnit'] = $this->data()->Unit == 'km' ? 'km' : 'm'; |
||
| 128 | $mapSettings['mapID'] = Config::inst()->get(LocatorController::class, 'map_container'); |
||
| 129 | $mapSettings['locationList'] = Config::inst()->get(LocatorController::class, 'list_container'); |
||
| 130 | |||
| 131 | if ($locations->filter('Featured', true)->count() > 0) { |
||
| 132 | $mapSettings['featuredLocations'] = true; |
||
| 133 | } |
||
| 134 | |||
| 135 | // map config based on user input in Settings tab |
||
| 136 | $limit = Config::inst()->get(LocatorController::class, 'limit'); |
||
| 137 | if (0 < $limit) { |
||
| 138 | $mapSettings['storeLimit'] = $limit; |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($coords = $this->getAddressSearchCoords()) { |
||
| 142 | $mapSettings['defaultLat'] = $coords->getField("Lat"); |
||
| 143 | $mapSettings['defaultLat'] = $coords->getField("Lng"); |
||
| 144 | } |
||
| 145 | |||
| 146 | // pass GET variables to xml action |
||
| 147 | $vars = $this->request->getVars(); |
||
| 148 | unset($vars['url']); |
||
| 149 | $url = ''; |
||
| 150 | if (count($vars)) { |
||
| 151 | $url .= '?' . http_build_query($vars); |
||
| 152 | } |
||
| 153 | $mapSettings['dataLocation'] = Controller::join_links($this->Link(), 'xml.xml', $url); |
||
| 154 | |||
| 155 | if ($stylePath = $this->getMapStyleJSONPath()) { |
||
| 156 | if ($style = file_get_contents($stylePath)) { |
||
| 157 | $mapSettings['mapSettings']['styles'] = json_decode($style); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($imagePath = $this->getMarkerIcon()) { |
||
| 162 | $mapSettings['markerImg'] = $imagePath; |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->extend('modifyMapSettings', $mapSettings); |
||
| 166 | |||
| 167 | $encoded = json_encode($mapSettings, JSON_UNESCAPED_SLASHES); |
||
| 168 | // mapTypeId is a javascript object |
||
| 169 | $encoded = preg_replace('/("mapTypeId"\s*:\s*)"(.+?)"/i', '$1$2', $encoded); |
||
| 170 | |||
| 171 | $this->extend('modifyMapSettingsEncoded', $encoded); |
||
| 172 | // init map |
||
| 173 | Requirements::customScript(" |
||
| 174 | $(function(){ |
||
| 175 | $('#map-container').storeLocator({$encoded}); |
||
| 176 | }); |
||
| 177 | ", 'jquery-locator'); |
||
| 178 | } |
||
| 366 |