Total Complexity | 71 |
Total Lines | 842 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 1 | 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 |
||
80 | class LocationController extends AbstractAdminController |
||
81 | { |
||
82 | // Location of files to import |
||
83 | private const PLACES_FOLDER = 'places/'; |
||
84 | |||
85 | /** @var GedcomService */ |
||
86 | private $gedcom_service; |
||
87 | |||
88 | /** @var TreeService */ |
||
89 | private $tree_service; |
||
90 | |||
91 | /** |
||
92 | * Dependency injection. |
||
93 | * |
||
94 | * @param GedcomService $gedcom_service |
||
95 | * @param TreeService $tree_service |
||
96 | */ |
||
97 | public function __construct(GedcomService $gedcom_service, TreeService $tree_service) |
||
98 | { |
||
99 | $this->gedcom_service = $gedcom_service; |
||
100 | $this->tree_service = $tree_service; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param ServerRequestInterface $request |
||
105 | * |
||
106 | * @return ResponseInterface |
||
107 | */ |
||
108 | public function mapData(ServerRequestInterface $request): ResponseInterface |
||
109 | { |
||
110 | $parent_id = (int) ($request->getQueryParams()['parent_id'] ?? 0); |
||
111 | $hierarchy = $this->getHierarchy($parent_id); |
||
112 | $title = I18N::translate('Geographic data'); |
||
113 | $breadcrumbs = [ |
||
114 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
115 | route('map-data') => $title, |
||
116 | ]; |
||
117 | |||
118 | foreach ($hierarchy as $row) { |
||
119 | $breadcrumbs[route('map-data', ['parent_id' => $row->pl_id])] = $row->pl_place; |
||
120 | } |
||
121 | $breadcrumbs[] = array_pop($breadcrumbs); |
||
122 | |||
123 | return $this->viewResponse('admin/locations', [ |
||
124 | 'title' => $title, |
||
125 | 'breadcrumbs' => $breadcrumbs, |
||
126 | 'parent_id' => $parent_id, |
||
127 | 'placelist' => $this->getPlaceListLocation($parent_id), |
||
128 | 'tree_titles' => $this->tree_service->titles(), |
||
129 | ]); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param int $id |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | private function getHierarchy(int $id): array |
||
138 | { |
||
139 | $arr = []; |
||
140 | $fqpn = []; |
||
141 | |||
142 | while ($id !== 0) { |
||
143 | $row = DB::table('placelocation') |
||
144 | ->where('pl_id', '=', $id) |
||
145 | ->first(); |
||
146 | |||
147 | $fqpn[] = $row->pl_place; |
||
148 | $row->fqpn = implode(Gedcom::PLACE_SEPARATOR, $fqpn); |
||
149 | $id = (int) $row->pl_parent_id; |
||
150 | $arr[] = $row; |
||
151 | } |
||
152 | |||
153 | return array_reverse($arr); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Find all of the places in the hierarchy |
||
158 | * |
||
159 | * @param int $id |
||
160 | * |
||
161 | * @return stdClass[] |
||
162 | */ |
||
163 | private function getPlaceListLocation(int $id): array |
||
164 | { |
||
165 | // We know the id of the place in the placelocation table, |
||
166 | // now get the id of the same place in the places table |
||
167 | if ($id === 0) { |
||
168 | $fqpn = ''; |
||
169 | } else { |
||
170 | $hierarchy = $this->getHierarchy($id); |
||
171 | $fqpn = ', ' . $hierarchy[0]->fqpn; |
||
172 | } |
||
173 | |||
174 | $rows = DB::table('placelocation') |
||
175 | ->where('pl_parent_id', '=', $id) |
||
176 | ->orderBy('pl_place') |
||
177 | ->get(); |
||
178 | |||
179 | $list = []; |
||
180 | foreach ($rows as $row) { |
||
181 | // Find/count places without co-ordinates |
||
182 | $children = $this->childLocationStatus((int) $row->pl_id); |
||
183 | $active = $this->isLocationActive($row->pl_place . $fqpn); |
||
184 | |||
185 | if (!$active) { |
||
186 | $badge = 'danger'; |
||
187 | } elseif ((int) $children->no_coord > 0) { |
||
188 | $badge = 'warning'; |
||
189 | } elseif ((int) $children->child_count > 0) { |
||
190 | $badge = 'info'; |
||
191 | } else { |
||
192 | $badge = 'secondary'; |
||
193 | } |
||
194 | |||
195 | $row->child_count = (int) $children->child_count; |
||
196 | $row->badge = $badge; |
||
197 | |||
198 | $list[] = $row; |
||
199 | } |
||
200 | |||
201 | return $list; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * How many children does place have? How many have co-ordinates? |
||
206 | * |
||
207 | * @param int $parent_id |
||
208 | * |
||
209 | * @return stdClass |
||
210 | */ |
||
211 | private function childLocationStatus(int $parent_id): stdClass |
||
212 | { |
||
213 | $prefix = DB::connection()->getTablePrefix(); |
||
214 | |||
215 | $expression = |
||
216 | $prefix . 'p0.pl_place IS NOT NULL AND ' . $prefix . 'p0.pl_lati IS NULL OR ' . |
||
217 | $prefix . 'p1.pl_place IS NOT NULL AND ' . $prefix . 'p1.pl_lati IS NULL OR ' . |
||
218 | $prefix . 'p2.pl_place IS NOT NULL AND ' . $prefix . 'p2.pl_lati IS NULL OR ' . |
||
219 | $prefix . 'p3.pl_place IS NOT NULL AND ' . $prefix . 'p3.pl_lati IS NULL OR ' . |
||
220 | $prefix . 'p4.pl_place IS NOT NULL AND ' . $prefix . 'p4.pl_lati IS NULL OR ' . |
||
221 | $prefix . 'p5.pl_place IS NOT NULL AND ' . $prefix . 'p5.pl_lati IS NULL OR ' . |
||
222 | $prefix . 'p6.pl_place IS NOT NULL AND ' . $prefix . 'p6.pl_lati IS NULL OR ' . |
||
223 | $prefix . 'p7.pl_place IS NOT NULL AND ' . $prefix . 'p7.pl_lati IS NULL OR ' . |
||
224 | $prefix . 'p8.pl_place IS NOT NULL AND ' . $prefix . 'p8.pl_lati IS NULL OR ' . |
||
225 | $prefix . 'p9.pl_place IS NOT NULL AND ' . $prefix . 'p9.pl_lati IS NULL'; |
||
226 | |||
227 | return DB::table('placelocation AS p0') |
||
228 | ->leftJoin('placelocation AS p1', 'p1.pl_parent_id', '=', 'p0.pl_id') |
||
229 | ->leftJoin('placelocation AS p2', 'p2.pl_parent_id', '=', 'p1.pl_id') |
||
230 | ->leftJoin('placelocation AS p3', 'p3.pl_parent_id', '=', 'p2.pl_id') |
||
231 | ->leftJoin('placelocation AS p4', 'p4.pl_parent_id', '=', 'p3.pl_id') |
||
232 | ->leftJoin('placelocation AS p5', 'p5.pl_parent_id', '=', 'p4.pl_id') |
||
233 | ->leftJoin('placelocation AS p6', 'p6.pl_parent_id', '=', 'p5.pl_id') |
||
234 | ->leftJoin('placelocation AS p7', 'p7.pl_parent_id', '=', 'p6.pl_id') |
||
235 | ->leftJoin('placelocation AS p8', 'p8.pl_parent_id', '=', 'p7.pl_id') |
||
236 | ->leftJoin('placelocation AS p9', 'p9.pl_parent_id', '=', 'p8.pl_id') |
||
237 | ->where('p0.pl_parent_id', '=', $parent_id) |
||
238 | ->select([new Expression('COUNT(*) AS child_count'), new Expression('SUM(' . $expression . ') AS no_coord')]) |
||
239 | ->first(); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Is a place name used in any tree? |
||
244 | * |
||
245 | * @param string $place_name |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | private function isLocationActive(string $place_name): bool |
||
250 | { |
||
251 | $places = explode(Gedcom::PLACE_SEPARATOR, $place_name); |
||
252 | |||
253 | $query = DB::table('places AS p0') |
||
254 | ->where('p0.p_place', '=', $places[0]) |
||
255 | ->select(['p0.*']); |
||
256 | |||
257 | array_shift($places); |
||
258 | |||
259 | foreach ($places as $n => $place) { |
||
260 | $query->join('places AS p' . ($n + 1), static function (JoinClause $join) use ($n, $place): void { |
||
261 | $join |
||
262 | ->on('p' . ($n + 1) . '.p_id', '=', 'p' . $n . '.p_parent_id') |
||
263 | ->where('p' . ($n + 1) . '.p_place', '=', $place); |
||
264 | }); |
||
265 | } |
||
266 | |||
267 | return $query->exists(); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param ServerRequestInterface $request |
||
272 | * |
||
273 | * @return ResponseInterface |
||
274 | */ |
||
275 | public function mapDataEdit(ServerRequestInterface $request): ResponseInterface |
||
276 | { |
||
277 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
278 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
279 | $hierarchy = $this->getHierarchy($place_id); |
||
280 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
281 | $location = new Location($fqpn); |
||
282 | |||
283 | if ($location->id() !== 0) { |
||
284 | $lat = $location->latitude(); |
||
285 | $lng = $location->longitude(); |
||
286 | $id = $place_id; |
||
287 | $title = e($location->locationName()); |
||
288 | } else { |
||
289 | // Add a place |
||
290 | $lat = ''; |
||
291 | $lng = ''; |
||
292 | $id = $parent_id; |
||
293 | if ($parent_id === 0) { |
||
294 | // We're at the global level so create a minimal |
||
295 | // place for the page title and breadcrumbs |
||
296 | $title = I18N::translate('World'); |
||
297 | $hierarchy = []; |
||
298 | } else { |
||
299 | $hierarchy = $this->getHierarchy($parent_id); |
||
300 | $tmp = new Location($hierarchy[0]->fqpn); |
||
301 | $title = e($tmp->locationName()); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | $breadcrumbs = [ |
||
306 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
307 | route('map-data') => I18N::translate('Geographic data'), |
||
308 | ]; |
||
309 | |||
310 | foreach ($hierarchy as $row) { |
||
311 | $breadcrumbs[route('map-data', ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
312 | } |
||
313 | |||
314 | if ($place_id === 0) { |
||
315 | $breadcrumbs[] = I18N::translate('Add'); |
||
316 | $title .= ' — ' . I18N::translate('Add'); |
||
317 | } else { |
||
318 | $breadcrumbs[] = I18N::translate('Edit'); |
||
319 | $title .= ' — ' . I18N::translate('Edit'); |
||
320 | } |
||
321 | |||
322 | return $this->viewResponse('admin/location-edit', [ |
||
323 | 'breadcrumbs' => $breadcrumbs, |
||
324 | 'title' => $title, |
||
325 | 'location' => $location, |
||
326 | 'place_id' => $place_id, |
||
327 | 'parent_id' => $parent_id, |
||
328 | 'lat' => $lat, |
||
329 | 'lng' => $lng, |
||
330 | 'data' => $this->mapLocationData($id), |
||
331 | 'provider' => [ |
||
332 | 'url' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
||
333 | 'options' => [ |
||
334 | 'attribution' => '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap</a> contributors', |
||
335 | 'max_zoom' => 19 |
||
336 | ] |
||
337 | ], |
||
338 | ]); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param int $id |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | private function mapLocationData(int $id): array |
||
347 | { |
||
348 | $row = DB::table('placelocation') |
||
349 | ->where('pl_id', '=', $id) |
||
350 | ->first(); |
||
351 | |||
352 | if ($row === null) { |
||
353 | $json = [ |
||
354 | 'zoom' => 2, |
||
355 | 'coordinates' => [ |
||
356 | 0.0, |
||
357 | 0.0, |
||
358 | ], |
||
359 | ]; |
||
360 | } else { |
||
361 | $json = [ |
||
362 | 'zoom' => (int) $row->pl_zoom ?: 2, |
||
363 | 'coordinates' => [ |
||
364 | (float) strtr($row->pl_lati ?? '0', ['N' => '', 'S' => '-', ',' => '.']), |
||
365 | (float) strtr($row->pl_long ?? '0', ['E' => '', 'W' => '-', ',' => '.']), |
||
366 | ], |
||
367 | ]; |
||
368 | } |
||
369 | |||
370 | return $json; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param ServerRequestInterface $request |
||
375 | * |
||
376 | * @return ResponseInterface |
||
377 | */ |
||
378 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
379 | { |
||
380 | $params = (array) $request->getParsedBody(); |
||
381 | |||
382 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
383 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
384 | $lat = round($params['new_place_lati'], 5); // 5 decimal places (locate to within about 1 metre) |
||
385 | $lat = ($lat < 0 ? 'S' : 'N') . abs($lat); |
||
386 | $lng = round($params['new_place_long'], 5); |
||
387 | $lng = ($lng < 0 ? 'W' : 'E') . abs($lng); |
||
388 | $hierarchy = $this->getHierarchy($parent_id); |
||
389 | $level = count($hierarchy); |
||
390 | $icon = $params['icon']; |
||
391 | $zoom = (int) $params['new_zoom_factor']; |
||
392 | |||
393 | if ($place_id === 0) { |
||
394 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
395 | |||
396 | DB::table('placelocation')->insert([ |
||
397 | 'pl_id' => $place_id, |
||
398 | 'pl_parent_id' => $parent_id, |
||
399 | 'pl_level' => $level, |
||
400 | 'pl_place' => $params['new_place_name'], |
||
401 | 'pl_lati' => $lat, |
||
402 | 'pl_long' => $lng, |
||
403 | 'pl_zoom' => $zoom, |
||
404 | 'pl_icon' => $icon, |
||
405 | ]); |
||
406 | } else { |
||
407 | DB::table('placelocation') |
||
408 | ->where('pl_id', '=', $place_id) |
||
409 | ->update([ |
||
410 | 'pl_place' => $params['new_place_name'], |
||
411 | 'pl_lati' => $lat, |
||
412 | 'pl_long' => $lng, |
||
413 | 'pl_zoom' => $zoom, |
||
414 | 'pl_icon' => $icon, |
||
415 | ]); |
||
416 | } |
||
417 | |||
418 | FlashMessages::addMessage( |
||
419 | I18N::translate( |
||
420 | 'The details for “%s” have been updated.', |
||
421 | e($params['new_place_name']) |
||
422 | ), |
||
423 | 'success' |
||
424 | ); |
||
425 | |||
426 | $url = route('map-data', ['parent_id' => $parent_id]); |
||
427 | |||
428 | return redirect($url); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @param ServerRequestInterface $request |
||
433 | * |
||
434 | * @return ResponseInterface |
||
435 | */ |
||
436 | public function mapDataDelete(ServerRequestInterface $request): ResponseInterface |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @param ServerRequestInterface $request |
||
469 | * |
||
470 | * @return ResponseInterface |
||
471 | */ |
||
472 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
473 | { |
||
474 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
475 | $format = $request->getQueryParams()['format']; |
||
476 | $hierarchy = $this->getHierarchy($parent_id); |
||
477 | |||
478 | // Create the file name |
||
479 | // $hierarchy[0] always holds the full placename |
||
480 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
481 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
482 | $filename = 'Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name); |
||
483 | |||
484 | // Fill in the place names for the starting conditions |
||
485 | $startfqpn = []; |
||
486 | foreach ($hierarchy as $record) { |
||
487 | $startfqpn[] = $record->pl_place; |
||
488 | } |
||
489 | |||
490 | // Generate an array containing the data to output. |
||
491 | $places = []; |
||
492 | $this->buildExport($parent_id, $startfqpn, $places); |
||
493 | |||
494 | // Pad all locations to the length of the longest. |
||
495 | $max_level = 0; |
||
496 | foreach ($places as $place) { |
||
497 | $max_level = max($max_level, count($place->fqpn)); |
||
498 | } |
||
499 | |||
500 | $places = array_map(static function (stdClass $place) use ($max_level): array { |
||
501 | return array_merge( |
||
502 | [count($place->fqpn) - 1], |
||
503 | array_pad($place->fqpn, $max_level, ''), |
||
504 | [$place->pl_long], |
||
505 | [$place->pl_lati], |
||
506 | [$place->pl_zoom], |
||
507 | [$place->pl_icon] |
||
508 | ); |
||
509 | }, $places); |
||
510 | |||
511 | if ($format === 'csv') { |
||
512 | // Create the header line for the output file (always English) |
||
513 | $header = [ |
||
514 | I18N::translate('Level'), |
||
515 | ]; |
||
516 | |||
517 | for ($i = 0; $i < $max_level; $i++) { |
||
518 | $header[] = 'Place' . $i; |
||
519 | } |
||
520 | |||
521 | $header[] = 'Longitude'; |
||
522 | $header[] = 'Latitude'; |
||
523 | $header[] = 'Zoom'; |
||
524 | $header[] = 'Icon'; |
||
525 | |||
526 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
527 | } |
||
528 | |||
529 | return $this->exportGeoJSON($filename . '.geojson', $places, $maxlevel); |
||
|
|||
530 | } |
||
531 | |||
532 | /** |
||
533 | * @param int $parent_id |
||
534 | * @param array<string> $fqpn |
||
535 | * @param array<stdClass> $places |
||
536 | * |
||
537 | * @return void |
||
538 | * @throws Exception |
||
539 | */ |
||
540 | private function buildExport(int $parent_id, array $fqpn, array &$places): void |
||
541 | { |
||
542 | // Current number of levels. |
||
543 | $level = count($fqpn); |
||
544 | |||
545 | // Data for the next level. |
||
546 | $rows = DB::table('placelocation') |
||
547 | ->where('pl_parent_id', '=', $parent_id) |
||
548 | ->orderBy('pl_place') |
||
549 | ->get(); |
||
550 | |||
551 | foreach ($rows as $row) { |
||
552 | $fqpn[$level] = $row->pl_place; |
||
553 | |||
554 | $row->fqpn = $fqpn; |
||
555 | $row->pl_long = $row->pl_long ?? 'E0'; |
||
556 | $row->pl_lati = $row->pl_lati ?? 'N0'; |
||
557 | $row->pl_zoom = (int) $row->pl_zoom; |
||
558 | $row->pl_icon = (string) $row->pl_icon; |
||
559 | |||
560 | if ($row->pl_long !== 'E0' || $row->pl_lati !== 'N0') { |
||
561 | $places[] = $row; |
||
562 | } |
||
563 | |||
564 | $this->buildExport((int) $row->pl_id, $fqpn, $places); |
||
565 | } |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @param string $filename |
||
570 | * @param string[] $columns |
||
571 | * @param string[][] $places |
||
572 | * |
||
573 | * @return ResponseInterface |
||
574 | */ |
||
575 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * @param string $filename |
||
596 | * @param array $rows |
||
597 | * @param int $maxlevel |
||
598 | * |
||
599 | * @return ResponseInterface |
||
600 | */ |
||
601 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * @param ServerRequestInterface $request |
||
641 | * |
||
642 | * @return ResponseInterface |
||
643 | */ |
||
644 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
645 | { |
||
646 | $data_filesystem = $request->getAttribute('filesystem.data'); |
||
647 | assert($data_filesystem instanceof FilesystemInterface); |
||
648 | |||
649 | $data_filesystem_name = $request->getAttribute('filesystem.data.name'); |
||
650 | assert(is_string($data_filesystem_name)); |
||
651 | |||
652 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
653 | |||
654 | $files = Collection::make($data_filesystem->listContents('places')) |
||
655 | ->filter(static function (array $metadata): bool { |
||
656 | $extension = strtolower($metadata['extension'] ?? ''); |
||
657 | |||
658 | return $extension === 'csv' || $extension === 'geojson'; |
||
659 | }) |
||
660 | ->map(static function (array $metadata): string { |
||
661 | return $metadata['basename']; |
||
662 | }) |
||
663 | ->sort(); |
||
664 | |||
665 | return $this->viewResponse('admin/map-import-form', [ |
||
666 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
667 | 'title' => I18N::translate('Import geographic data'), |
||
668 | 'parent_id' => $parent_id, |
||
669 | 'files' => $files, |
||
670 | ]); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * This function assumes the input file layout is |
||
675 | * level followed by a variable number of placename fields |
||
676 | * followed by Longitude, Latitude, Zoom & Icon |
||
677 | * |
||
678 | * @param ServerRequestInterface $request |
||
679 | * |
||
680 | * @return ResponseInterface |
||
681 | * @throws Exception |
||
682 | */ |
||
683 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * @param ServerRequestInterface $request |
||
808 | * |
||
809 | * @return ResponseInterface |
||
810 | */ |
||
811 | public function importLocationsFromTree(ServerRequestInterface $request): ResponseInterface |
||
922 | } |
||
923 | } |
||
924 |