Completed
Push — develop ( c3a705...4b9394 )
by Greg
11:16
created

ModuleMapAutocompleteTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createPlaceNameSearchRequest() 0 3 1
A parsePlaceNameSearchResponse() 0 3 1
A searchPlaceNames() 0 31 4
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\Module;
21
22
use Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Registry;
24
use GuzzleHttp\Client;
25
use GuzzleHttp\Exception\RequestException;
26
use GuzzleHttp\Psr7\Request;
27
use Psr\Http\Message\RequestInterface;
28
use Psr\Http\Message\ResponseInterface;
29
30
/**
31
 * Trait ModuleMapAutocompleteTrait - default implementation of ModuleMapAutocompleteInterface
32
 */
33
trait ModuleMapAutocompleteTrait
34
{
35
    /**
36
     * @param string $place
37
     *
38
     * @return array<string>
39
     */
40
    public function searchPlaceNames(string $place): array
41
    {
42
        if (strlen($place) <= 2) {
43
            return [];
44
        }
45
46
        $key   = $this->name() . $place;
0 ignored issues
show
Bug introduced by
It seems like name() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $key   = $this->/** @scrutinizer ignore-call */ name() . $place;
Loading history...
47
        $cache = Registry::cache()->file();
48
        $ttl   = 86400;
49
50
        try {
51
            return $cache->remember($key, function () use ($place) {
52
                $request = $this->createPlaceNameSearchRequest($place);
53
54
                $client = new Client([
55
                    'timeout' => 3,
56
                ]);
57
58
                $response = $client->send($request);
59
60
                if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
61
                    return $this->parsePlaceNameSearchResponse($response);
62
                }
63
64
                return [];
65
            }, $ttl);
66
        } catch (RequestException $ex) {
67
            // Service down?  Quota exceeded?
68
            // Don't try for another hour.
69
            $cache->remember($key, fn () => [], 3600);
70
            return [];
71
        }
72
    }
73
74
    /**
75
     * @param string $place
76
     *
77
     * @return RequestInterface
78
     */
79
    protected function createPlaceNameSearchRequest(string $place): RequestInterface
0 ignored issues
show
Unused Code introduced by
The parameter $place is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
    protected function createPlaceNameSearchRequest(/** @scrutinizer ignore-unused */ string $place): RequestInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        return new Request('GET', '');
82
    }
83
84
    /**
85
     * @param ResponseInterface $response
86
     *
87
     * @return array<string>
88
     */
89
    protected function parsePlaceNameSearchResponse(ResponseInterface $response): array
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
    protected function parsePlaceNameSearchResponse(/** @scrutinizer ignore-unused */ ResponseInterface $response): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        return [];
92
    }
93
}
94