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 |
||
81 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
82 | { |
||
83 | $source = Validator::parsedBody($request)->isInArray(['client', 'server'])->string('source'); |
||
84 | $options = Validator::parsedBody($request)->isInArray(['add', 'addupdate', 'update'])->string('options'); |
||
85 | |||
86 | $places = []; |
||
87 | $url = route(MapDataList::class, ['parent_id' => 0]); |
||
88 | $fp = null; |
||
89 | |||
90 | if ($source === 'client') { |
||
91 | $client_file = $request->getUploadedFiles()['client_file'] ?? null; |
||
92 | |||
93 | if ($client_file === null || $client_file->getError() === UPLOAD_ERR_NO_FILE) { |
||
94 | FlashMessages::addMessage(I18N::translate('No file was received.'), 'danger'); |
||
95 | |||
96 | return redirect(route(MapDataImportPage::class)); |
||
97 | } |
||
98 | |||
99 | if ($client_file->getError() !== UPLOAD_ERR_OK) { |
||
100 | throw new FileUploadException($client_file); |
||
101 | } |
||
102 | |||
103 | $fp = $client_file->getStream()->detach(); |
||
104 | } |
||
105 | |||
106 | if ($source === 'server') { |
||
107 | $server_file = Validator::parsedBody($request)->string('server_file'); |
||
108 | |||
109 | if ($server_file === '') { |
||
110 | FlashMessages::addMessage(I18N::translate('No file was received.'), 'danger'); |
||
111 | |||
112 | return redirect(route(MapDataImportPage::class)); |
||
113 | } |
||
114 | |||
115 | $resource = Registry::filesystem()->data()->readStream('places/' . $server_file); |
||
116 | $fp = $this->stream_factory->createStreamFromResource($resource)->detach(); |
||
117 | } |
||
118 | |||
119 | if ($fp === null) { |
||
120 | return redirect(route(MapDataImportPage::class)); |
||
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 | $added = 0; |
||
159 | $updated = 0; |
||
160 | |||
161 | // Remove places with 0,0 coordinates at lower levels. |
||
162 | $callback = static fn (array $place): bool => !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0; |
||
163 | |||
164 | $places = array_filter($places, $callback); |
||
165 | |||
166 | foreach ($places as $place) { |
||
167 | $location = new PlaceLocation($place['name']); |
||
168 | $exists = $location->exists(); |
||
169 | |||
170 | // Only update existing records |
||
171 | if ($options === 'update' && !$exists) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | // Only add new records |
||
176 | if ($options === 'add' && $exists) { |
||
177 | continue; |
||
178 | } |
||
179 | |||
180 | if (!$exists) { |
||
181 | $added++; |
||
182 | } |
||
183 | |||
184 | $updated += DB::table('place_location') |
||
185 | ->where('id', '=', $location->id()) |
||
186 | ->update([ |
||
187 | 'latitude' => $place['latitude'], |
||
188 | 'longitude' => $place['longitude'], |
||
189 | ]); |
||
190 | } |
||
191 | |||
192 | FlashMessages::addMessage( |
||
193 | I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)), |
||
194 | 'info' |
||
195 | ); |
||
196 | |||
197 | return redirect($url); |
||
198 | } |
||
200 |