Conditions | 20 |
Paths | 123 |
Total Lines | 111 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
67 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
68 | { |
||
69 | $data_filesystem = Registry::filesystem()->data(); |
||
70 | |||
71 | $params = (array) $request->getParsedBody(); |
||
72 | |||
73 | $serverfile = $params['serverfile'] ?? ''; |
||
74 | $options = $params['import-options'] ?? ''; |
||
75 | $clear_database = (bool) ($params['cleardatabase'] ?? false); |
||
76 | $local_file = $request->getUploadedFiles()['localfile'] ?? null; |
||
77 | |||
78 | $places = []; |
||
79 | |||
80 | $url = route(MapDataList::class, ['parent_id' => 0]); |
||
81 | |||
82 | $fp = false; |
||
83 | |||
84 | if ($serverfile !== '' && $data_filesystem->has(MapDataService::PLACES_FOLDER . $serverfile)) { |
||
85 | // first choice is file on server |
||
86 | $fp = $data_filesystem->readStream(MapDataService::PLACES_FOLDER . $serverfile); |
||
87 | } elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) { |
||
88 | // 2nd choice is local file |
||
89 | $fp = $local_file->getStream()->detach(); |
||
90 | } |
||
91 | |||
92 | if ($fp === false || $fp === null) { |
||
93 | return redirect($url); |
||
94 | } |
||
95 | |||
96 | $string = stream_get_contents($fp); |
||
97 | |||
98 | // Check the file type |
||
99 | if (str_contains($string, 'FeatureCollection')) { |
||
100 | $input_array = json_decode($string, false); |
||
101 | |||
102 | foreach ($input_array->features as $feature) { |
||
103 | $places[] = [ |
||
104 | 'latitude' => $feature->geometry->coordinates[1], |
||
105 | 'longitude' => $feature->geometry->coordinates[0], |
||
106 | 'name' => $feature->properties->name, |
||
107 | ]; |
||
108 | } |
||
109 | } else { |
||
110 | rewind($fp); |
||
111 | while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) { |
||
112 | // Skip the header |
||
113 | if (!is_numeric($row[0])) { |
||
114 | continue; |
||
115 | } |
||
116 | |||
117 | $level = (int) $row[0]; |
||
118 | $count = count($row); |
||
119 | $name = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level))); |
||
120 | |||
121 | $places[] = [ |
||
122 | 'latitude' => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']), |
||
123 | 'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']), |
||
124 | 'name' => $name |
||
125 | ]; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | fclose($fp); |
||
130 | |||
131 | if ($clear_database) { |
||
132 | // Child places are deleted via on-delete-cascade... |
||
133 | DB::table('place_location') |
||
134 | ->whereNull('parent_id') |
||
135 | ->delete(); |
||
136 | } |
||
137 | |||
138 | $added = 0; |
||
139 | $updated = 0; |
||
140 | |||
141 | // Remove places with 0,0 coordinates at lower levels. |
||
142 | $places = array_filter($places, static function ($place) { |
||
143 | return !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0; |
||
144 | }); |
||
145 | |||
146 | foreach ($places as $place) { |
||
147 | $location = new PlaceLocation($place['name']); |
||
148 | $exists = $location->exists(); |
||
149 | |||
150 | // Only update existing records |
||
151 | if ($options === 'update' && !$exists) { |
||
152 | continue; |
||
153 | } |
||
154 | |||
155 | // Only add new records |
||
156 | if ($options === 'add' && $exists) { |
||
157 | continue; |
||
158 | } |
||
159 | |||
160 | if (!$exists) { |
||
161 | $added++; |
||
162 | } |
||
163 | |||
164 | $updated += DB::table('place_location') |
||
165 | ->where('id', '=', $location->id()) |
||
166 | ->update([ |
||
167 | 'latitude' => $place['latitude'], |
||
168 | 'longitude' => $place['longitude'], |
||
169 | ]); |
||
170 | } |
||
171 | |||
172 | FlashMessages::addMessage( |
||
173 | I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)), |
||
174 | 'info' |
||
175 | ); |
||
176 | |||
177 | return redirect($url); |
||
178 | } |
||
180 |