| Total Complexity | 55 |
| Total Lines | 512 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 | //Used when exporting csv file |
||
| 77 | private const FIELD_DELIMITER = ';'; |
||
| 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 | if ($tmp->latitude() === 0.0 && $tmp->longitude() === 0.0) { |
||
|
|
|||
| 151 | FlashMessages::addMessage(I18N::translate('%s (coordinates [0,0]) cannot have a subordinate place', $title), 'warning'); |
||
| 152 | |||
| 153 | return redirect(route(MapDataList::class, ['parent_id' => 0])); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $breadcrumbs = [ |
||
| 159 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
| 160 | route(MapDataList::class) => I18N::translate('Geographic data'), |
||
| 161 | ]; |
||
| 162 | |||
| 163 | foreach ($hierarchy as $row) { |
||
| 164 | $breadcrumbs[route(MapDataList::class, ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($place_id === 0) { |
||
| 168 | $title .= ' — ' . I18N::translate('Add'); |
||
| 169 | $breadcrumbs[] = I18N::translate('Add'); |
||
| 170 | $latitude = null; |
||
| 171 | $longitude = null; |
||
| 172 | $map_bounds = $parent->boundingRectangle(); |
||
| 173 | } else { |
||
| 174 | $title .= ' — ' . I18N::translate('Edit'); |
||
| 175 | $breadcrumbs[] = I18N::translate('Edit'); |
||
| 176 | $latitude = $location->latitude(); |
||
| 177 | $longitude = $location->longitude(); |
||
| 178 | $map_bounds = $location->boundingRectangle(); |
||
| 179 | } |
||
| 180 | |||
| 181 | // If the current co-ordinates are unknown, leave the input fields empty, |
||
| 182 | // and show a marker in the middle of the map. |
||
| 183 | if ($latitude === null || $longitude === null) { |
||
| 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 |
||
| 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 | if ($format === 'csv') { |
||
| 299 | return $this->exportCSV($filename . '.csv', $places); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $this->exportGeoJSON($filename . '.geojson', $places); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param int $parent_id |
||
| 307 | * @param array<string> $fqpn |
||
| 308 | * @param array<stdClass> $places |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | * @throws Exception |
||
| 312 | */ |
||
| 313 | private function buildExport(int $parent_id, array $fqpn, array &$places): void |
||
| 314 | { |
||
| 315 | // Data for the next level. |
||
| 316 | $rows = DB::table('placelocation') |
||
| 317 | ->where('pl_parent_id', '=', $parent_id) |
||
| 318 | ->whereNotNull('pl_lati') |
||
| 319 | ->whereNotNull('pl_long') |
||
| 320 | ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */')) |
||
| 321 | ->get() |
||
| 322 | ->map(static function (stdClass $x) use ($fqpn) { |
||
| 323 | $x->fqpn = array_merge($fqpn, [$x->pl_place]); |
||
| 324 | $x->pl_zoom = (int) $x->pl_zoom; |
||
| 325 | |||
| 326 | return $x; |
||
| 327 | }); |
||
| 328 | |||
| 329 | foreach ($rows as $row) { |
||
| 330 | $places[] = $row; |
||
| 331 | $this->buildExport((int) $row->pl_id, $row->fqpn, $places); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $filename |
||
| 337 | * @param string[][] $places |
||
| 338 | * |
||
| 339 | * @return ResponseInterface |
||
| 340 | */ |
||
| 341 | private function exportCSV(string $filename, array $places): ResponseInterface |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $filename |
||
| 393 | * @param array $rows |
||
| 394 | * |
||
| 395 | * @return ResponseInterface |
||
| 396 | */ |
||
| 397 | private function exportGeoJSON(string $filename, array $rows): ResponseInterface |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param ServerRequestInterface $request |
||
| 426 | * |
||
| 427 | * @return ResponseInterface |
||
| 428 | */ |
||
| 429 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
| 430 | { |
||
| 431 | $data_filesystem = Registry::filesystem()->data(); |
||
| 432 | $data_filesystem_name = Registry::filesystem()->dataName(); |
||
| 433 | |||
| 434 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 435 | |||
| 436 | $files = Collection::make($data_filesystem->listContents('places')) |
||
| 437 | ->filter(static function (array $metadata): bool { |
||
| 438 | $extension = strtolower($metadata['extension'] ?? ''); |
||
| 439 | |||
| 440 | return $extension === 'csv' || $extension === 'geojson'; |
||
| 441 | }) |
||
| 442 | ->map(static function (array $metadata): string { |
||
| 443 | return $metadata['basename']; |
||
| 444 | }) |
||
| 445 | ->sort(); |
||
| 446 | |||
| 447 | return $this->viewResponse('admin/map-import-form', [ |
||
| 448 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
| 449 | 'title' => I18N::translate('Import geographic data'), |
||
| 450 | 'parent_id' => $parent_id, |
||
| 451 | 'files' => $files, |
||
| 452 | ]); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * This function assumes the input file layout is |
||
| 457 | * level followed by a variable number of placename fields |
||
| 458 | * followed by Longitude, Latitude, Zoom & Icon |
||
| 459 | * |
||
| 460 | * @param ServerRequestInterface $request |
||
| 461 | * |
||
| 462 | * @return ResponseInterface |
||
| 463 | * @throws Exception |
||
| 464 | */ |
||
| 465 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
| 583 | } |
||
| 584 | } |
||
| 585 |