| Total Complexity | 51 |
| Total Lines | 519 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LocationController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LocationController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 74 | class LocationController extends AbstractAdminController |
||
| 75 | { |
||
| 76 | // Location of files to import |
||
| 77 | private const PLACES_FOLDER = 'places/'; |
||
| 78 | |||
| 79 | /** @var GedcomService */ |
||
| 80 | private $gedcom_service; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Dependency injection. |
||
| 84 | * |
||
| 85 | * @param GedcomService $gedcom_service |
||
| 86 | */ |
||
| 87 | public function __construct(GedcomService $gedcom_service) |
||
| 88 | { |
||
| 89 | $this->gedcom_service = $gedcom_service; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param int $id |
||
| 94 | * |
||
| 95 | * @return array<stdClass> |
||
| 96 | */ |
||
| 97 | private function getHierarchy(int $id): array |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param ServerRequestInterface $request |
||
| 121 | * |
||
| 122 | * @return ResponseInterface |
||
| 123 | */ |
||
| 124 | public function mapDataEdit(ServerRequestInterface $request): ResponseInterface |
||
| 125 | { |
||
| 126 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 127 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 128 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
| 129 | $parent = new PlaceLocation($fqpn); |
||
| 130 | |||
| 131 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
| 132 | $hierarchy = $this->getHierarchy($place_id); |
||
| 133 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
| 134 | $location = new PlaceLocation($fqpn); |
||
| 135 | |||
| 136 | if ($location->id() !== 0) { |
||
| 137 | $title = e($location->locationName()); |
||
| 138 | } else { |
||
| 139 | // Add a place |
||
| 140 | if ($parent_id === 0) { |
||
| 141 | // We're at the global level so create a minimal |
||
| 142 | // place for the page title and breadcrumbs |
||
| 143 | $title = I18N::translate('World'); |
||
| 144 | $hierarchy = []; |
||
| 145 | } else { |
||
| 146 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 147 | $tmp = new PlaceLocation($hierarchy[0]->fqpn); |
||
| 148 | $title = e($tmp->locationName()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $breadcrumbs = [ |
||
| 153 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
| 154 | route(MapDataList::class) => I18N::translate('Geographic data'), |
||
| 155 | ]; |
||
| 156 | |||
| 157 | foreach ($hierarchy as $row) { |
||
| 158 | $breadcrumbs[route(MapDataList::class, ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($place_id === 0) { |
||
| 162 | $title .= ' — ' . I18N::translate('Add'); |
||
| 163 | $breadcrumbs[] = I18N::translate('Add'); |
||
| 164 | $latitude = null; |
||
| 165 | $longitude = null; |
||
| 166 | $map_bounds = $parent->boundingRectangle(); |
||
| 167 | } else { |
||
| 168 | $title .= ' — ' . I18N::translate('Edit'); |
||
| 169 | $breadcrumbs[] = I18N::translate('Edit'); |
||
| 170 | $latitude = $location->latitude(); |
||
| 171 | $longitude = $location->longitude(); |
||
| 172 | $map_bounds = $location->boundingRectangle(); |
||
| 173 | } |
||
| 174 | |||
| 175 | // If the current co-ordinates are unknown, leave the input fields empty, |
||
| 176 | // and show a marker in the middle of the map. |
||
| 177 | if ($latitude === null || $longitude === null) { |
||
| 178 | $latitude = ''; |
||
| 179 | $longitude = ''; |
||
| 180 | |||
| 181 | $marker_position = [ |
||
| 182 | ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0, |
||
| 183 | ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0, |
||
| 184 | ]; |
||
| 185 | } else { |
||
| 186 | $marker_position = [$latitude, $longitude]; |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->viewResponse('admin/location-edit', [ |
||
| 190 | 'breadcrumbs' => $breadcrumbs, |
||
| 191 | 'title' => $title, |
||
| 192 | 'location' => $location, |
||
| 193 | 'latitude' => $latitude, |
||
| 194 | 'longitude' => $longitude, |
||
| 195 | 'map_bounds' => $map_bounds, |
||
| 196 | 'marker_position' => $marker_position, |
||
| 197 | 'parent' => $parent, |
||
| 198 | 'level' => $parent_id, |
||
| 199 | 'provider' => [ |
||
| 200 | 'url' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
||
| 201 | 'options' => [ |
||
| 202 | 'attribution' => '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap</a> contributors', |
||
| 203 | 'max_zoom' => 19 |
||
| 204 | ] |
||
| 205 | ], |
||
| 206 | ]); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param ServerRequestInterface $request |
||
| 211 | * |
||
| 212 | * @return ResponseInterface |
||
| 213 | */ |
||
| 214 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
| 215 | { |
||
| 216 | $params = (array) $request->getParsedBody(); |
||
| 217 | |||
| 218 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 219 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
| 220 | $lat = $this->gedcom_service->writeLatitude((float) $params['new_place_lati']); |
||
| 221 | $lng = $this->gedcom_service->writeLongitude((float) $params['new_place_long']); |
||
| 222 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 223 | $level = count($hierarchy); |
||
| 224 | $icon = $params['icon']; |
||
| 225 | $zoom = (int) $params['new_zoom_factor']; |
||
| 226 | |||
| 227 | if ($place_id === 0) { |
||
| 228 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
| 229 | |||
| 230 | DB::table('placelocation')->insert([ |
||
| 231 | 'pl_id' => $place_id, |
||
| 232 | 'pl_parent_id' => $parent_id, |
||
| 233 | 'pl_level' => $level, |
||
| 234 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
| 235 | 'pl_lati' => $lat, |
||
| 236 | 'pl_long' => $lng, |
||
| 237 | 'pl_zoom' => $zoom, |
||
| 238 | 'pl_icon' => $icon, |
||
| 239 | ]); |
||
| 240 | } else { |
||
| 241 | DB::table('placelocation') |
||
| 242 | ->where('pl_id', '=', $place_id) |
||
| 243 | ->update([ |
||
| 244 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
| 245 | 'pl_lati' => $lat, |
||
| 246 | 'pl_long' => $lng, |
||
| 247 | 'pl_zoom' => $zoom, |
||
| 248 | 'pl_icon' => $icon, |
||
| 249 | ]); |
||
| 250 | } |
||
| 251 | |||
| 252 | FlashMessages::addMessage( |
||
| 253 | I18N::translate( |
||
| 254 | 'The details for “%s” have been updated.', |
||
| 255 | e($params['new_place_name']) |
||
| 256 | ), |
||
| 257 | 'success' |
||
| 258 | ); |
||
| 259 | |||
| 260 | $url = route(MapDataList::class, ['parent_id' => $parent_id]); |
||
| 261 | |||
| 262 | return redirect($url); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param ServerRequestInterface $request |
||
| 267 | * |
||
| 268 | * @return ResponseInterface |
||
| 269 | */ |
||
| 270 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
| 271 | { |
||
| 272 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 273 | $format = $request->getQueryParams()['format']; |
||
| 274 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 275 | |||
| 276 | // Create the file name |
||
| 277 | // $hierarchy[0] always holds the full placename |
||
| 278 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
| 279 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
| 280 | $filename = addcslashes('Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name), '"'); |
||
| 281 | |||
| 282 | // Fill in the place names for the starting conditions |
||
| 283 | $startfqpn = []; |
||
| 284 | foreach ($hierarchy as $record) { |
||
| 285 | $startfqpn[] = $record->pl_place; |
||
| 286 | } |
||
| 287 | |||
| 288 | // Generate an array containing the data to output. |
||
| 289 | $places = []; |
||
| 290 | $this->buildExport($parent_id, $startfqpn, $places); |
||
| 291 | |||
| 292 | // Pad all locations to the length of the longest. |
||
| 293 | $max_level = 0; |
||
| 294 | foreach ($places as $place) { |
||
| 295 | $max_level = max($max_level, count($place->fqpn)); |
||
| 296 | } |
||
| 297 | |||
| 298 | $places = array_map(static function (stdClass $place) use ($max_level): array { |
||
| 299 | return array_merge( |
||
| 300 | [count($place->fqpn) - 1], |
||
| 301 | array_pad($place->fqpn, $max_level, ''), |
||
| 302 | [$place->pl_long], |
||
| 303 | [$place->pl_lati], |
||
| 304 | [$place->pl_zoom], |
||
| 305 | [$place->pl_icon] |
||
| 306 | ); |
||
| 307 | }, $places); |
||
| 308 | |||
| 309 | if ($format === 'csv') { |
||
| 310 | // Create the header line for the output file (always English) |
||
| 311 | $header = [ |
||
| 312 | I18N::translate('Level'), |
||
| 313 | ]; |
||
| 314 | |||
| 315 | for ($i = 0; $i < $max_level; $i++) { |
||
| 316 | $header[] = 'Place' . $i; |
||
| 317 | } |
||
| 318 | |||
| 319 | $header[] = 'Longitude'; |
||
| 320 | $header[] = 'Latitude'; |
||
| 321 | $header[] = 'Zoom'; |
||
| 322 | $header[] = 'Icon'; |
||
| 323 | |||
| 324 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $this->exportGeoJSON($filename . '.geojson', $places, $max_level); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param int $parent_id |
||
| 332 | * @param array<string> $fqpn |
||
| 333 | * @param array<stdClass> $places |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | * @throws Exception |
||
| 337 | */ |
||
| 338 | private function buildExport(int $parent_id, array $fqpn, array &$places): void |
||
| 339 | { |
||
| 340 | // Current number of levels. |
||
| 341 | $level = count($fqpn); |
||
| 342 | |||
| 343 | // Data for the next level. |
||
| 344 | $rows = DB::table('placelocation') |
||
| 345 | ->where('pl_parent_id', '=', $parent_id) |
||
| 346 | ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */')) |
||
| 347 | ->get() |
||
| 348 | ->filter(function ($v) { |
||
| 349 | return $v->pl_lati !== '' && $v->pl_long !== ''; |
||
| 350 | }); |
||
| 351 | |||
| 352 | foreach ($rows as $row) { |
||
| 353 | $fqpn[$level] = $row->pl_place; |
||
| 354 | |||
| 355 | $row->fqpn = $fqpn; |
||
| 356 | $row->pl_zoom = (int) $row->pl_zoom; |
||
| 357 | $row->pl_icon = (string) $row->pl_icon; |
||
| 358 | |||
| 359 | $places[] = $row; |
||
| 360 | |||
| 361 | $this->buildExport((int) $row->pl_id, $fqpn, $places); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $filename |
||
| 367 | * @param string[] $columns |
||
| 368 | * @param string[][] $places |
||
| 369 | * |
||
| 370 | * @return ResponseInterface |
||
| 371 | */ |
||
| 372 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $filename |
||
| 395 | * @param array $rows |
||
| 396 | * @param int $maxlevel |
||
| 397 | * |
||
| 398 | * @return ResponseInterface |
||
| 399 | */ |
||
| 400 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param ServerRequestInterface $request |
||
| 438 | * |
||
| 439 | * @return ResponseInterface |
||
| 440 | */ |
||
| 441 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
| 464 | ]); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * This function assumes the input file layout is |
||
| 469 | * level followed by a variable number of placename fields |
||
| 470 | * followed by Longitude, Latitude, Zoom & Icon |
||
| 471 | * |
||
| 472 | * @param ServerRequestInterface $request |
||
| 473 | * |
||
| 474 | * @return ResponseInterface |
||
| 475 | * @throws Exception |
||
| 476 | */ |
||
| 477 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
| 595 |