Total Complexity | 69 |
Total Lines | 814 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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 |
||
81 | class LocationController extends AbstractAdminController |
||
82 | { |
||
83 | // Location of files to import |
||
84 | private const PLACES_FOLDER = 'places/'; |
||
85 | |||
86 | /** @var GedcomService */ |
||
87 | private $gedcom_service; |
||
88 | |||
89 | /** @var TreeService */ |
||
90 | private $tree_service; |
||
91 | |||
92 | /** |
||
93 | * Dependency injection. |
||
94 | * |
||
95 | * @param GedcomService $gedcom_service |
||
96 | * @param TreeService $tree_service |
||
97 | */ |
||
98 | public function __construct(GedcomService $gedcom_service, TreeService $tree_service) |
||
99 | { |
||
100 | $this->gedcom_service = $gedcom_service; |
||
101 | $this->tree_service = $tree_service; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param ServerRequestInterface $request |
||
106 | * |
||
107 | * @return ResponseInterface |
||
108 | */ |
||
109 | public function mapData(ServerRequestInterface $request): ResponseInterface |
||
110 | { |
||
111 | $parent_id = (int) ($request->getQueryParams()['parent_id'] ?? 0); |
||
112 | $hierarchy = $this->getHierarchy($parent_id); |
||
113 | $title = I18N::translate('Geographic data'); |
||
114 | $breadcrumbs = [ |
||
115 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
116 | route('map-data') => $title, |
||
117 | ]; |
||
118 | |||
119 | foreach ($hierarchy as $row) { |
||
120 | $breadcrumbs[route('map-data', ['parent_id' => $row->pl_id])] = $row->pl_place; |
||
121 | } |
||
122 | $breadcrumbs[] = array_pop($breadcrumbs); |
||
123 | |||
124 | return $this->viewResponse('admin/locations', [ |
||
125 | 'title' => $title, |
||
126 | 'breadcrumbs' => $breadcrumbs, |
||
127 | 'parent_id' => $parent_id, |
||
128 | 'placelist' => $this->getPlaceListLocation($parent_id), |
||
129 | 'tree_titles' => $this->tree_service->titles(), |
||
130 | ]); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param int $id |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | private function getHierarchy(int $id): array |
||
139 | { |
||
140 | $arr = []; |
||
141 | $fqpn = []; |
||
142 | |||
143 | while ($id !== 0) { |
||
144 | $row = DB::table('placelocation') |
||
145 | ->where('pl_id', '=', $id) |
||
146 | ->first(); |
||
147 | |||
148 | $fqpn[] = $row->pl_place; |
||
149 | $row->fqpn = implode(Gedcom::PLACE_SEPARATOR, $fqpn); |
||
150 | $id = (int) $row->pl_parent_id; |
||
151 | $arr[] = $row; |
||
152 | } |
||
153 | |||
154 | return array_reverse($arr); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Find all of the places in the hierarchy |
||
159 | * |
||
160 | * @param int $id |
||
161 | * |
||
162 | * @return stdClass[] |
||
163 | */ |
||
164 | private function getPlaceListLocation(int $id): array |
||
165 | { |
||
166 | // We know the id of the place in the placelocation table, |
||
167 | // now get the id of the same place in the places table |
||
168 | if ($id === 0) { |
||
169 | $fqpn = ''; |
||
170 | } else { |
||
171 | $hierarchy = $this->getHierarchy($id); |
||
172 | $fqpn = ', ' . $hierarchy[0]->fqpn; |
||
173 | } |
||
174 | |||
175 | $rows = DB::table('placelocation') |
||
176 | ->where('pl_parent_id', '=', $id) |
||
177 | ->orderBy('pl_place') |
||
178 | ->get(); |
||
179 | |||
180 | $list = []; |
||
181 | foreach ($rows as $row) { |
||
182 | // Find/count places without co-ordinates |
||
183 | $children = $this->childLocationStatus((int) $row->pl_id); |
||
184 | $active = $this->isLocationActive($row->pl_place . $fqpn); |
||
185 | |||
186 | if (!$active) { |
||
187 | $badge = 'danger'; |
||
188 | } elseif ((int) $children->no_coord > 0) { |
||
189 | $badge = 'warning'; |
||
190 | } elseif ((int) $children->child_count > 0) { |
||
191 | $badge = 'info'; |
||
192 | } else { |
||
193 | $badge = 'secondary'; |
||
194 | } |
||
195 | |||
196 | $row->child_count = (int) $children->child_count; |
||
197 | $row->badge = $badge; |
||
198 | |||
199 | $list[] = $row; |
||
200 | } |
||
201 | |||
202 | return $list; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * How many children does place have? How many have co-ordinates? |
||
207 | * |
||
208 | * @param int $parent_id |
||
209 | * |
||
210 | * @return stdClass |
||
211 | */ |
||
212 | private function childLocationStatus(int $parent_id): stdClass |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Is a place name used in any tree? |
||
245 | * |
||
246 | * @param string $place_name |
||
247 | * |
||
248 | * @return bool |
||
249 | */ |
||
250 | private function isLocationActive(string $place_name): bool |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param ServerRequestInterface $request |
||
273 | * |
||
274 | * @return ResponseInterface |
||
275 | */ |
||
276 | public function mapDataEdit(ServerRequestInterface $request): ResponseInterface |
||
277 | { |
||
278 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
279 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
280 | $hierarchy = $this->getHierarchy($place_id); |
||
281 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
282 | $location = new Location($fqpn); |
||
283 | |||
284 | if ($location->id() !== 0) { |
||
285 | $lat = $location->latitude(); |
||
286 | $lng = $location->longitude(); |
||
287 | $id = $place_id; |
||
288 | $title = e($location->locationName()); |
||
289 | } else { |
||
290 | // Add a place |
||
291 | $lat = ''; |
||
292 | $lng = ''; |
||
293 | $id = $parent_id; |
||
294 | if ($parent_id === 0) { |
||
295 | // We're at the global level so create a minimal |
||
296 | // place for the page title and breadcrumbs |
||
297 | $title = I18N::translate('World'); |
||
298 | $hierarchy = []; |
||
299 | } else { |
||
300 | $hierarchy = $this->getHierarchy($parent_id); |
||
301 | $tmp = new Location($hierarchy[0]->fqpn); |
||
302 | $title = e($tmp->locationName()); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | $breadcrumbs = [ |
||
307 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
308 | route('map-data') => I18N::translate('Geographic data'), |
||
309 | ]; |
||
310 | |||
311 | foreach ($hierarchy as $row) { |
||
312 | $breadcrumbs[route('map-data', ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
313 | } |
||
314 | |||
315 | if ($place_id === 0) { |
||
316 | $breadcrumbs[] = I18N::translate('Add'); |
||
317 | $title .= ' — ' . I18N::translate('Add'); |
||
318 | } else { |
||
319 | $breadcrumbs[] = I18N::translate('Edit'); |
||
320 | $title .= ' — ' . I18N::translate('Edit'); |
||
321 | } |
||
322 | |||
323 | return $this->viewResponse('admin/location-edit', [ |
||
324 | 'breadcrumbs' => $breadcrumbs, |
||
325 | 'title' => $title, |
||
326 | 'location' => $location, |
||
327 | 'place_id' => $place_id, |
||
328 | 'parent_id' => $parent_id, |
||
329 | 'lat' => $lat, |
||
330 | 'lng' => $lng, |
||
331 | 'ref' => $id, |
||
332 | 'data' => $this->mapLocationData($id), |
||
333 | ]); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @param int $id |
||
338 | * |
||
339 | * @return array |
||
340 | */ |
||
341 | private function mapLocationData(int $id): array |
||
342 | { |
||
343 | $row = DB::table('placelocation') |
||
344 | ->where('pl_id', '=', $id) |
||
345 | ->first(); |
||
346 | |||
347 | if ($row === null) { |
||
348 | $json = [ |
||
349 | 'zoom' => 2, |
||
350 | 'coordinates' => [ |
||
351 | 0.0, |
||
352 | 0.0, |
||
353 | ], |
||
354 | ]; |
||
355 | } else { |
||
356 | $json = [ |
||
357 | 'zoom' => (int) $row->pl_zoom ?: 2, |
||
358 | 'coordinates' => [ |
||
359 | (float) strtr($row->pl_lati ?? '0', ['N' => '', 'S' => '-', ',' => '.']), |
||
360 | (float) strtr($row->pl_long ?? '0', ['E' => '', 'W' => '-', ',' => '.']), |
||
361 | ], |
||
362 | ]; |
||
363 | } |
||
364 | |||
365 | return $json; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param ServerRequestInterface $request |
||
370 | * |
||
371 | * @return ResponseInterface |
||
372 | */ |
||
373 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
374 | { |
||
375 | $params = (array) $request->getParsedBody(); |
||
376 | |||
377 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
378 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
379 | $lat = round($params['new_place_lati'], 5); // 5 decimal places (locate to within about 1 metre) |
||
380 | $lat = ($lat < 0 ? 'S' : 'N') . abs($lat); |
||
381 | $lng = round($params['new_place_long'], 5); |
||
382 | $lng = ($lng < 0 ? 'W' : 'E') . abs($lng); |
||
383 | $hierarchy = $this->getHierarchy($parent_id); |
||
384 | $level = count($hierarchy); |
||
385 | $icon = $params['icon']; |
||
386 | $zoom = (int) $params['new_zoom_factor']; |
||
387 | |||
388 | if ($place_id === 0) { |
||
389 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
390 | |||
391 | DB::table('placelocation')->insert([ |
||
392 | 'pl_id' => $place_id, |
||
393 | 'pl_parent_id' => $parent_id, |
||
394 | 'pl_level' => $level, |
||
395 | 'pl_place' => $params['new_place_name'], |
||
396 | 'pl_lati' => $lat, |
||
397 | 'pl_long' => $lng, |
||
398 | 'pl_zoom' => $zoom, |
||
399 | 'pl_icon' => $icon, |
||
400 | ]); |
||
401 | } else { |
||
402 | DB::table('placelocation') |
||
403 | ->where('pl_id', '=', $place_id) |
||
404 | ->update([ |
||
405 | 'pl_place' => $params['new_place_name'], |
||
406 | 'pl_lati' => $lat, |
||
407 | 'pl_long' => $lng, |
||
408 | 'pl_zoom' => $zoom, |
||
409 | 'pl_icon' => $icon, |
||
410 | ]); |
||
411 | } |
||
412 | |||
413 | FlashMessages::addMessage( |
||
414 | I18N::translate( |
||
415 | 'The details for “%s” have been updated.', |
||
416 | e($params['new_place_name']) |
||
417 | ), |
||
418 | 'success' |
||
419 | ); |
||
420 | |||
421 | $url = route('map-data', ['parent_id' => $parent_id]); |
||
422 | |||
423 | return redirect($url); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @param ServerRequestInterface $request |
||
428 | * |
||
429 | * @return ResponseInterface |
||
430 | */ |
||
431 | public function mapDataDelete(ServerRequestInterface $request): ResponseInterface |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @param ServerRequestInterface $request |
||
464 | * |
||
465 | * @return ResponseInterface |
||
466 | */ |
||
467 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
468 | { |
||
469 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
470 | $format = $request->getQueryParams()['format']; |
||
471 | $maxlevel = (int) DB::table('placelocation')->max('pl_level'); |
||
472 | $startfqpn = []; |
||
473 | $hierarchy = $this->getHierarchy($parent_id); |
||
474 | |||
475 | // Create the file name |
||
476 | // $hierarchy[0] always holds the full placename |
||
477 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
478 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
479 | $filename = 'Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name); |
||
480 | |||
481 | // Fill in the place names for the starting conditions |
||
482 | foreach ($hierarchy as $level => $record) { |
||
483 | $startfqpn[$level] = $record->pl_place; |
||
484 | } |
||
485 | $startfqpn = array_pad($startfqpn, $maxlevel + 1, ''); |
||
486 | |||
487 | // Generate an array containing the data to output |
||
488 | $places = []; |
||
489 | $this->buildLevel($parent_id, $startfqpn, $places); |
||
490 | |||
491 | $places = array_filter($places, static function (array $place): bool { |
||
492 | return $place['pl_long'] !== 0.0 && $place['pl_lati'] !== 0.0; |
||
493 | }); |
||
494 | |||
495 | if ($format === 'csv') { |
||
496 | // Create the header line for the output file (always English) |
||
497 | $header = [ |
||
498 | I18N::translate('Level'), |
||
499 | ]; |
||
500 | |||
501 | for ($i = 0; $i <= $maxlevel; $i++) { |
||
502 | $header[] = 'Place' . ($i + 1); |
||
503 | } |
||
504 | |||
505 | $header[] = 'Longitude'; |
||
506 | $header[] = 'Latitude'; |
||
507 | $header[] = 'Zoom'; |
||
508 | $header[] = 'Icon'; |
||
509 | |||
510 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
511 | } |
||
512 | |||
513 | return $this->exportGeoJSON($filename . '.geojson', $places, $maxlevel); |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * @param int $parent_id |
||
518 | * @param array $placename |
||
519 | * @param array $places |
||
520 | * |
||
521 | * @return void |
||
522 | * @throws Exception |
||
523 | */ |
||
524 | private function buildLevel(int $parent_id, array $placename, array &$places): void |
||
525 | { |
||
526 | $level = array_search('', $placename, true); |
||
527 | |||
528 | $rows = DB::table('placelocation') |
||
529 | ->where('pl_parent_id', '=', $parent_id) |
||
530 | ->orderBy('pl_place') |
||
531 | ->get(); |
||
532 | |||
533 | foreach ($rows as $row) { |
||
534 | $index = (int) $row->pl_id; |
||
535 | $placename[$level] = $row->pl_place; |
||
536 | $places[] = array_merge(['pl_level' => $row->pl_level], $placename, ['pl_long' => $row->pl_long, 'pl_lati' => $row->pl_lati, 'pl_zoom' => $row->pl_zoom, 'pl_icon' => $row->pl_icon]); |
||
537 | $this->buildLevel($index, $placename, $places); |
||
538 | } |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @param string $filename |
||
543 | * @param string[] $columns |
||
544 | * @param string[][] $places |
||
545 | * |
||
546 | * @return ResponseInterface |
||
547 | */ |
||
548 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * @param string $filename |
||
569 | * @param array $rows |
||
570 | * @param int $maxlevel |
||
571 | * |
||
572 | * @return ResponseInterface |
||
573 | */ |
||
574 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @param ServerRequestInterface $request |
||
614 | * |
||
615 | * @return ResponseInterface |
||
616 | */ |
||
617 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
618 | { |
||
619 | $data_filesystem = $request->getAttribute('filesystem.data'); |
||
620 | assert($data_filesystem instanceof FilesystemInterface); |
||
621 | |||
622 | $data_filesystem_name = $request->getAttribute('filesystem.data.name'); |
||
623 | assert(is_string($data_filesystem_name)); |
||
624 | |||
625 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
626 | |||
627 | $files = Collection::make($data_filesystem->listContents('places')) |
||
628 | ->filter(static function (array $metadata): bool { |
||
629 | $extension = strtolower($metadata['extension'] ?? ''); |
||
630 | |||
631 | return $extension === 'csv' || $extension === 'geojson'; |
||
632 | }) |
||
633 | ->map(static function (array $metadata): string { |
||
634 | return $metadata['basename']; |
||
635 | }) |
||
636 | ->sort(); |
||
637 | |||
638 | return $this->viewResponse('admin/map-import-form', [ |
||
639 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
640 | 'title' => I18N::translate('Import geographic data'), |
||
641 | 'parent_id' => $parent_id, |
||
642 | 'files' => $files, |
||
643 | ]); |
||
644 | } |
||
645 | |||
646 | /** |
||
647 | * This function assumes the input file layout is |
||
648 | * level followed by a variable number of placename fields |
||
649 | * followed by Longitude, Latitude, Zoom & Icon |
||
650 | * |
||
651 | * @param ServerRequestInterface $request |
||
652 | * |
||
653 | * @return ResponseInterface |
||
654 | * @throws Exception |
||
655 | */ |
||
656 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * @param ServerRequestInterface $request |
||
781 | * |
||
782 | * @return ResponseInterface |
||
783 | */ |
||
784 | public function importLocationsFromTree(ServerRequestInterface $request): ResponseInterface |
||
895 | } |
||
896 | } |
||
897 |