Client   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A research() 0 12 2
1
<?php
2
namespace KWTClient;
3
4
use GuzzleHttp\ClientInterface;
5
use GuzzleHttp\Psr7\Uri;
6
use KWTClient\Exception\ApiException;
7
use KWTClient\Exception\ExceptionFactory;
8
use KWTClient\Exception\SearchLimitException;
9
use KWTClient\Request\RequestInterface;
10
use KWTClient\Response\Response;
11
use KWTClient\Response\ResponseInterface;
12
13
class Client
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $apiKey;
19
20
    /**
21
     * @var ClientInterface
22
     */
23
    protected $httpClient;
24
25
    /**
26
     * Client constructor.
27
     *
28
     * @param string $apiKey
29
     * @param ClientInterface|null $httpClient
30
     */
31
    public function __construct($apiKey,
32
                                ClientInterface $httpClient = null)
33
    {
34
        $this->apiKey = $apiKey;
35
        $this->httpClient = $httpClient;
36
37
        if (is_null($httpClient)) {
38
            $this->httpClient = new \GuzzleHttp\Client();
39
        }
40
    }
41
42
    /**
43
     * @param RequestInterface $request
44
     *
45
     * @return ResponseInterface
46
     *
47
     * @throws ApiException | SearchLimitException
48
     */
49
    public function research(RequestInterface $request)
50
    {
51
        try {
52
            $response = $this->httpClient->request('GET',
53
                                                    Uri::withQueryValue($request->getUri(),
54
                                                                        'apikey',
55
                                                                        $this->apiKey));
56
            return new Response($response);
57
        } catch (\GuzzleHttp\Exception\ClientException $ex) {
58
            throw ExceptionFactory::createThrowable($ex);
59
        }
60
    }
61
}
62