Completed
Push — master ( 0b3446...59d977 )
by
unknown
26s
created

Sapi3CountingSearchService::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Search;
4
5
use GuzzleHttp\Psr7\Request;
6
use Http\Client\HttpClient;
7
use function http_build_query;
8
use Psr\Http\Message\UriInterface;
9
10
class Sapi3CountingSearchService implements CountingSearchServiceInterface
11
{
12
    /**
13
     * @var UriInterface
14
     */
15
    private $searchLocation;
16
17
    /**
18
     * @var HttpClient
19
     */
20
    private $httpClient;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $apiKey;
26
27
    /**
28
     * @var array
29
     */
30
    private $queryParameters;
31
32
    /**
33
     * @param UriInterface $searchLocation
34
     * @param HttpClient $httpClient
35
     * @param string|null $apiKey
36
     */
37
    public function __construct(
38
        UriInterface $searchLocation,
39
        HttpClient $httpClient,
40
        string $apiKey = null
41
    ) {
42
        $this->searchLocation = $searchLocation;
43
        $this->httpClient = $httpClient;
44
        $this->apiKey = $apiKey;
45
        $this->queryParameters = [];
46
    }
47
48
    public function withQueryParameter(string $key, $value)
49
    {
50
        $c = clone $this;
51
        $c->queryParameters[$key] = $value;
52
        return $c;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function search(string $query): int
59
    {
60
        $queryParameters =
61
            [
62
                'q' => $query,
63
                'start' => 0,
64
                'limit' => 1,
65
            ];
66
67
        $queryParameters += $this->queryParameters;
68
69
        $queryParameters = http_build_query($queryParameters);
70
71
        $headers = [];
72
73
        if ($this->apiKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->apiKey of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74
            $headers['X-Api-Key'] = $this->apiKey;
75
        }
76
77
        $url = $this->searchLocation->withQuery($queryParameters);
78
79
        $request = new Request(
80
            'GET',
81
            (string) $url,
82
            $headers
83
        );
84
85
        $response = $this->httpClient->sendRequest($request);
86
87
        $decodedResponse = json_decode(
88
            $response->getBody()->getContents()
89
        );
90
91
        return (int) $decodedResponse->{'totalItems'};
92
    }
93
}
94