Completed
Pull Request — master (#2)
by
unknown
06:17 queued 03:34
created

BaseClient   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 89.01%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 18
c 4
b 1
f 1
lcom 1
cbo 7
dl 0
loc 263
ccs 81
cts 91
cp 0.8901
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A loadClient() 0 7 1
A loadGuzzle() 0 12 1
A getUniqueId() 0 4 1
A getVersion() 0 4 1
A getVenture() 0 4 1
A getPartner() 0 4 1
A getAccessToken() 0 4 1
A getClient() 0 4 1
A buildRequestUrl() 0 10 1
A throwException() 0 17 2
A create() 0 12 1
A update() 0 12 1
A logResponse() 0 12 1
A isResponseOk() 0 4 1
A doRequest() 0 21 2
1
<?php
2
3
namespace Iris\Api;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\RequestException;
7
8
abstract class BaseClient
9
{
10
    const RESPONSE_OK = 200;
11
12
    const RESPONSE_OK_NO_BODY = 204;
13
14
    /**
15
     * @var \GuzzleHttp\Client
16
     */
17
    protected $client;
18
19
    /**
20
     * @var string
21
     */
22
    protected $version;
23
24
    /**
25
     * @var string
26
     */
27
    protected $partner;
28
29
    /**
30
     * @var string
31
     */
32
    protected $accessToken;
33
34
    /**
35
     * @var string
36
     */
37
    protected $venture;
38
39
    /**
40
     * @var string
41
     */
42
    protected $uniqueId;
43
44
    /**
45
     * @var \Psr\Log\LoggerInterface
46
     */
47
    protected $logger;
48
49
    /**
50
     * @param \Iris\Interfaces\Configuration $configuration
51
     * @param \Psr\Log\LoggerInterface $logger
52
     */
53 32
    public function __construct(
54
        \Iris\Interfaces\Configuration $configuration,
55
        \Psr\Log\LoggerInterface $logger
56
    ) {
57 32
        $this->partner      = $configuration->getVenture();
58 32
        $this->venture      = $configuration->getVenture();
59 32
        $this->uniqueId     = uniqid();
60 32
        $this->logger       = $logger;
61
62 32
        $this->loadClient($configuration->getConnection());
63 32
    }
64
65
    /**
66
     * @param \Iris\Interfaces\Connection $connection
67
     */
68 32
    protected function loadClient(\Iris\Interfaces\Connection $connection)
69
    {
70 32
        $this->version      = $connection->getVersion();
71 32
        $this->accessToken  = $connection->getAccessToken();
72
73 32
        $this->loadGuzzle($connection->getBaseUrl());
74 32
    }
75
76
    /**
77
     * @param string $baseUrl
78
     */
79
    protected function loadGuzzle($baseUrl)
80
    {
81
        $this->client = new HttpClient(array(
82
            'base_url' => $baseUrl,
83
            'defaults' => array(
84
                'headers' => array(
85
                    'Accept' => 'application/json',
86
                    'Content-Type' => 'application/json'
87
                )
88
            )
89
        ));
90
    }
91
92
    /**
93
     * @return string
94
     */
95 27
    public function getUniqueId()
96
    {
97 27
        return $this->uniqueId;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 26
    public function getVersion()
104
    {
105 26
        return $this->version;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 20
    public function getVenture()
112
    {
113 20
        return $this->venture;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 7
    public function getPartner()
120
    {
121 7
        return $this->partner;
122
    }
123
124
    /**
125
     * @return string
126
     */
127 20
    public function getAccessToken()
128
    {
129 20
        return $this->accessToken;
130
    }
131
132
    /**
133
     * @return \GuzzleHttp\Client
134
     */
135
    public function getClient()
136
    {
137
        return $this->client;
138
    }
139
140
    /**TODO generalize this method to be used by Bob as venture
141
     * Builds path to call api.
142
     * @param string $callPath Suffix path, the action.
143
     * @param string $venture Venture code.
144
     * @return string
145
     */
146 6
    protected function buildRequestUrl($callPath, $venture)
147
    {
148 6
        $requestUrl = sprintf(
149 6
            'api/%s/gfg/%s/%s',
150 6
            $this->getVersion(),
151 6
            $this->getPartner(),
152
            $venture
153 6
        );
154 6
        return $requestUrl . $callPath;
155
    }
156
157
    /**
158
     * @param \Exception $exception
159
     * @param string $message
160
     * @param array $context
161
     * @throw \Iris\Exceptions\RetryMessage
162
     */
163 2
    public function throwException(\Exception $exception, $message, array $context = [])
164
    {
165 2
        $message = $message . ' ( Exception message: ' . $exception->getMessage() . ')';
166 2
        $this->logger->error($message, $context);
167
168 2
        $retryMessage = 'Try Again';
169 2
        $code = 500;
170
171 2
        if ($exception instanceof RequestException) {
172 1
            $this->logResponse($exception->getResponse());
173
174 1
            $retryMessage = $exception->getResponse()->getBody();
175 1
            $code         = $exception->getResponse()->getStatusCode();
176 1
        }
177
178 2
        throw new \Iris\Exceptions\RetryMessage($retryMessage, $code);
179
    }
180
181
    /**
182
     * @param string $url
183
     * @param $body
184
     * @return \GuzzleHttp\Message\Response
185
     */
186 2
    protected function create($url, $body)
187
    {
188 2
        return $this->doRequest(
189 2
            'post',
190 2
            $url,
191
            array(
192 2
                'body' => json_encode($body),
193 2
                'headers' => ['venture-access-token' => $this->getAccessToken(), 'venture-key' => $this->getVenture()],
194
                'exceptions' => false
195 2
            )
196 2
        );
197
    }
198
199
    /**
200
     * @param string $url
201
     * @param $body
202
     * @return \GuzzleHttp\Message\Response
203
     */
204 17
    protected function update($url, $body)
205
    {
206 17
        return $this->doRequest(
207 17
            'put',
208 17
            $url,
209
            array(
210 17
                'body' => json_encode($body),
211 17
                'headers' => ['venture-access-token' => $this->getAccessToken(), 'venture-key' => $this->getVenture()],
212
                'exceptions' => false
213 17
            )
214 17
        );
215
    }
216
217
    /**
218
     * @param string $method
219
     * @param string $url
220
     * @param array $options
221
     * @return \GuzzleHttp\Message\Response
222
     */
223 25
    protected function doRequest($method, $url, $options = array())
224
    {
225 25
        $message = sprintf(
226 25
            '%s REQUEST TO: %s -- OPTIONS %s',
227 25
            $this->getUniqueId(),
228 25
            $url,
229 25
            json_encode($options)
230 25
        );
231
232 25
        $this->logger->info($message);
233
234 25
        $response = $this->getClient()->$method($url, $options);
235
236 14
        $this->logResponse($response);
237
238 14
        if (!$this->isResponseOk($response)) {
239 3
            throw new \Iris\Exceptions\RetryMessage($response->getBody(), $response->getStatusCode());
240
        }
241
242 11
        return $response;
243
    }
244
245
    /**
246
     * @param \GuzzleHttp\Message\ResponseInterface $response
247
     * @return void
248
     */
249 15
    protected function logResponse(\GuzzleHttp\Message\ResponseInterface $response)
250
    {
251 15
        $message = sprintf(
252 15
            '%s RESPONSE: %s -- HTTP CODE %s -- BODY %s',
253 15
            $this->getUniqueId(),
254 15
            $response->getEffectiveUrl(),
255 15
            $response->getStatusCode(),
256 15
            $response->getBody()
257 15
        );
258
259 15
        $this->logger->info($message);
260 15
    }
261
262
    /**
263
     * @param \GuzzleHttp\Message\ResponseInterface $response
264
     * @return bool
265
     */
266 14
    protected function isResponseOk(\GuzzleHttp\Message\ResponseInterface $response)
267
    {
268 14
        return in_array($response->getStatusCode(), array(static::RESPONSE_OK, static::RESPONSE_OK_NO_BODY));
269
    }
270
}
271