1c9c6f2ecSGreg Roach<?php 2c9c6f2ecSGreg Roach 3c9c6f2ecSGreg Roach/** 4c9c6f2ecSGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg 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 30c9c6f2ecSGreg Roach/** 31c9c6f2ecSGreg Roach * Trait ModuleMapAutocompleteTrait - default implementation of ModuleMapAutocompleteInterface 32c9c6f2ecSGreg Roach */ 33c9c6f2ecSGreg Roachtrait ModuleMapAutocompleteTrait 34c9c6f2ecSGreg Roach{ 35c9c6f2ecSGreg Roach /** 36bfed30e4SGreg Roach * A unique internal name for this module (based on the installation folder). 37bfed30e4SGreg Roach * 38bfed30e4SGreg Roach * @return string 39bfed30e4SGreg Roach */ 40bfed30e4SGreg Roach abstract public function name(): string; 41bfed30e4SGreg Roach 42bfed30e4SGreg Roach /** 43c9c6f2ecSGreg Roach * @param string $place 44c9c6f2ecSGreg Roach * 45c9c6f2ecSGreg Roach * @return array<string> 46c9c6f2ecSGreg Roach */ 47c9c6f2ecSGreg Roach public function searchPlaceNames(string $place): array 48c9c6f2ecSGreg Roach { 49c9c6f2ecSGreg Roach if (strlen($place) <= 2) { 50c9c6f2ecSGreg Roach return []; 51c9c6f2ecSGreg Roach } 52c9c6f2ecSGreg Roach 53c9c6f2ecSGreg Roach $key = $this->name() . $place; 54c9c6f2ecSGreg Roach $cache = Registry::cache()->file(); 55c9c6f2ecSGreg Roach $ttl = 86400; 56c9c6f2ecSGreg Roach 57c9c6f2ecSGreg Roach try { 58c9c6f2ecSGreg Roach return $cache->remember($key, function () use ($place) { 59c9c6f2ecSGreg Roach $request = $this->createPlaceNameSearchRequest($place); 60c9c6f2ecSGreg Roach 61c9c6f2ecSGreg Roach $client = new Client([ 62c9c6f2ecSGreg Roach 'timeout' => 3, 63c9c6f2ecSGreg Roach ]); 64c9c6f2ecSGreg Roach 65c9c6f2ecSGreg Roach $response = $client->send($request); 66c9c6f2ecSGreg Roach 67c9c6f2ecSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 68c9c6f2ecSGreg Roach return $this->parsePlaceNameSearchResponse($response); 69c9c6f2ecSGreg Roach } 70c9c6f2ecSGreg Roach 71c9c6f2ecSGreg Roach return []; 72c9c6f2ecSGreg Roach }, $ttl); 734022f21aSGreg Roach } catch (GuzzleException $ex) { 74c9c6f2ecSGreg Roach // Service down? Quota exceeded? 75c9c6f2ecSGreg Roach // Don't try for another hour. 76c9c6f2ecSGreg Roach $cache->remember($key, fn () => [], 3600); 77928b8592SGreg Roach 78c9c6f2ecSGreg Roach return []; 79c9c6f2ecSGreg Roach } 80c9c6f2ecSGreg Roach } 81c9c6f2ecSGreg Roach 82c9c6f2ecSGreg Roach /** 83c9c6f2ecSGreg Roach * @param string $place 84c9c6f2ecSGreg Roach * 85c9c6f2ecSGreg Roach * @return RequestInterface 86c9c6f2ecSGreg Roach */ 87a09ea7ccSGreg Roach protected function createPlaceNameSearchRequest(/** @scrutinizer ignore-unused */ string $place): RequestInterface 88c9c6f2ecSGreg Roach { 89c9c6f2ecSGreg Roach return new Request('GET', ''); 90c9c6f2ecSGreg Roach } 91c9c6f2ecSGreg Roach 92c9c6f2ecSGreg Roach /** 93c9c6f2ecSGreg Roach * @param ResponseInterface $response 94c9c6f2ecSGreg Roach * 95c9c6f2ecSGreg Roach * @return array<string> 96c9c6f2ecSGreg Roach */ 97a09ea7ccSGreg Roach protected function parsePlaceNameSearchResponse(/** @scrutinizer ignore-unused */ ResponseInterface $response): array 98c9c6f2ecSGreg Roach { 99c9c6f2ecSGreg Roach return []; 100c9c6f2ecSGreg Roach } 101c9c6f2ecSGreg Roach} 102