xref: /webtrees/app/Module/ModuleMapAutocompleteTrait.php (revision 28d026ad36e53e5af3ffb5b483ee815e04b18ecf)
1c9c6f2ecSGreg Roach<?php
2c9c6f2ecSGreg Roach
3c9c6f2ecSGreg Roach/**
4c9c6f2ecSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6c9c6f2ecSGreg Roach * This program is free software: you can redistribute it and/or modify
7c9c6f2ecSGreg Roach * it under the terms of the GNU General Public License as published by
8c9c6f2ecSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9c9c6f2ecSGreg Roach * (at your option) any later version.
10c9c6f2ecSGreg Roach * This program is distributed in the hope that it will be useful,
11c9c6f2ecSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c9c6f2ecSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13c9c6f2ecSGreg Roach * GNU General Public License for more details.
14c9c6f2ecSGreg Roach * You should have received a copy of the GNU General Public License
15c9c6f2ecSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16c9c6f2ecSGreg Roach */
17c9c6f2ecSGreg Roach
18c9c6f2ecSGreg Roachdeclare(strict_types=1);
19c9c6f2ecSGreg Roach
20c9c6f2ecSGreg Roachnamespace Fisharebest\Webtrees\Module;
21c9c6f2ecSGreg Roach
22c9c6f2ecSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23c9c6f2ecSGreg Roachuse Fisharebest\Webtrees\Registry;
24c9c6f2ecSGreg Roachuse GuzzleHttp\Client;
254022f21aSGreg Roachuse GuzzleHttp\Exception\GuzzleException;
26c9c6f2ecSGreg Roachuse GuzzleHttp\Psr7\Request;
27c9c6f2ecSGreg Roachuse Psr\Http\Message\RequestInterface;
28c9c6f2ecSGreg Roachuse Psr\Http\Message\ResponseInterface;
29c9c6f2ecSGreg Roach
3010e06497SGreg Roachuse function strlen;
3110e06497SGreg Roach
32c9c6f2ecSGreg Roach/**
33c9c6f2ecSGreg Roach * Trait ModuleMapAutocompleteTrait - default implementation of ModuleMapAutocompleteInterface
34c9c6f2ecSGreg Roach */
35c9c6f2ecSGreg Roachtrait ModuleMapAutocompleteTrait
36c9c6f2ecSGreg Roach{
37c9c6f2ecSGreg Roach    /**
38bfed30e4SGreg Roach     * A unique internal name for this module (based on the installation folder).
39bfed30e4SGreg Roach     *
40bfed30e4SGreg Roach     * @return string
41bfed30e4SGreg Roach     */
42bfed30e4SGreg Roach    abstract public function name(): string;
43bfed30e4SGreg Roach
44bfed30e4SGreg Roach    /**
45c9c6f2ecSGreg Roach     * @param string $place
46c9c6f2ecSGreg Roach     *
47c9c6f2ecSGreg Roach     * @return array<string>
48c9c6f2ecSGreg Roach     */
49c9c6f2ecSGreg Roach    public function searchPlaceNames(string $place): array
50c9c6f2ecSGreg Roach    {
51c9c6f2ecSGreg Roach        if (strlen($place) <= 2) {
52c9c6f2ecSGreg Roach            return [];
53c9c6f2ecSGreg Roach        }
54c9c6f2ecSGreg Roach
55c9c6f2ecSGreg Roach        $key   = $this->name() . $place;
56c9c6f2ecSGreg Roach        $cache = Registry::cache()->file();
57c9c6f2ecSGreg Roach        $ttl   = 86400;
58c9c6f2ecSGreg Roach
59c9c6f2ecSGreg Roach        try {
60c9c6f2ecSGreg Roach            return $cache->remember($key, function () use ($place) {
61c9c6f2ecSGreg Roach                $request = $this->createPlaceNameSearchRequest($place);
62c9c6f2ecSGreg Roach
63c9c6f2ecSGreg Roach                $client = new Client([
64c9c6f2ecSGreg Roach                    'timeout' => 3,
65c9c6f2ecSGreg Roach                ]);
66c9c6f2ecSGreg Roach
67c9c6f2ecSGreg Roach                $response = $client->send($request);
68c9c6f2ecSGreg Roach
69c9c6f2ecSGreg Roach                if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
70c9c6f2ecSGreg Roach                    return $this->parsePlaceNameSearchResponse($response);
71c9c6f2ecSGreg Roach                }
72c9c6f2ecSGreg Roach
73c9c6f2ecSGreg Roach                return [];
74c9c6f2ecSGreg Roach            }, $ttl);
75*28d026adSGreg Roach        } catch (GuzzleException) {
76c9c6f2ecSGreg Roach            // Service down?  Quota exceeded?
77c9c6f2ecSGreg Roach            // Don't try for another hour.
78c9c6f2ecSGreg Roach            $cache->remember($key, fn () => [], 3600);
79928b8592SGreg Roach
80c9c6f2ecSGreg Roach            return [];
81c9c6f2ecSGreg Roach        }
82c9c6f2ecSGreg Roach    }
83c9c6f2ecSGreg Roach
84c9c6f2ecSGreg Roach    /**
85c9c6f2ecSGreg Roach     * @param string $place
86c9c6f2ecSGreg Roach     *
87c9c6f2ecSGreg Roach     * @return RequestInterface
88c9c6f2ecSGreg Roach     */
89a09ea7ccSGreg Roach    protected function createPlaceNameSearchRequest(/** @scrutinizer ignore-unused */ string $place): RequestInterface
90c9c6f2ecSGreg Roach    {
91c9c6f2ecSGreg Roach        return new Request('GET', '');
92c9c6f2ecSGreg Roach    }
93c9c6f2ecSGreg Roach
94c9c6f2ecSGreg Roach    /**
95c9c6f2ecSGreg Roach     * @param ResponseInterface $response
96c9c6f2ecSGreg Roach     *
97c9c6f2ecSGreg Roach     * @return array<string>
98c9c6f2ecSGreg Roach     */
99a09ea7ccSGreg Roach    protected function parsePlaceNameSearchResponse(/** @scrutinizer ignore-unused */ ResponseInterface $response): array
100c9c6f2ecSGreg Roach    {
101c9c6f2ecSGreg Roach        return [];
102c9c6f2ecSGreg Roach    }
103c9c6f2ecSGreg Roach}
104