Total Complexity | 56 |
Total Lines | 524 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 | /** @var GedcomService */ |
||
77 | private $gedcom_service; |
||
78 | |||
79 | /** |
||
80 | * Dependency injection. |
||
81 | * |
||
82 | * @param GedcomService $gedcom_service |
||
83 | */ |
||
84 | public function __construct(GedcomService $gedcom_service) |
||
85 | { |
||
86 | $this->gedcom_service = $gedcom_service; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param int $id |
||
91 | * |
||
92 | * @return array<stdClass> |
||
93 | */ |
||
94 | private function getHierarchy(int $id): array |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param ServerRequestInterface $request |
||
118 | * |
||
119 | * @return ResponseInterface |
||
120 | */ |
||
121 | public function mapDataEdit(ServerRequestInterface $request): ResponseInterface |
||
122 | { |
||
123 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
124 | $hierarchy = $this->getHierarchy($parent_id); |
||
125 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
126 | $parent = new PlaceLocation($fqpn); |
||
127 | |||
128 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
129 | $hierarchy = $this->getHierarchy($place_id); |
||
130 | $fqpn = $hierarchy === [] ? '' : $hierarchy[0]->fqpn; |
||
131 | $location = new PlaceLocation($fqpn); |
||
132 | |||
133 | if ($location->id() !== 0) { |
||
134 | $title = e($location->locationName()); |
||
135 | } else { |
||
136 | // Add a place |
||
137 | if ($parent_id === 0) { |
||
138 | // We're at the global level so create a minimal |
||
139 | // place for the page title and breadcrumbs |
||
140 | $title = I18N::translate('World'); |
||
141 | $hierarchy = []; |
||
142 | } else { |
||
143 | $hierarchy = $this->getHierarchy($parent_id); |
||
144 | $tmp = new PlaceLocation($hierarchy[0]->fqpn); |
||
145 | $title = e($tmp->locationName()); |
||
146 | |||
147 | if ($tmp->latitude() === 0.0 && $tmp->longitude() === 0.0) { |
||
|
|||
148 | FlashMessages::addMessage(I18N::translate('%s (coordinates [0,0]) cannot have a subordinate place', $title), 'warning'); |
||
149 | |||
150 | return redirect(route(MapDataList::class, ['parent_id' => 0])); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $breadcrumbs = [ |
||
156 | route(ControlPanel::class) => I18N::translate('Control panel'), |
||
157 | route(MapDataList::class) => I18N::translate('Geographic data'), |
||
158 | ]; |
||
159 | |||
160 | foreach ($hierarchy as $row) { |
||
161 | $breadcrumbs[route(MapDataList::class, ['parent_id' => $row->pl_id])] = e($row->pl_place); |
||
162 | } |
||
163 | |||
164 | if ($place_id === 0) { |
||
165 | $title .= ' — ' . I18N::translate('Add'); |
||
166 | $breadcrumbs[] = I18N::translate('Add'); |
||
167 | $latitude = null; |
||
168 | $longitude = null; |
||
169 | $map_bounds = $parent->boundingRectangle(); |
||
170 | } else { |
||
171 | $title .= ' — ' . I18N::translate('Edit'); |
||
172 | $breadcrumbs[] = I18N::translate('Edit'); |
||
173 | $latitude = $location->latitude(); |
||
174 | $longitude = $location->longitude(); |
||
175 | $map_bounds = $location->boundingRectangle(); |
||
176 | } |
||
177 | |||
178 | // If the current co-ordinates are unknown, leave the input fields empty, |
||
179 | // and show a marker in the middle of the map. |
||
180 | if ($latitude === null || $longitude === null) { |
||
181 | $marker_position = [ |
||
182 | ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0, |
||
183 | ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0, |
||
184 | ]; |
||
185 | } else { |
||
186 | $marker_position = [$latitude, $longitude]; |
||
187 | } |
||
188 | |||
189 | return $this->viewResponse('admin/location-edit', [ |
||
190 | 'breadcrumbs' => $breadcrumbs, |
||
191 | 'title' => $title, |
||
192 | 'location' => $location, |
||
193 | 'latitude' => $latitude, |
||
194 | 'longitude' => $longitude, |
||
195 | 'map_bounds' => $map_bounds, |
||
196 | 'marker_position' => $marker_position, |
||
197 | 'parent' => $parent, |
||
198 | 'level' => $parent_id, |
||
199 | 'provider' => [ |
||
200 | 'url' => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
||
201 | 'options' => [ |
||
202 | 'attribution' => '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap</a> contributors', |
||
203 | 'max_zoom' => 19 |
||
204 | ] |
||
205 | ], |
||
206 | ]); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param ServerRequestInterface $request |
||
211 | * |
||
212 | * @return ResponseInterface |
||
213 | */ |
||
214 | public function mapDataSave(ServerRequestInterface $request): ResponseInterface |
||
215 | { |
||
216 | $params = (array) $request->getParsedBody(); |
||
217 | |||
218 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
219 | $place_id = (int) $request->getQueryParams()['place_id']; |
||
220 | $lat = $this->gedcom_service->writeLatitude((float) $params['new_place_lati']); |
||
221 | $lng = $this->gedcom_service->writeLongitude((float) $params['new_place_long']); |
||
222 | $hierarchy = $this->getHierarchy($parent_id); |
||
223 | $level = count($hierarchy); |
||
224 | $icon = $params['icon'] ?: null; |
||
225 | $zoom = (int) $params['new_zoom_factor']; |
||
226 | |||
227 | if ($parent_id > 0 && $lat === 'N0' && $lng === 'E0') { |
||
228 | FlashMessages::addMessage(I18N::translate('Location [0,0] cannot be subordinate to another place'), 'warning'); |
||
229 | } else { |
||
230 | if ($place_id === 0) { |
||
231 | $place_id = 1 + (int) DB::table('placelocation')->max('pl_id'); |
||
232 | |||
233 | DB::table('placelocation')->insert([ |
||
234 | 'pl_id' => $place_id, |
||
235 | 'pl_parent_id' => $parent_id, |
||
236 | 'pl_level' => $level, |
||
237 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
238 | 'pl_lati' => $lat, |
||
239 | 'pl_long' => $lng, |
||
240 | 'pl_zoom' => $zoom, |
||
241 | 'pl_icon' => $icon, |
||
242 | ]); |
||
243 | } else { |
||
244 | DB::table('placelocation') |
||
245 | ->where('pl_id', '=', $place_id) |
||
246 | ->update([ |
||
247 | 'pl_place' => mb_substr($params['new_place_name'], 0, 120), |
||
248 | 'pl_lati' => $lat, |
||
249 | 'pl_long' => $lng, |
||
250 | 'pl_zoom' => $zoom, |
||
251 | 'pl_icon' => $icon, |
||
252 | ]); |
||
253 | } |
||
254 | |||
255 | FlashMessages::addMessage( |
||
256 | I18N::translate( |
||
257 | 'The details for “%s” have been updated.', |
||
258 | e($params['new_place_name']) |
||
259 | ), |
||
260 | 'success' |
||
261 | ); |
||
262 | } |
||
263 | $url = route(MapDataList::class, ['parent_id' => $parent_id]); |
||
264 | |||
265 | return redirect($url); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param ServerRequestInterface $request |
||
270 | * |
||
271 | * @return ResponseInterface |
||
272 | */ |
||
273 | public function exportLocations(ServerRequestInterface $request): ResponseInterface |
||
274 | { |
||
275 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
276 | $format = $request->getQueryParams()['format']; |
||
277 | $hierarchy = $this->getHierarchy($parent_id); |
||
278 | |||
279 | // Create the file name |
||
280 | // $hierarchy[0] always holds the full placename |
||
281 | $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn; |
||
282 | $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name); |
||
283 | $filename = addcslashes('Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name), '"'); |
||
284 | |||
285 | // Fill in the place names for the starting conditions |
||
286 | $startfqpn = []; |
||
287 | foreach ($hierarchy as $record) { |
||
288 | $startfqpn[] = $record->pl_place; |
||
289 | } |
||
290 | |||
291 | // Generate an array containing the data to output. |
||
292 | $places = []; |
||
293 | $this->buildExport($parent_id, $startfqpn, $places); |
||
294 | |||
295 | // Pad all locations to the length of the longest. |
||
296 | $max_level = 0; |
||
297 | foreach ($places as $place) { |
||
298 | $max_level = max($max_level, count($place->fqpn)); |
||
299 | } |
||
300 | |||
301 | $places = array_map(static function (stdClass $place) use ($max_level): array { |
||
302 | return array_merge( |
||
303 | [count($place->fqpn) - 1], |
||
304 | array_pad($place->fqpn, $max_level, ''), |
||
305 | [$place->pl_long], |
||
306 | [$place->pl_lati], |
||
307 | [$place->pl_zoom], |
||
308 | [$place->pl_icon] |
||
309 | ); |
||
310 | }, $places); |
||
311 | |||
312 | if ($format === 'csv') { |
||
313 | // Create the header line for the output file (always English) |
||
314 | $header = [ |
||
315 | 'Level', |
||
316 | ]; |
||
317 | |||
318 | for ($i = 0; $i < $max_level; $i++) { |
||
319 | $header[] = 'Place' . $i; |
||
320 | } |
||
321 | |||
322 | $header[] = 'Longitude'; |
||
323 | $header[] = 'Latitude'; |
||
324 | $header[] = 'Zoom'; |
||
325 | $header[] = 'Icon'; |
||
326 | |||
327 | return $this->exportCSV($filename . '.csv', $header, $places); |
||
328 | } |
||
329 | |||
330 | return $this->exportGeoJSON($filename . '.geojson', $places, $max_level); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @param int $parent_id |
||
335 | * @param array<string> $fqpn |
||
336 | * @param array<stdClass> $places |
||
337 | * |
||
338 | * @return void |
||
339 | * @throws Exception |
||
340 | */ |
||
341 | private function buildExport(int $parent_id, array $fqpn, array &$places): void |
||
342 | { |
||
343 | // Current number of levels. |
||
344 | $level = count($fqpn); |
||
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 | |||
354 | foreach ($rows as $row) { |
||
355 | $fqpn[$level] = $row->pl_place; |
||
356 | |||
357 | $row->fqpn = $fqpn; |
||
358 | $row->pl_zoom = (int) $row->pl_zoom; |
||
359 | $row->pl_icon = (string) $row->pl_icon; |
||
360 | |||
361 | $places[] = $row; |
||
362 | |||
363 | $this->buildExport((int) $row->pl_id, $fqpn, $places); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * @param string $filename |
||
369 | * @param string[] $columns |
||
370 | * @param string[][] $places |
||
371 | * |
||
372 | * @return ResponseInterface |
||
373 | */ |
||
374 | private function exportCSV(string $filename, array $columns, array $places): ResponseInterface |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @param string $filename |
||
397 | * @param array $rows |
||
398 | * @param int $maxlevel |
||
399 | * |
||
400 | * @return ResponseInterface |
||
401 | */ |
||
402 | private function exportGeoJSON(string $filename, array $rows, int $maxlevel): ResponseInterface |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @param ServerRequestInterface $request |
||
440 | * |
||
441 | * @return ResponseInterface |
||
442 | */ |
||
443 | public function importLocations(ServerRequestInterface $request): ResponseInterface |
||
444 | { |
||
445 | $data_filesystem = Registry::filesystem()->data(); |
||
446 | $data_filesystem_name = Registry::filesystem()->dataName(); |
||
447 | |||
448 | $parent_id = (int) $request->getQueryParams()['parent_id']; |
||
449 | |||
450 | $files = Collection::make($data_filesystem->listContents('places')) |
||
451 | ->filter(static function (array $metadata): bool { |
||
452 | $extension = strtolower($metadata['extension'] ?? ''); |
||
453 | |||
454 | return $extension === 'csv' || $extension === 'geojson'; |
||
455 | }) |
||
456 | ->map(static function (array $metadata): string { |
||
457 | return $metadata['basename']; |
||
458 | }) |
||
459 | ->sort(); |
||
460 | |||
461 | return $this->viewResponse('admin/map-import-form', [ |
||
462 | 'place_folder' => $data_filesystem_name . self::PLACES_FOLDER, |
||
463 | 'title' => I18N::translate('Import geographic data'), |
||
464 | 'parent_id' => $parent_id, |
||
465 | 'files' => $files, |
||
466 | ]); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * This function assumes the input file layout is |
||
471 | * level followed by a variable number of placename fields |
||
472 | * followed by Longitude, Latitude, Zoom & Icon |
||
473 | * |
||
474 | * @param ServerRequestInterface $request |
||
475 | * |
||
476 | * @return ResponseInterface |
||
477 | * @throws Exception |
||
478 | */ |
||
479 | public function importLocationsAction(ServerRequestInterface $request): ResponseInterface |
||
595 | } |
||
596 | } |
||
597 |