Passed
Pull Request — master (#3706)
by
unknown
11:53 queued 05:11
created

AutoCompletePlace   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 59
c 1
b 0
f 1
dl 0
loc 100
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B searchGazetteer() 0 60 10
A search() 0 19 2
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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 Fisharebest\Webtrees\Gedcom;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Place;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Fisharebest\Webtrees\Site;
27
use Fisharebest\Webtrees\Tree;
28
use GuzzleHttp\Client;
29
use GuzzleHttp\Exception\RequestException;
30
use Illuminate\Support\Collection;
31
use Psr\Http\Message\ServerRequestInterface;
32
33
use function assert;
34
use function explode;
35
use function json_decode;
36
use function json_last_error;
37
use function rawurlencode;
38
use function trim;
39
40
use const JSON_ERROR_NONE;
41
42
/**
43
 * Autocomplete handler for places
44
 */
45
class AutoCompletePlace extends AbstractAutocompleteHandler
46
{
47
    // Gazetteer urls
48
    private const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search';
49
    private const OPENROUTE_URL = 'https://api.openrouteservice.org/geocode/autocomplete';
50
51
    // Options for fetching files using GuzzleHTTP
52
    private const GUZZLE_OPTIONS = [
53
        'connect_timeout' => 3,
54
        'read_timeout'    => 3,
55
        'timeout'         => 3,
56
    ];
57
58
    protected function search(ServerRequestInterface $request): Collection
59
    {
60
        $tree = $request->getAttribute('tree');
61
        assert($tree instanceof Tree);
62
63
        $query = $request->getAttribute('query');
64
65
        $data = $this->search_service
66
            ->searchPlaces($tree, $query, 0, static::LIMIT)
67
            ->map(static function (Place $place): string {
68
                return $place->gedcomName();
69
            });
70
71
        if ((bool) Site::getPreference('use_gazetteer')) {
72
            $data = $data->concat($this->searchGazetteer($query))->unique();
73
            $data = $data->slice(0, static::LIMIT);
74
        }
75
76
        return new Collection($data);
77
    }
78
79
    /**
80
     *
81
     * @param string $query
82
     *
83
     * @return Collection<string>
84
     */
85
    private function searchGazetteer($query): collection
86
    {
87
        $key          = Site::getPreference('openroute_key');
88
        $data         = new Collection();
89
        $user_service = new UserService();
90
91
        if ($key === '') {
92
            $url   = self::NOMINATIM_URL;
93
            $qry = [
94
                'q'               => rawurlencode($query),
95
                'format'          => 'jsonv2',
96
                'limit'           => static::LIMIT,
97
                'accept-language' => I18N::languageTag(),
98
                'featuretype'     => 'settlement',
99
                'email'           => rawurlencode($user_service->administrators()->first()->email()),
100
            ];
101
        } else {
102
            $url   = self::OPENROUTE_URL;
103
            $qry = [
104
                'api_key' => $key,
105
                'text'    => rawurlencode($query),
106
                'layers'  => 'coarse',
107
                'size'    => static::LIMIT,
108
            ];
109
        }
110
111
        // Read from the URL
112
        $client = new Client();
113
        try {
114
            $json     = $client->get($url, array_merge(self::GUZZLE_OPTIONS, ['query' => $qry]))->getBody()->__toString();
115
            $results  = json_decode($json, false);
116
            if (json_last_error() === JSON_ERROR_NONE) {
117
                if ($key === '') {
118
                    foreach ($results as $result) {
119
                        $data->add($result->display_name);
120
                    }
121
                } else {
122
                    $parts     = Site::getPreference('openroute_parts');
123
                    $parts_arr = explode(',', $parts);
124
                    foreach ($results->features as $result) {
125
                        if ($parts === '') {
126
                            $place = $result->properties->label;
127
                        } else {
128
                            $place = '';
129
                            foreach ($parts_arr as $part) {
130
                                if (isset($result->properties->{$part})) {
131
                                    $place .= $result->properties->{$part} . Gedcom::PLACE_SEPARATOR;
132
                                }
133
                            }
134
                            $place = trim($place, Gedcom::PLACE_SEPARATOR);
135
                        }
136
                        $data->add($place);
137
                    }
138
                }
139
            }
140
        } catch (RequestException $ex) {
141
            // Service down?  Quota exceeded?
142
        }
143
144
        return $data;
145
    }
146
}
147