| Conditions | 20 |
| Paths | 88 |
| Total Lines | 117 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 84 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 85 | { |
||
| 86 | $source = Validator::parsedBody($request)->isInArray(['client', 'server'])->string('source'); |
||
| 87 | $options = Validator::parsedBody($request)->isInArray(['add', 'addupdate', 'update'])->string('options'); |
||
| 88 | |||
| 89 | $places = []; |
||
| 90 | $url = route(MapDataList::class, ['parent_id' => 0]); |
||
| 91 | $fp = null; |
||
| 92 | |||
| 93 | if ($source === 'client') { |
||
| 94 | $client_file = $request->getUploadedFiles()['client_file'] ?? null; |
||
| 95 | |||
| 96 | if ($client_file === null || $client_file->getError() === UPLOAD_ERR_NO_FILE) { |
||
| 97 | FlashMessages::addMessage(I18N::translate('No file was received.'), 'danger'); |
||
| 98 | |||
| 99 | return redirect(route(MapDataImportPage::class)); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($client_file->getError() !== UPLOAD_ERR_OK) { |
||
| 103 | throw new FileUploadException($client_file); |
||
| 104 | } |
||
| 105 | |||
| 106 | $fp = $client_file->getStream()->detach(); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($source === 'server') { |
||
| 110 | $server_file = Validator::parsedBody($request)->string('server_file'); |
||
| 111 | |||
| 112 | if ($server_file === '') { |
||
| 113 | FlashMessages::addMessage(I18N::translate('No file was received.'), 'danger'); |
||
| 114 | |||
| 115 | return redirect(route(MapDataImportPage::class)); |
||
| 116 | } |
||
| 117 | |||
| 118 | $resource = Registry::filesystem()->data()->readStream('places/' . $server_file); |
||
| 119 | $fp = $this->stream_factory->createStreamFromResource($resource)->detach(); |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($fp === null) { |
||
| 123 | return redirect(route(MapDataImportPage::class)); |
||
| 124 | } |
||
| 125 | |||
| 126 | $string = stream_get_contents($fp); |
||
| 127 | |||
| 128 | // Check the file type |
||
| 129 | if (str_contains($string, 'FeatureCollection')) { |
||
| 130 | $input_array = json_decode($string, false, 512, JSON_THROW_ON_ERROR); |
||
| 131 | |||
| 132 | foreach ($input_array->features as $feature) { |
||
| 133 | $places[] = [ |
||
| 134 | 'latitude' => $feature->geometry->coordinates[1], |
||
| 135 | 'longitude' => $feature->geometry->coordinates[0], |
||
| 136 | 'name' => $feature->properties->name, |
||
| 137 | ]; |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | rewind($fp); |
||
| 141 | while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) { |
||
| 142 | // Skip the header |
||
| 143 | if (!is_numeric($row[0])) { |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | |||
| 147 | $level = (int) $row[0]; |
||
| 148 | $count = count($row); |
||
| 149 | $name = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level))); |
||
| 150 | |||
| 151 | $places[] = [ |
||
| 152 | 'latitude' => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']), |
||
| 153 | 'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']), |
||
| 154 | 'name' => $name |
||
| 155 | ]; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | fclose($fp); |
||
| 160 | |||
| 161 | $added = 0; |
||
| 162 | $updated = 0; |
||
| 163 | |||
| 164 | // Remove places with 0,0 coordinates at lower levels. |
||
| 165 | $callback = static fn (array $place): bool => !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0; |
||
| 166 | |||
| 167 | $places = array_filter($places, $callback); |
||
| 168 | |||
| 169 | foreach ($places as $place) { |
||
| 170 | $location = new PlaceLocation($place['name']); |
||
| 171 | $exists = $location->exists(); |
||
| 172 | |||
| 173 | // Only update existing records |
||
| 174 | if ($options === 'update' && !$exists) { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Only add new records |
||
| 179 | if ($options === 'add' && $exists) { |
||
| 180 | continue; |
||
| 181 | } |
||
| 182 | |||
| 183 | if (!$exists) { |
||
| 184 | $added++; |
||
| 185 | } |
||
| 186 | |||
| 187 | $updated += DB::table('place_location') |
||
| 188 | ->where('id', '=', $location->id()) |
||
| 189 | ->update([ |
||
| 190 | 'latitude' => $place['latitude'], |
||
| 191 | 'longitude' => $place['longitude'], |
||
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | FlashMessages::addMessage( |
||
| 196 | I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)), |
||
| 197 | 'info' |
||
| 198 | ); |
||
| 199 | |||
| 200 | return Registry::responseFactory()->redirectUrl($url); |
||
| 201 | } |
||
| 203 |