Completed
Pull Request — master (#110)
by Ruben de
29:47
created

RestClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
dl 0
loc 139
ccs 27
cts 42
cp 0.6429
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B createGuzzleClient() 0 30 1
A getGuzzleClient() 0 3 1
A setCurlDebugging() 0 5 1
A setCurlDefaultOption() 0 5 1
A setProxy() 0 5 1
A request() 0 8 1
1
<?php
2
3
namespace Blocktrail\SDK\Connection;
4
5
use Blocktrail\SDK\Blocktrail;
6
use Blocktrail\SDK\Throttler;
7
use GuzzleHttp\Client as Guzzle;
8
use GuzzleHttp\Handler\CurlHandler;
9
use GuzzleHttp\HandlerStack;
10
use HttpSignatures\Context;
11
use HttpSignatures\GuzzleHttpSignatures;
12
13
class RestClient extends BaseRestClient
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $options = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected $curlOptions = [];
24
25
    /**
26
     * @var Guzzle
27
     */
28
    protected $guzzle;
29
30
    /**
31
     * @var Throttler
32
     */
33
    protected $throttler;
34
35
    /**
36
     * GuzzleRestClient constructor.
37
     * @param $apiEndpoint
38
     * @param $apiVersion
39
     * @param $apiKey
40
     * @param $apiSecret
41
     */
42 118
    public function __construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret) {
43 118
        parent::__construct($apiEndpoint, $apiVersion, $apiKey, $apiSecret);
44 118
        $this->guzzle = $this->createGuzzleClient();
45 118
        if ($throttle = \getenv('BLOCKTRAIL_SDK_THROTTLE_BTCCOM')) {
46 118
            $throttle = (float)$throttle;
47
        } else {
48
            $throttle = 2.0;
49
        }
50
51 118
        $this->throttler = Throttler::getInstance($this->apiEndpoint, $throttle);
52 118
    }
53
54
    /**
55
     * @param array $options
56
     * @param array $curlOptions
57
     * @return Guzzle
58
     */
59 118
    protected function createGuzzleClient(array $options = [], array $curlOptions = []) {
60 118
        $options = $options + $this->options;
61 118
        $curlOptions = $curlOptions + $this->curlOptions;
62
63 118
        $context = new Context([
0 ignored issues
show
Unused Code introduced by
$context is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64 118
            'keys' => [$this->apiKey => $this->apiSecret],
65 118
            'algorithm' => 'hmac-sha256',
66
            'headers' => ['(request-target)', 'Content-MD5', 'Date'],
67
        ]);
68
69 118
        $curlHandler = new CurlHandler($curlOptions);
70 118
        $handler = HandlerStack::create($curlHandler);
71
//        $handler->push(GuzzleHttpSignatures::middlewareFromContext($context));
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
73 118
        return new Guzzle($options + array(
74 118
            'handler' => $handler,
75 118
            'base_uri' => $this->apiEndpoint,
76
            'headers' => array(
77
                'User-Agent' => Blocktrail::SDK_USER_AGENT . '/' . Blocktrail::SDK_VERSION,
78
            ),
79
            'http_errors' => false,
80 118
            'connect_timeout' => 3,
81 118
            'timeout' => 20.0, // tmp until we have a good matrix of all the requests and their expect min/max time
82
            'verify' => true,
83 118
            'proxy' => '',
84
            'debug' => false,
85
            'config' => array(),
86 118
            'auth' => '',
87
        ));
88
    }
89
90
    /**
91
     * @return Guzzle
92
     */
93
    public function getGuzzleClient() {
94
        return $this->guzzle;
95
    }
96
97
    /**
98
     * enable CURL debugging output
99
     *
100
     * @param   bool        $debug
101
     */
102
    public function setCurlDebugging($debug = true) {
103
        $this->options['debug'] = $debug;
104
105
        $this->guzzle = $this->createGuzzleClient();
106
    }
107
108
109
    /**
110
     * set cURL default option on Guzzle client
111
     * @param string    $key
112
     * @param bool      $value
113
     */
114
    public function setCurlDefaultOption($key, $value) {
115
        $this->curlOptions[$key] = $value;
116
117
        $this->guzzle = $this->createGuzzleClient();
118
    }
119
120
    /**
121
     * set the proxy config for Guzzle
122
     *
123
     * @param   $proxy
124
     */
125
    public function setProxy($proxy) {
126
        $this->options['proxy'] = $proxy;
127
128
        $this->guzzle = $this->createGuzzleClient();
129
    }
130
131
    /**
132
     * generic request executor
133
     *
134
     * @param   string          $method         GET, POST, PUT, DELETE
135
     * @param   string          $endpointUrl
136
     * @param   array           $queryString
137
     * @param   array|string    $body
138
     * @param   string          $auth           http-signatures to enable http-signature signing
139
     * @param   string          $contentMD5Mode body or url
140
     * @param   float           $timeout        timeout in seconds
141
     * @return Response
142
     */
143 32
    public function request($method, $endpointUrl, $queryString = null, $body = null, $auth = null, $contentMD5Mode = null, $timeout = null) {
144 32
        $this->throttler->waitForThrottle();
145
146 32
        $request = $this->buildRequest($method, $endpointUrl, $queryString, $body, $auth, $contentMD5Mode, $timeout);
147 32
        $response = $this->guzzle->send($request, ['auth' => $auth, 'timeout' => $timeout]);
148
149 32
        return $this->responseHandler($response);
150
    }
151
}
152