| Total Complexity | 56 |
| Total Lines | 528 |
| 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 |
||
| 71 | class LocationController extends AbstractAdminController |
||
| 72 | { |
||
| 73 | // Location of files to import |
||
| 74 | private const PLACES_FOLDER = 'places/'; |
||
| 75 | |||
| 76 | /** @var GedcomService */ |
||
| 77 | private $gedcom_service; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Dependency injection. |
||
| 81 | * |
||
| 82 | * @param GedcomService $gedcom_service |
||
| 83 | */ |
||
| 84 | public function __construct(GedcomService $gedcom_service) |
||
| 85 | { |
||
| 86 | $this->gedcom_service = $gedcom_service; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param int $id |
||
| 91 | * |
||
| 92 | * @return array<stdClass> |
||
| 93 | */ |
||
| 94 | private function getHierarchy(int $id): array |
||
| 95 | { |
||
| 96 | $arr = []; |
||
| 97 | $fqpn = []; |
||
| 98 | |||
| 99 | while ($id !== 0) { |
||
| 100 | $row = DB::table('placelocation') |
||
| 101 | ->where('pl_id', '=', $id) |
||
| 102 | ->first(); |
||
| 103 | |||
| 104 | // For static analysis tools. |
||
| 105 | assert($row instanceof stdClass); |
||
| 106 | |||
| 107 | $fqpn[] = $row->pl_place; |
||
| 108 | $row->fqpn = implode(Gedcom::PLACE_SEPARATOR, $fqpn); |
||
| 109 | $id = (int) $row->pl_parent_id; |
||
| 110 | $arr[] = $row; |
||
| 111 | } |
||
| 112 | |||
| 113 | return array_reverse($arr); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param ServerRequestInterface $request |
||
| 118 | * |
||
| 119 | * @return ResponseInterface |
||
| 120 | */ |
||
| 121 | public function mapDataEdit(ServerRequestInterface $request): ResponseInterface |
||
| 122 | { |
||
| 123 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 124 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 125 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
| 126 | $parent = new PlaceLocation($fqpn); |
||
| 127 | |||
| 128 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
| 129 | $hierarchy = $this->getHierarchy($place_id); |
||
| 130 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
| 131 | $location = new PlaceLocation($fqpn); |
||
| 132 | |||
| 133 | if ($location->id() !== 0) { |
||
| 134 | $title = e($location->locationName()); |
||
| 135 | } else { |
||
| 136 | // Add a place |
||
| 137 | if ($parent_id === 0) { |
||
| 138 | // We're at the global level so create a minimal |
||
| 139 | // place for the page title and breadcrumbs |
||
| 140 | $title = I18N::translate('World'); |
||
| 141 | $hierarchy = []; |
||
| 142 | } else { |
||
| 143 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 144 | $tmp = new PlaceLocation($hierarchy[0]->fqpn); |
||
| 145 | |||
| 146 | if ($tmp->latitude() === 0.0 && $tmp->longitude() === 0.0) { |
||
|
|
|||
| 147 | FlashMessages::addMessage(I18N::translate('%s (coordinates [0,0]) cannot have a subordinate place', $hierarchy[0]->fqpn), 'warning'); |
||
| 148 | |||
| 149 | return redirect(route(MapDataList::class)); |
||
| 150 | } |
||
| 151 | $title = e($tmp->locationName()); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $breadcrumbs = [ |
||
| 156 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
| 157 | route(MapDataList::class) => I18N::translate('Geographic data'), |
||
| 158 | ]; |
||
| 159 | |||
| 160 | foreach ($hierarchy as $row) { |
||
| 161 | $breadcrumbs[route(MapDataList::class, ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($place_id === 0) { |
||
| 165 | $title .= ' — ' . I18N::translate('Add'); |
||
| 166 | $breadcrumbs[] = I18N::translate('Add'); |
||
| 167 | $latitude = null; |
||
| 168 | $longitude = null; |
||
| 169 | $map_bounds = $parent->boundingRectangle(); |
||
| 170 | } else { |
||
| 171 | $title .= ' — ' . I18N::translate('Edit'); |
||
| 172 | $breadcrumbs[] = I18N::translate('Edit'); |
||
| 173 | $latitude = $location->latitude(); |
||
| 174 | $longitude = $location->longitude(); |
||
| 175 | $map_bounds = $location->boundingRectangle(); |
||
| 176 | } |
||
| 177 | |||
| 178 | // If the current co-ordinates are unknown, leave the input fields empty, |
||
| 179 | // and show a marker in the middle of the map. |
||
| 180 | if ($latitude === null || $longitude === null) { |
||
| 181 | $latitude = ''; |
||
| 182 | $longitude = ''; |
||
| 183 | |||
| 184 | $marker_position = [ |
||
| 185 | ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0, |
||
| 186 | ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0, |
||
| 187 | ]; |
||
| 188 | } else { |
||
| 189 | $marker_position = [$latitude, $longitude]; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $this->viewResponse('admin/location-edit', [ |
||
| 193 | 'breadcrumbs' => $breadcrumbs, |
||
| 194 | 'title' => $title, |
||
| 195 | 'location' => $location, |
||
| 196 | 'latitude' => $latitude, |
||
| 197 | 'longitude' => $longitude, |
||
| 198 | 'map_bounds' => $map_bounds, |
||
| 199 | 'marker_position' => $marker_position, |
||
| 200 | 'parent' => $parent, |
||
| 201 | 'level' => $parent_id, |
||
| 202 | 'provider' => [ |
||
| 203 | 'url' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
||
| 204 | 'options' => [ |
||
| 205 | 'attribution' => '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap</a> contributors', |
||
| 206 | 'max_zoom' => 19 |
||
| 207 | ] |
||
| 208 | ], |
||
| 209 | ]); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ServerRequestInterface $request |
||
| 214 | * |
||
| 215 | * @return ResponseInterface |
||
| 216 | */ |
||
| 217 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
| 218 | { |
||
| 219 | $params = (array) $request->getParsedBody(); |
||
| 220 | |||
| 221 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 222 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
| 223 | $lat = $this->gedcom_service->writeLatitude((float) $params['new_place_lati']); |
||
| 224 | $lng = $this->gedcom_service->writeLongitude((float) $params['new_place_long']); |
||
| 225 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 226 | $level = count($hierarchy); |
||
| 227 | $icon = $params['icon']; |
||
| 228 | $zoom = (int) $params['new_zoom_factor']; |
||
| 229 | |||
| 230 | if ($parent_id > 0 && $lat === 'N0' && $lng === 'E0') { |
||
| 231 | FlashMessages::addMessage(I18N::translate('Location [0,0] cannot be subordinate to another place'), 'warning'); |
||
| 232 | } else { |
||
| 233 | if ($place_id === 0) { |
||
| 234 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
| 235 | |||
| 236 | DB::table('placelocation')->insert([ |
||
| 237 | 'pl_id' => $place_id, |
||
| 238 | 'pl_parent_id' => $parent_id, |
||
| 239 | 'pl_level' => $level, |
||
| 240 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
| 241 | 'pl_lati' => $lat, |
||
| 242 | 'pl_long' => $lng, |
||
| 243 | 'pl_zoom' => $zoom, |
||
| 244 | 'pl_icon' => $icon, |
||
| 245 | ]); |
||
| 246 | } else { |
||
| 247 | DB::table('placelocation') |
||
| 248 | ->where('pl_id', '=', $place_id) |
||
| 249 | ->update([ |
||
| 250 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
| 251 | 'pl_lati' => $lat, |
||
| 252 | 'pl_long' => $lng, |
||
| 253 | 'pl_zoom' => $zoom, |
||
| 254 | 'pl_icon' => $icon, |
||
| 255 | ]); |
||
| 256 | } |
||
| 257 | |||
| 258 | FlashMessages::addMessage( |
||
| 259 | I18N::translate( |
||
| 260 | 'The details for “%s” have been updated.', |
||
| 261 | e($params['new_place_name']) |
||
| 262 | ), |
||
| 263 | 'success' |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | $url = route(MapDataList::class, ['parent_id' => $parent_id]); |
||
| 267 | |||
| 268 | return redirect($url); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param ServerRequestInterface $request |
||
| 273 | * |
||
| 274 | * @return ResponseInterface |
||
| 275 | */ |
||
| 276 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
| 277 | { |
||
| 278 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 279 | $format = $request->getQueryParams()['format']; |
||
| 280 | $hierarchy = $this->getHierarchy($parent_id); |
||
| 281 | |||
| 282 | // Create the file name |
||
| 283 | // $hierarchy[0] always holds the full placename |
||
| 284 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
| 285 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
| 286 | $filename = addcslashes('Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name), '"'); |
||
| 287 | |||
| 288 | // Fill in the place names for the starting conditions |
||
| 289 | $startfqpn = []; |
||
| 290 | foreach ($hierarchy as $record) { |
||
| 291 | $startfqpn[] = $record->pl_place; |
||
| 292 | } |
||
| 293 | |||
| 294 | // Generate an array containing the data to output. |
||
| 295 | $places = []; |
||
| 296 | $this->buildExport($parent_id, $startfqpn, $places); |
||
| 297 | |||
| 298 | // Pad all locations to the length of the longest. |
||
| 299 | $max_level = 0; |
||
| 300 | foreach ($places as $place) { |
||
| 301 | $max_level = max($max_level, count($place->fqpn)); |
||
| 302 | } |
||
| 303 | |||
| 304 | $places = array_map(static function (stdClass $place) use ($max_level): array { |
||
| 305 | return array_merge( |
||
| 306 | [count($place->fqpn) - 1], |
||
| 307 | array_pad($place->fqpn, $max_level, ''), |
||
| 308 | [$place->pl_long], |
||
| 309 | [$place->pl_lati], |
||
| 310 | [$place->pl_zoom], |
||
| 311 | [$place->pl_icon] |
||
| 312 | ); |
||
| 313 | }, $places); |
||
| 314 | |||
| 315 | if ($format === 'csv') { |
||
| 316 | // Create the header line for the output file (always English) |
||
| 317 | $header = [ |
||
| 318 | I18N::translate('Level'), |
||
| 319 | ]; |
||
| 320 | |||
| 321 | for ($i = 0; $i < $max_level; $i++) { |
||
| 322 | $header[] = 'Place' . $i; |
||
| 323 | } |
||
| 324 | |||
| 325 | $header[] = 'Longitude'; |
||
| 326 | $header[] = 'Latitude'; |
||
| 327 | $header[] = 'Zoom'; |
||
| 328 | $header[] = 'Icon'; |
||
| 329 | |||
| 330 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
| 331 | } |
||
| 332 | |||
| 333 | return $this->exportGeoJSON($filename . '.geojson', $places, $max_level); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param int $parent_id |
||
| 338 | * @param array<string> $fqpn |
||
| 339 | * @param array<stdClass> $places |
||
| 340 | * |
||
| 341 | * @return void |
||
| 342 | * @throws Exception |
||
| 343 | */ |
||
| 344 | private function buildExport(int $parent_id, array $fqpn, array &$places): void |
||
| 345 | { |
||
| 346 | // Current number of levels. |
||
| 347 | $level = count($fqpn); |
||
| 348 | |||
| 349 | // Data for the next level. |
||
| 350 | $rows = DB::table('placelocation') |
||
| 351 | ->where('pl_parent_id', '=', $parent_id) |
||
| 352 | ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */')) |
||
| 353 | ->get() |
||
| 354 | ->filter(function ($v) { |
||
| 355 | return $v->pl_lati !== '' && $v->pl_long !== ''; |
||
| 356 | }); |
||
| 357 | |||
| 358 | foreach ($rows as $row) { |
||
| 359 | $fqpn[$level] = $row->pl_place; |
||
| 360 | |||
| 361 | $row->fqpn = $fqpn; |
||
| 362 | $row->pl_zoom = (int) $row->pl_zoom; |
||
| 363 | $row->pl_icon = (string) $row->pl_icon; |
||
| 364 | |||
| 365 | $places[] = $row; |
||
| 366 | |||
| 367 | $this->buildExport((int) $row->pl_id, $fqpn, $places); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $filename |
||
| 373 | * @param string[] $columns |
||
| 374 | * @param string[][] $places |
||
| 375 | * |
||
| 376 | * @return ResponseInterface |
||
| 377 | */ |
||
| 378 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
| 379 | { |
||
| 380 | $resource = fopen('php://temp', 'wb+'); |
||
| 381 | |||
| 382 | if ($resource === false) { |
||
| 383 | throw new RuntimeException('Failed to create temporary stream'); |
||
| 384 | } |
||
| 385 | |||
| 386 | fputcsv($resource, $columns, ';'); |
||
| 387 | |||
| 388 | foreach ($places as $place) { |
||
| 389 | fputcsv($resource, $place, ';'); |
||
| 390 | } |
||
| 391 | |||
| 392 | rewind($resource); |
||
| 393 | |||
| 394 | return response(stream_get_contents($resource)) |
||
| 395 | ->withHeader('Content-Type', 'text/csv; charset=utf-8') |
||
| 396 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $filename |
||
| 401 | * @param array $rows |
||
| 402 | * @param int $maxlevel |
||
| 403 | * |
||
| 404 | * @return ResponseInterface |
||
| 405 | */ |
||
| 406 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
| 407 | { |
||
| 408 | $geojson = [ |
||
| 409 | 'type' => 'FeatureCollection', |
||
| 410 | 'features' => [], |
||
| 411 | ]; |
||
| 412 | foreach ($rows as $place) { |
||
| 413 | $fqpn = implode( |
||
| 414 | Gedcom::PLACE_SEPARATOR, |
||
| 415 | array_reverse( |
||
| 416 | array_filter( |
||
| 417 | array_slice($place, 1, $maxlevel) |
||
| 418 | ) |
||
| 419 | ) |
||
| 420 | ); |
||
| 421 | |||
| 422 | $geojson['features'][] = [ |
||
| 423 | 'type' => 'Feature', |
||
| 424 | 'geometry' => [ |
||
| 425 | 'type' => 'Point', |
||
| 426 | 'coordinates' => [ |
||
| 427 | $this->gedcom_service->readLongitude($place[$maxlevel + 1]), |
||
| 428 | $this->gedcom_service->readLatitude($place[$maxlevel + 2]), |
||
| 429 | ], |
||
| 430 | ], |
||
| 431 | 'properties' => [ |
||
| 432 | 'name' => $fqpn, |
||
| 433 | ], |
||
| 434 | ]; |
||
| 435 | } |
||
| 436 | |||
| 437 | return response($geojson) |
||
| 438 | ->withHeader('Content-Type', 'application/vnd.geo+json') |
||
| 439 | ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param ServerRequestInterface $request |
||
| 444 | * |
||
| 445 | * @return ResponseInterface |
||
| 446 | */ |
||
| 447 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
| 470 | ]); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * This function assumes the input file layout is |
||
| 475 | * level followed by a variable number of placename fields |
||
| 476 | * followed by Longitude, Latitude, Zoom & Icon |
||
| 477 | * |
||
| 478 | * @param ServerRequestInterface $request |
||
| 479 | * |
||
| 480 | * @return ResponseInterface |
||
| 481 | * @throws Exception |
||
| 482 | */ |
||
| 483 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
| 599 | } |
||
| 600 | } |
||
| 601 |