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