Completed
Push — master ( 9df105...b9a551 )
by Arthur
02:20
created

Client::request()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 10
nc 2
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 string          $apiKey
43
     */
44
    public function __construct(ClientInterface $httpClient, $decoder, $apiKey)
45
    {
46
        $this->apiKey = $apiKey;
47
        $this->decoder = $decoder;
48
        $this->httpClient = $httpClient;
49
    }
50
51
    /**
52
     * @param string $method
53
     * @param string $uri
54
     * @param array  $parameters
55
     * @param array  $options
56
     *
57
     * @return array
58
     *
59
     * @throws ClientErrorException
60
     */
61
    public function request($method, $uri, array $parameters = [], array $options = [])
62
    {
63
        $parameters['key'] = $this->apiKey;
64
        $options['query'] = $parameters;
65
66
        $uri = $this->apiUrl.$uri.'/json';
67
68
        $this->log($method, $uri, $options);
69
        $response = $this->httpClient->request($method, $uri, $options);
70
71
        $result = $this->decoder->decode($response);
72
73
        if (isset($result['status']) && 'OK' !== $result['status']) {
74
            throw new ClientErrorException(sprintf('Invalid return status "%s"', $result['status']));
75
        }
76
77
        return $result;
78
    }
79
80
    /**
81
     * @param string $method
82
     * @param string $uri
83
     * @param array  $options
84
     */
85
    private function log($method, $uri, array $options = [])
86
    {
87
        if (null === $this->logger) {
88
            return;
89
        }
90
91
        $this->logger->info(
92
            sprintf(
93
                'google-api request: %s %s',
94
                $method,
95
                $uri
96
            ),
97
            [
98
                'params' => http_build_query(
99
                    $options['query']
100
                ),
101
            ]
102
        );
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function setLogger(LoggerInterface $logger)
109
    {
110
        $this->logger = $logger;
111
112
        return $this;
113
    }
114
}
115