Completed
Push — master ( bee36e...8d4065 )
by Greg
14:04
created

AutoCompletePlace::search()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 25
c 1
b 0
f 1
nc 10
nop 1
dl 0
loc 41
rs 8.5866
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 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 <http://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\Site;
25
use Fisharebest\Webtrees\Tree;
26
use GuzzleHttp\Client;
27
use GuzzleHttp\Exception\RequestException;
28
use Illuminate\Support\Collection;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
use function assert;
32
use function is_array;
33
use function json_decode;
34
use function rawurlencode;
35
36
/**
37
 * Autocomplete handler for places
38
 */
39
class AutoCompletePlace extends AbstractAutocompleteHandler
40
{
41
    // Options for fetching files using GuzzleHTTP
42
    private const GUZZLE_OPTIONS = [
43
        'connect_timeout' => 3,
44
        'read_timeout'    => 3,
45
        'timeout'         => 3,
46
    ];
47
48
    protected function search(ServerRequestInterface $request): Collection
49
    {
50
        $tree = $request->getAttribute('tree');
51
        assert($tree instanceof Tree);
52
53
        $query = $request->getAttribute('query');
54
55
        $data = $this->search_service
56
            ->searchPlaces($tree, $query, 0, static::LIMIT)
57
            ->map(static function (Place $place): string {
58
                return $place->gedcomName();
59
            });
60
61
        $geonames = Site::getPreference('geonames');
62
63
        if ($data->isEmpty() && $geonames !== '') {
64
            // No place found? Use an external gazetteer
65
            $url =
66
                'https://secure.geonames.org/searchJSON' .
67
                '?name_startsWith=' . rawurlencode($query) .
68
                '&lang=' . I18N::languageTag() .
69
                '&fcode=CMTY&fcode=ADM4&fcode=PPL&fcode=PPLA&fcode=PPLC' .
70
                '&style=full' .
71
                '&username=' . rawurlencode($geonames);
72
73
            // Read from the URL
74
            $client = new Client();
75
            try {
76
                $json   = $client->get($url, self::GUZZLE_OPTIONS)->getBody()->__toString();
77
                $places = json_decode($json, true);
78
                if (isset($places['geonames']) && is_array($places['geonames'])) {
79
                    foreach ($places['geonames'] as $k => $place) {
80
                        $data->add($place['name'] . ', ' . $place['adminName2'] . ', ' . $place['adminName1'] . ', ' . $place['countryName']);
81
                    }
82
                }
83
            } catch (RequestException $ex) {
84
                // Service down?  Quota exceeded?
85
            }
86
        }
87
88
        return new Collection($data);
89
    }
90
}
91