Conditions | 22 |
Paths | 328 |
Total Lines | 125 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
83 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
84 | { |
||
85 | $data_filesystem = Registry::filesystem()->data(); |
||
86 | |||
87 | $params = (array) $request->getParsedBody(); |
||
88 | |||
89 | $serverfile = $params['serverfile'] ?? ''; |
||
90 | $options = $params['import-options'] ?? ''; |
||
91 | $clear_database = (bool) ($params['cleardatabase'] ?? false); |
||
92 | $local_file = $request->getUploadedFiles()['localfile'] ?? null; |
||
93 | |||
94 | $places = []; |
||
95 | |||
96 | $url = route(MapDataList::class, ['parent_id' => 0]); |
||
97 | |||
98 | $fp = false; |
||
99 | |||
100 | try { |
||
101 | $file_exists = $data_filesystem->fileExists(MapDataService::PLACES_FOLDER . $serverfile); |
||
102 | } catch (FilesystemException | UnableToCheckFileExistence $ex) { |
||
103 | $file_exists = false; |
||
104 | } |
||
105 | |||
106 | |||
107 | if ($serverfile !== '' && $file_exists) { |
||
108 | // first choice is file on server |
||
109 | try { |
||
110 | $fp = $data_filesystem->readStream(MapDataService::PLACES_FOLDER . $serverfile); |
||
111 | } catch (FilesystemException | UnableToReadFile $ex) { |
||
112 | $fp = false; |
||
113 | } |
||
114 | } elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) { |
||
115 | // 2nd choice is local file |
||
116 | $fp = $local_file->getStream()->detach(); |
||
117 | } |
||
118 | |||
119 | if ($fp === false || $fp === null) { |
||
120 | return redirect($url); |
||
121 | } |
||
122 | |||
123 | $string = stream_get_contents($fp); |
||
124 | |||
125 | // Check the file type |
||
126 | if (str_contains($string, 'FeatureCollection')) { |
||
127 | $input_array = json_decode($string, false, 512, JSON_THROW_ON_ERROR); |
||
128 | |||
129 | foreach ($input_array->features as $feature) { |
||
130 | $places[] = [ |
||
131 | 'latitude' => $feature->geometry->coordinates[1], |
||
132 | 'longitude' => $feature->geometry->coordinates[0], |
||
133 | 'name' => $feature->properties->name, |
||
134 | ]; |
||
135 | } |
||
136 | } else { |
||
137 | rewind($fp); |
||
138 | while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) { |
||
139 | // Skip the header |
||
140 | if (!is_numeric($row[0])) { |
||
141 | continue; |
||
142 | } |
||
143 | |||
144 | $level = (int) $row[0]; |
||
145 | $count = count($row); |
||
146 | $name = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level))); |
||
147 | |||
148 | $places[] = [ |
||
149 | 'latitude' => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']), |
||
150 | 'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']), |
||
151 | 'name' => $name |
||
152 | ]; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | fclose($fp); |
||
157 | |||
158 | if ($clear_database) { |
||
159 | // Child places are deleted via on-delete-cascade... |
||
160 | DB::table('place_location') |
||
161 | ->whereNull('parent_id') |
||
162 | ->delete(); |
||
163 | |||
164 | // Automatically import any new/missing places. |
||
165 | $this->map_data_service->importMissingLocations(); |
||
166 | } |
||
167 | |||
168 | $added = 0; |
||
169 | $updated = 0; |
||
170 | |||
171 | // Remove places with 0,0 coordinates at lower levels. |
||
172 | $places = array_filter($places, static function ($place) { |
||
173 | return !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0; |
||
174 | }); |
||
175 | |||
176 | foreach ($places as $place) { |
||
177 | $location = new PlaceLocation($place['name']); |
||
178 | $exists = $location->exists(); |
||
179 | |||
180 | // Only update existing records |
||
181 | if ($options === 'update' && !$exists) { |
||
182 | continue; |
||
183 | } |
||
184 | |||
185 | // Only add new records |
||
186 | if ($options === 'add' && $exists) { |
||
187 | continue; |
||
188 | } |
||
189 | |||
190 | if (!$exists) { |
||
191 | $added++; |
||
192 | } |
||
193 | |||
194 | $updated += DB::table('place_location') |
||
195 | ->where('id', '=', $location->id()) |
||
196 | ->update([ |
||
197 | 'latitude' => $place['latitude'], |
||
198 | 'longitude' => $place['longitude'], |
||
199 | ]); |
||
200 | } |
||
201 | |||
202 | FlashMessages::addMessage( |
||
203 | I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)), |
||
204 | 'info' |
||
205 | ); |
||
206 | |||
207 | return redirect($url); |
||
208 | } |
||
210 |