Passed
Pull Request — master (#3706)
by
unknown
06:48
created

AutoCompletePlace::searchGazetteer()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 20
nop 1
dl 0
loc 47
rs 8.4746
c 0
b 0
f 0
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\I18N;
23
use Fisharebest\Webtrees\Place;
24
use Fisharebest\Webtrees\Services\UserService;
25
use Fisharebest\Webtrees\Site;
26
use Fisharebest\Webtrees\Tree;
27
use GuzzleHttp\Client;
28
use GuzzleHttp\Exception\RequestException;
29
use Illuminate\Support\Collection;
30
use Psr\Http\Message\ServerRequestInterface;
31
32
use function assert;
33
use function json_decode;
34
use function json_last_error;
35
use function rawurlencode;
36
37
use const JSON_ERROR_NONE;
38
39
/**
40
 * Autocomplete handler for places
41
 */
42
class AutoCompletePlace extends AbstractAutocompleteHandler
43
{
44
    // Gazetteer urls
45
    private const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search';
46
    private const OPENROUTE_URL = 'https://api.openrouteservice.org/geocode/search';
47
48
    // Max number of search result to return
49
    private const SEARCH_LIMIT = 10;
50
51
    // Options for fetching files using GuzzleHTTP
52
    private const GUZZLE_OPTIONS = [
53
        'connect_timeout' => 3,
54
        'read_timeout'    => 2,
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 ($data->isEmpty() && (bool) Site::getPreference('use_gazetteer')) {
72
            // No place found? Use an external gazetteer
73
            $data = $this->searchGazetteer($query);
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::OPENROUTE_URL;
93
            $qry = [
94
                'api_key' => $key,
95
                'text'    => rawurlencode($query),
96
                'layers'  => 'coarse',
97
                'size'    => self::SEARCH_LIMIT,
98
            ];
99
        } else {
100
            $url   = self::NOMINATIM_URL;
101
            $qry = [
102
                'q'               => rawurlencode($query),
103
                'format'          => 'jsonv2',
104
                'limit'           => self::SEARCH_LIMIT,
105
                'accept-language' => I18N::languageTag(),
106
                'featuretype'     => 'settlement',
107
                'email'           => rawurlencode($user_service->administrators()->first()->email()),
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, 512);
116
            if (json_last_error() === JSON_ERROR_NONE) {
117
                if ($key !== '') {
118
                    foreach ($results->features as $result) {
119
                        $data->add($result->properties->label);
120
                    }
121
                } else {
122
                    foreach ($results as $result) {
123
                        $data->add($result->display_name);
124
                    }
125
                }
126
            }
127
        } catch (RequestException $ex) {
128
            // Service down?  Quota exceeded?
129
        }
130
131
        return $data;
132
    }
133
}
134