Completed
Push — master ( ae801a...66cad5 )
by Arthur
08:28 queued 02:44
created

Client::request()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 15
nc 3
nop 4
1
<?php
2
3
namespace Arthem\GoogleApi\Infrastructure\Client;
4
5
use Arthem\GoogleApi\Infrastructure\Client\Decoder\DecoderInterface;
6
use Arthem\GoogleApi\Infrastructure\Client\Exception\ClientErrorException;
7
use GuzzleHttp\ClientInterface;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LoggerInterface;
10
11
abstract class Client implements LoggerAwareInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $apiUrl;
17
18
    /**
19
     * The client API key.
20
     *
21
     * @var string
22
     */
23
    private $apiKey;
24
25
    /**
26
     * @var ClientInterface
27
     */
28
    private $httpClient;
29
30
    /**
31
     * @var DecoderInterface
32
     */
33
    private $decoder;
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param ClientInterface  $httpClient
42
     * @param DecoderInterface $decoder
43
     * @param string           $apiKey
44
     */
45
    public function __construct(ClientInterface $httpClient, DecoderInterface $decoder, $apiKey)
46
    {
47
        $this->apiKey = $apiKey;
48
        $this->decoder = $decoder;
49
        $this->httpClient = $httpClient;
50
    }
51
52
    /**
53
     * @param string $method
54
     * @param string $uri
55
     * @param array  $parameters
56
     * @param array  $options
57
     *
58
     * @return array
59
     *
60
     * @throws ClientErrorException
61
     */
62
    public function request($method, $uri, array $parameters = [], array $options = [])
63
    {
64
        $parameters['key'] = $this->apiKey;
65
        $options['query'] = $parameters;
66
67
        $uri = $this->apiUrl.$uri.'/json';
68
69
        $this->log($method, $uri, $options);
70
        $response = $this->httpClient->request($method, $uri, $options);
71
72
        $result = $this->decoder->decode($response);
73
74
        if (!isset($result['status'])) {
75
            throw new ClientErrorException('Missing return status');
76
        }
77
78
        if (!in_array($result['status'], [
79
                'OK',
80
                'ZERO_RESULTS',
81
            ], true)) {
82
            throw new ClientErrorException(sprintf('Invalid return status "%s"', $result['status']));
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * @param string $method
90
     * @param string $uri
91
     * @param array  $options
92
     */
93
    private function log($method, $uri, array $options = [])
94
    {
95
        if (null === $this->logger) {
96
            return;
97
        }
98
99
        $this->logger->info(
100
            sprintf(
101
                'google-api request: %s %s',
102
                $method,
103
                $uri
104
            ),
105
            [
106
                'params' => http_build_query(
107
                    $options['query']
108
                ),
109
            ]
110
        );
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function setLogger(LoggerInterface $logger)
117
    {
118
        $this->logger = $logger;
119
120
        return $this;
121
    }
122
}
123