MapDataImportAction::handle()   F
last analyzed

Complexity

Conditions 20
Paths 123

Size

Total Lines 114
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 63
nc 123
nop 1
dl 0
loc 114
rs 3.975
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Exception;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Gedcom;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\PlaceLocation;
27
use Fisharebest\Webtrees\Registry;
28
use Fisharebest\Webtrees\Services\MapDataService;
29
use Illuminate\Database\Capsule\Manager as DB;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Message\UploadedFileInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
35
use function array_filter;
36
use function array_reverse;
37
use function array_slice;
38
use function count;
39
use function fclose;
40
use function fgetcsv;
41
use function implode;
42
use function is_numeric;
43
use function json_decode;
44
use function redirect;
45
use function rewind;
46
use function route;
47
use function str_contains;
48
use function stream_get_contents;
49
50
use const UPLOAD_ERR_OK;
51
52
/**
53
 * Import geographic data.
54
 */
55
class MapDataImportAction implements RequestHandlerInterface
56
{
57
    /** @var MapDataService  */
58
    private $map_data_service;
59
60
    /**
61
     * MapDataImportAction constructor.
62
     *
63
     * @param MapDataService $map_data_service
64
     */
65
    public function __construct(MapDataService $map_data_service)
66
    {
67
        $this->map_data_service = $map_data_service;
68
    }
69
70
    /**
71
     * This function assumes the input file layout is
72
     * level followed by a variable number of placename fields
73
     * followed by Longitude, Latitude, Zoom & Icon
74
     *
75
     * @param ServerRequestInterface $request
76
     *
77
     * @return ResponseInterface
78
     * @throws Exception
79
     */
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
            // Automatically import any new/missing places.
151
            $this->map_data_service->importMissingLocations();
152
        }
153
154
        $added   = 0;
155
        $updated = 0;
156
157
        // Remove places with 0,0 coordinates at lower levels.
158
        $places = array_filter($places, static function ($place) {
159
            return !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0;
160
        });
161
162
        foreach ($places as $place) {
163
            $location = new PlaceLocation($place['name']);
164
            $exists   = $location->exists();
165
166
            // Only update existing records
167
            if ($options === 'update' && !$exists) {
168
                continue;
169
            }
170
171
            // Only add new records
172
            if ($options === 'add' && $exists) {
173
                continue;
174
            }
175
176
            if (!$exists) {
177
                $added++;
178
            }
179
180
            $updated += DB::table('place_location')
181
                ->where('id', '=', $location->id())
182
                ->update([
183
                    'latitude'  => $place['latitude'],
184
                    'longitude' => $place['longitude'],
185
                ]);
186
        }
187
188
        FlashMessages::addMessage(
189
            I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)),
190
            'info'
191
        );
192
193
        return redirect($url);
194
    }
195
}
196