1c9c6f2ecSGreg Roach<?php 2c9c6f2ecSGreg Roach 3c9c6f2ecSGreg Roach/** 4c9c6f2ecSGreg Roach * webtrees: online genealogy 5c9c6f2ecSGreg Roach * Copyright (C) 2021 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 /** 36c9c6f2ecSGreg Roach * @param string $place 37c9c6f2ecSGreg Roach * 38c9c6f2ecSGreg Roach * @return array<string> 39c9c6f2ecSGreg Roach */ 40c9c6f2ecSGreg Roach public function searchPlaceNames(string $place): array 41c9c6f2ecSGreg Roach { 42c9c6f2ecSGreg Roach if (strlen($place) <= 2) { 43c9c6f2ecSGreg Roach return []; 44c9c6f2ecSGreg Roach } 45c9c6f2ecSGreg Roach 46c9c6f2ecSGreg Roach $key = $this->name() . $place; 47c9c6f2ecSGreg Roach $cache = Registry::cache()->file(); 48c9c6f2ecSGreg Roach $ttl = 86400; 49c9c6f2ecSGreg Roach 50c9c6f2ecSGreg Roach try { 51c9c6f2ecSGreg Roach return $cache->remember($key, function () use ($place) { 52c9c6f2ecSGreg Roach $request = $this->createPlaceNameSearchRequest($place); 53c9c6f2ecSGreg Roach 54c9c6f2ecSGreg Roach $client = new Client([ 55c9c6f2ecSGreg Roach 'timeout' => 3, 56c9c6f2ecSGreg Roach ]); 57c9c6f2ecSGreg Roach 58c9c6f2ecSGreg Roach $response = $client->send($request); 59c9c6f2ecSGreg Roach 60c9c6f2ecSGreg Roach if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) { 61c9c6f2ecSGreg Roach return $this->parsePlaceNameSearchResponse($response); 62c9c6f2ecSGreg Roach } 63c9c6f2ecSGreg Roach 64c9c6f2ecSGreg Roach return []; 65c9c6f2ecSGreg Roach }, $ttl); 664022f21aSGreg Roach } catch (GuzzleException $ex) { 67c9c6f2ecSGreg Roach // Service down? Quota exceeded? 68c9c6f2ecSGreg Roach // Don't try for another hour. 69c9c6f2ecSGreg Roach $cache->remember($key, fn () => [], 3600); 70928b8592SGreg Roach 71c9c6f2ecSGreg Roach return []; 72c9c6f2ecSGreg Roach } 73c9c6f2ecSGreg Roach } 74c9c6f2ecSGreg Roach 75c9c6f2ecSGreg Roach /** 76c9c6f2ecSGreg Roach * @param string $place 77c9c6f2ecSGreg Roach * 78c9c6f2ecSGreg Roach * @return RequestInterface 79c9c6f2ecSGreg Roach */ 80*a09ea7ccSGreg Roach protected function createPlaceNameSearchRequest(/** @scrutinizer ignore-unused */ string $place): RequestInterface 81c9c6f2ecSGreg Roach { 82c9c6f2ecSGreg Roach return new Request('GET', ''); 83c9c6f2ecSGreg Roach } 84c9c6f2ecSGreg Roach 85c9c6f2ecSGreg Roach /** 86c9c6f2ecSGreg Roach * @param ResponseInterface $response 87c9c6f2ecSGreg Roach * 88c9c6f2ecSGreg Roach * @return array<string> 89c9c6f2ecSGreg Roach */ 90*a09ea7ccSGreg Roach protected function parsePlaceNameSearchResponse(/** @scrutinizer ignore-unused */ ResponseInterface $response): array 91c9c6f2ecSGreg Roach { 92c9c6f2ecSGreg Roach return []; 93c9c6f2ecSGreg Roach } 94c9c6f2ecSGreg Roach} 95