| Total Complexity | 56 |
| Total Lines | 525 |
| 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 |
||
| 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'] ?: null; |
||
| 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 |
||
| 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 | // Data for the next level. |
||
| 347 | $rows = DB::table('placelocation') |
||
| 348 | ->where('pl_parent_id', '=', $parent_id) |
||
| 349 | ->whereNotNull('pl_lati') |
||
| 350 | ->whereNotNull('pl_long') |
||
| 351 | ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */')) |
||
| 352 | ->get() |
||
| 353 | ->map(static function (stdClass $x) use ($fqpn) { |
||
| 354 | $x->fqpn = array_merge($fqpn, [$x->pl_place]); |
||
| 355 | $x->pl_zoom = (int) $x->pl_zoom; |
||
| 356 | |||
| 357 | return $x; |
||
| 358 | }); |
||
| 359 | |||
| 360 | foreach ($rows as $row) { |
||
| 361 | $places[] = $row; |
||
| 362 | $this->buildExport((int) $row->pl_id, $row->fqpn, $places); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $filename |
||
| 368 | * @param string[] $columns |
||
| 369 | * @param string[][] $places |
||
| 370 | * |
||
| 371 | * @return ResponseInterface |
||
| 372 | */ |
||
| 373 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $filename |
||
| 396 | * @param array $rows |
||
| 397 | * @param int $maxlevel |
||
| 398 | * |
||
| 399 | * @return ResponseInterface |
||
| 400 | */ |
||
| 401 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param ServerRequestInterface $request |
||
| 439 | * |
||
| 440 | * @return ResponseInterface |
||
| 441 | */ |
||
| 442 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
| 443 | { |
||
| 444 | $data_filesystem = Registry::filesystem()->data(); |
||
| 445 | $data_filesystem_name = Registry::filesystem()->dataName(); |
||
| 446 | |||
| 447 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
| 448 | |||
| 449 | $files = Collection::make($data_filesystem->listContents('places')) |
||
| 450 | ->filter(static function (array $metadata): bool { |
||
| 451 | $extension = strtolower($metadata['extension'] ?? ''); |
||
| 452 | |||
| 453 | return $extension === 'csv' || $extension === 'geojson'; |
||
| 454 | }) |
||
| 455 | ->map(static function (array $metadata): string { |
||
| 456 | return $metadata['basename']; |
||
| 457 | }) |
||
| 458 | ->sort(); |
||
| 459 | |||
| 460 | return $this->viewResponse('admin/map-import-form', [ |
||
| 461 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
| 462 | 'title' => I18N::translate('Import geographic data'), |
||
| 463 | 'parent_id' => $parent_id, |
||
| 464 | 'files' => $files, |
||
| 465 | ]); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * This function assumes the input file layout is |
||
| 470 | * level followed by a variable number of placename fields |
||
| 471 | * followed by Longitude, Latitude, Zoom & Icon |
||
| 472 | * |
||
| 473 | * @param ServerRequestInterface $request |
||
| 474 | * |
||
| 475 | * @return ResponseInterface |
||
| 476 | * @throws Exception |
||
| 477 | */ |
||
| 478 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
| 596 | } |
||
| 597 | } |
||
| 598 |