Completed
Push — master ( 850634...9eeabf )
by Gonzalo
06:26
created

HttpClientWrapper::wrapRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
4
namespace Mrzard\OpenExchangeRates\Service;
5
6
7
class HttpClientWrapper implements HttpClientInterface
8
{
9
    /**
10
     * @var object
11
     */
12
    private $wrappedClient;
13
14
    public function __construct($client)
15
    {
16
        $this->wrappedClient = $client;
17
    }
18
19
    /**
20
     * @return object
21
     */
22
    public function getWrappedClient()
23
    {
24
        return $this->wrappedClient;
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function send($request)
31
    {
32
        $request = $request instanceof HttpRequestInterface ? $request->request() : new HttpRequestWrapper($request);
33
34
        return $this->wrapResponse($this->wrappedClient->send($request));
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function request($method, $uri = null, array $options = [])
41
    {
42
        $response = $this->wrappedClient->request($method, $uri, $options);
43
44
        return $this->wrapResponse($response);
45
    }
46
47
    private function wrapResponse($response)
48
    {
49
        if ($response instanceof HttpResponseInterface) {
50
            return $response;
51
        }
52
        if (!method_exists($response, 'getBody')) {
53
54
            throw new \ErrorException('Supplied client\'s response don\'t have method `getBody()`');
55
        }
56
        return new HttpResponseWrapper($response);
57
    }
58
}