Total Complexity | 69 |
Total Lines | 795 |
Duplicated Lines | 0 % |
Changes | 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 |
||
213 | { |
||
214 | $prefix = DB::connection()->getTablePrefix(); |
||
215 | |||
216 | $expression = |
||
217 | $prefix . 'p0.pl_place IS NOT NULL AND ' . $prefix . 'p0.pl_lati IS NULL OR ' . |
||
218 | $prefix . 'p1.pl_place IS NOT NULL AND ' . $prefix . 'p1.pl_lati IS NULL OR ' . |
||
219 | $prefix . 'p2.pl_place IS NOT NULL AND ' . $prefix . 'p2.pl_lati IS NULL OR ' . |
||
220 | $prefix . 'p3.pl_place IS NOT NULL AND ' . $prefix . 'p3.pl_lati IS NULL OR ' . |
||
221 | $prefix . 'p4.pl_place IS NOT NULL AND ' . $prefix . 'p4.pl_lati IS NULL OR ' . |
||
222 | $prefix . 'p5.pl_place IS NOT NULL AND ' . $prefix . 'p5.pl_lati IS NULL OR ' . |
||
223 | $prefix . 'p6.pl_place IS NOT NULL AND ' . $prefix . 'p6.pl_lati IS NULL OR ' . |
||
224 | $prefix . 'p7.pl_place IS NOT NULL AND ' . $prefix . 'p7.pl_lati IS NULL OR ' . |
||
225 | $prefix . 'p8.pl_place IS NOT NULL AND ' . $prefix . 'p8.pl_lati IS NULL OR ' . |
||
226 | $prefix . 'p9.pl_place IS NOT NULL AND ' . $prefix . 'p9.pl_lati IS NULL'; |
||
227 | |||
228 | return DB::table('placelocation AS p0') |
||
229 | ->leftJoin('placelocation AS p1', 'p1.pl_parent_id', '=', 'p0.pl_id') |
||
230 | ->leftJoin('placelocation AS p2', 'p2.pl_parent_id', '=', 'p1.pl_id') |
||
231 | ->leftJoin('placelocation AS p3', 'p3.pl_parent_id', '=', 'p2.pl_id') |
||
232 | ->leftJoin('placelocation AS p4', 'p4.pl_parent_id', '=', 'p3.pl_id') |
||
233 | ->leftJoin('placelocation AS p5', 'p5.pl_parent_id', '=', 'p4.pl_id') |
||
234 | ->leftJoin('placelocation AS p6', 'p6.pl_parent_id', '=', 'p5.pl_id') |
||
235 | ->leftJoin('placelocation AS p7', 'p7.pl_parent_id', '=', 'p6.pl_id') |
||
236 | ->leftJoin('placelocation AS p8', 'p8.pl_parent_id', '=', 'p7.pl_id') |
||
237 | ->leftJoin('placelocation AS p9', 'p9.pl_parent_id', '=', 'p8.pl_id') |
||
238 | ->where('p0.pl_parent_id', '=', $parent_id) |
||
239 | ->select([new Expression('COUNT(*) AS child_count'), new Expression('SUM(' . $expression . ') AS no_coord')]) |
||
240 | ->first(); |
||
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 |
||
251 | { |
||
252 | $places = explode(Gedcom::PLACE_SEPARATOR, $place_name); |
||
253 | |||
254 | $query = DB::table('places AS p0') |
||
255 | ->where('p0.p_place', '=', $places[0]) |
||
256 | ->select(['pl0.*']); |
||
257 | |||
258 | array_shift($places); |
||
259 | |||
260 | foreach ($places as $n => $place) { |
||
261 | $query->join('places AS p' . ($n + 1), static function (JoinClause $join) use ($n, $place): void { |
||
262 | $join |
||
263 | ->on('p' . ($n + 1) . '.p_id', '=', 'p' . $n . '.p_parent_id') |
||
264 | ->where('p' . ($n + 1) . '.p_place', '=', $place); |
||
265 | }); |
||
266 | } |
||
267 | |||
268 | return $query->exists(); |
||
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 | } else { |
||
289 | $lat = ''; |
||
290 | $lng = ''; |
||
291 | $id = $parent_id; |
||
292 | } |
||
293 | |||
294 | $title = e($location->locationName()); |
||
295 | |||
296 | $breadcrumbs = [ |
||
297 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
298 | route('map-data') => I18N::translate('Geographic data'), |
||
299 | ]; |
||
300 | |||
301 | foreach ($hierarchy as $row) { |
||
302 | $breadcrumbs[route('map-data', ['parent_id' => $row->pl_id])] = $row->pl_place; |
||
303 | } |
||
304 | |||
305 | if ($place_id === 0) { |
||
306 | $breadcrumbs[] = I18N::translate('Add'); |
||
307 | } else { |
||
308 | $breadcrumbs[] = I18N::translate('Edit'); |
||
309 | $title .= ' — ' . I18N::translate('Edit'); |
||
310 | } |
||
311 | |||
312 | return $this->viewResponse('admin/location-edit', [ |
||
313 | 'breadcrumbs' => $breadcrumbs, |
||
314 | 'title' => $title, |
||
315 | 'location' => $location, |
||
316 | 'place_id' => $place_id, |
||
317 | 'parent_id' => $parent_id, |
||
318 | 'hierarchy' => $hierarchy, |
||
319 | 'lat' => $lat, |
||
320 | 'lng' => $lng, |
||
321 | 'ref' => $id, |
||
322 | 'data' => $this->mapLocationData($id), |
||
323 | ]); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param int $id |
||
328 | * |
||
329 | * @return array |
||
330 | */ |
||
331 | private function mapLocationData(int $id): array |
||
332 | { |
||
333 | $row = DB::table('placelocation') |
||
334 | ->where('pl_id', '=', $id) |
||
335 | ->first(); |
||
336 | |||
337 | if ($row === null) { |
||
338 | $json = [ |
||
339 | 'zoom' => 2, |
||
340 | 'coordinates' => [ |
||
341 | 0.0, |
||
342 | 0.0, |
||
343 | ], |
||
344 | ]; |
||
345 | } else { |
||
346 | $json = [ |
||
347 | 'zoom' => (int) $row->pl_zoom ?: 2, |
||
348 | 'coordinates' => [ |
||
349 | (float) strtr($row->pl_lati ?? '0', ['N' => '', 'S' => '-', ',' => '.']), |
||
350 | (float) strtr($row->pl_long ?? '0', ['E' => '', 'W' => '-', ',' => '.']), |
||
351 | ], |
||
352 | ]; |
||
353 | } |
||
354 | |||
355 | return $json; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param ServerRequestInterface $request |
||
360 | * |
||
361 | * @return ResponseInterface |
||
362 | */ |
||
363 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
364 | { |
||
365 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
366 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
367 | $lat = round($request->getParsedBody()['new_place_lati'], 5); // 5 decimal places (locate to within about 1 metre) |
||
368 | $lat = ($lat < 0 ? 'S' : 'N') . abs($lat); |
||
369 | $lng = round($request->getParsedBody()['new_place_long'], 5); |
||
370 | $lng = ($lng < 0 ? 'W' : 'E') . abs($lng); |
||
371 | $hierarchy = $this->getHierarchy($parent_id); |
||
372 | $level = count($hierarchy); |
||
373 | $icon = $request->getParsedBody()['icon']; |
||
374 | $zoom = (int) $request->getParsedBody()['new_zoom_factor']; |
||
375 | |||
376 | if ($place_id === 0) { |
||
377 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
378 | |||
379 | DB::table('placelocation')->insert([ |
||
380 | 'pl_id' => $place_id, |
||
381 | 'pl_parent_id' => $parent_id, |
||
382 | 'pl_level' => $level, |
||
383 | 'pl_place' => $request->getParsedBody()['new_place_name'], |
||
384 | 'pl_lati' => $lat, |
||
385 | 'pl_long' => $lng, |
||
386 | 'pl_zoom' => $zoom, |
||
387 | 'pl_icon' => $icon, |
||
388 | ]); |
||
389 | } else { |
||
390 | DB::table('placelocation') |
||
391 | ->where('pl_id', '=', $place_id) |
||
392 | ->update([ |
||
393 | 'pl_place' => $request->getParsedBody()['new_place_name'], |
||
394 | 'pl_lati' => $lat, |
||
395 | 'pl_long' => $lng, |
||
396 | 'pl_zoom' => $zoom, |
||
397 | 'pl_icon' => $icon, |
||
398 | ]); |
||
399 | } |
||
400 | |||
401 | FlashMessages::addMessage( |
||
402 | I18N::translate( |
||
403 | 'The details for “%s” have been updated.', |
||
404 | $request->getParsedBody()['new_place_name'] |
||
405 | ), |
||
406 | 'success' |
||
407 | ); |
||
408 | |||
409 | $url = route('map-data', ['parent_id' => $parent_id]); |
||
410 | |||
411 | return redirect($url); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param ServerRequestInterface $request |
||
416 | * |
||
417 | * @return ResponseInterface |
||
418 | */ |
||
419 | public function mapDataDelete(ServerRequestInterface $request): ResponseInterface |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @param ServerRequestInterface $request |
||
452 | * |
||
453 | * @return ResponseInterface |
||
454 | */ |
||
455 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
456 | { |
||
457 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
458 | $format = $request->getQueryParams()['format']; |
||
459 | $maxlevel = (int) DB::table('placelocation')->max('pl_level'); |
||
460 | $startfqpn = []; |
||
461 | $hierarchy = $this->getHierarchy($parent_id); |
||
462 | |||
463 | // Create the file name |
||
464 | // $hierarchy[0] always holds the full placename |
||
465 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
466 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
467 | $filename = 'Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name); |
||
468 | |||
469 | // Fill in the place names for the starting conditions |
||
470 | foreach ($hierarchy as $level => $record) { |
||
471 | $startfqpn[$level] = $record->pl_place; |
||
472 | } |
||
473 | $startfqpn = array_pad($startfqpn, $maxlevel + 1, ''); |
||
474 | |||
475 | // Generate an array containing the data to output |
||
476 | $places = []; |
||
477 | $this->buildLevel($parent_id, $startfqpn, $places); |
||
478 | |||
479 | $places = array_filter($places, static function (array $place): bool { |
||
480 | return $place['pl_long'] !== 0.0 && $place['pl_lati'] !== 0.0; |
||
481 | }); |
||
482 | |||
483 | if ($format === 'csv') { |
||
484 | // Create the header line for the output file (always English) |
||
485 | $header = [ |
||
486 | I18N::translate('Level'), |
||
487 | ]; |
||
488 | |||
489 | for ($i = 0; $i <= $maxlevel; $i++) { |
||
490 | $header[] = 'Place' . ($i + 1); |
||
491 | } |
||
492 | |||
493 | $header[] = 'Longitude'; |
||
494 | $header[] = 'Latitude'; |
||
495 | $header[] = 'Zoom'; |
||
496 | $header[] = 'Icon'; |
||
497 | |||
498 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
499 | } |
||
500 | |||
501 | return $this->exportGeoJSON($filename . '.geojson', $places, $maxlevel); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param int $parent_id |
||
506 | * @param array $placename |
||
507 | * @param array $places |
||
508 | * |
||
509 | * @return void |
||
510 | * @throws Exception |
||
511 | */ |
||
512 | private function buildLevel(int $parent_id, array $placename, array &$places): void |
||
513 | { |
||
514 | $level = array_search('', $placename, true); |
||
515 | |||
516 | $rows = DB::table('placelocation') |
||
517 | ->where('pl_parent_id', '=', $parent_id) |
||
518 | ->orderBy('pl_place') |
||
519 | ->get(); |
||
520 | |||
521 | foreach ($rows as $row) { |
||
522 | $index = (int) $row->pl_id; |
||
523 | $placename[$level] = $row->pl_place; |
||
524 | $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]); |
||
525 | $this->buildLevel($index, $placename, $places); |
||
526 | } |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * @param string $filename |
||
531 | * @param string[] $columns |
||
532 | * @param string[][] $places |
||
533 | * |
||
534 | * @return ResponseInterface |
||
535 | */ |
||
536 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * @param string $filename |
||
557 | * @param array $rows |
||
558 | * @param int $maxlevel |
||
559 | * |
||
560 | * @return ResponseInterface |
||
561 | */ |
||
562 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * @param ServerRequestInterface $request |
||
602 | * |
||
603 | * @return ResponseInterface |
||
604 | */ |
||
605 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
606 | { |
||
607 | $data_filesystem = $request->getAttribute('filesystem.data'); |
||
608 | assert($data_filesystem instanceof FilesystemInterface); |
||
609 | |||
610 | $data_filesystem_name = $request->getAttribute('filesystem.data.name'); |
||
611 | assert(is_string($data_filesystem_name)); |
||
612 | |||
613 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
614 | |||
615 | $files = Collection::make($data_filesystem->listContents('places')) |
||
616 | ->filter(static function (array $metadata): bool { |
||
617 | return $metadata['extension'] === 'csv' || $metadata['extension'] === 'geojson'; |
||
618 | }) |
||
619 | ->map(static function (array $metadata): string { |
||
620 | return $metadata['basename']; |
||
621 | }) |
||
622 | ->sort(); |
||
623 | |||
624 | return $this->viewResponse('admin/map-import-form', [ |
||
625 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
626 | 'title' => I18N::translate('Import geographic data'), |
||
627 | 'parent_id' => $parent_id, |
||
628 | 'files' => $files, |
||
629 | ]); |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * This function assumes the input file layout is |
||
634 | * level followed by a variable number of placename fields |
||
635 | * followed by Longitude, Latitude, Zoom & Icon |
||
636 | * |
||
637 | * @param ServerRequestInterface $request |
||
638 | * |
||
639 | * @return ResponseInterface |
||
640 | * @throws Exception |
||
641 | */ |
||
642 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
760 | } |
||
761 | |||
762 | /** |
||
763 | * @param ServerRequestInterface $request |
||
764 | * |
||
765 | * @return ResponseInterface |
||
766 | */ |
||
767 | public function importLocationsFromTree(ServerRequestInterface $request): ResponseInterface |
||
876 | } |
||
877 | } |
||
878 |