Completed
Push — master ( f7238e...31c6cc )
by Peter
02:35
created

Client::setTimeGranularity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace PeterColes\Betfair\Http;
4
5
use Exception;
6
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PeterColes\Betfair\Http\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use GuzzleHttp\Psr7\Response;
8
use PeterColes\Betfair\Api\Auth;
9
use stdClass;
10
11
class Client
12
{
13
    protected $guzzleClient;
14
15
    protected $method = 'get';
16
17
    protected $uri = '';
18
19
    protected $options = [ ];
20
21
    /**
22
     * instantiate Guzzle client (unless one is injected).
23
     *
24
     * @param GuzzleClient $client
25
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
     */
27
    public function __construct($client = null)
28
    {
29
        $this->guzzleClient = $client ?: new Client;
30
        $this->options[ 'headers' ] = [ 'Accept' => 'application/json' ];
31
    }
32
33
    /**
34
     * Setter for request method.
35
     *
36
     * @param string $method
37
     * @return Client
38
     */
39
    public function setMethod(string $method)
40
    {
41
        $this->method = $method;
42
        return $this;
43
    }
44
45
    /**
46
     * Setter for request end point URI.
47
     *
48
     * @param string $endPoint
49
     * @return Client
50
     */
51
    public function setEndPoint(string $endPoint)
52
    {
53
        $this->uri = $endPoint;
54
        return $this;
55
    }
56
57
    /**
58
     * Setter for request headers.
59
     *
60
     * @param array $header
61
     * @return Client
62
     */
63
    public function addHeader(array $header)
64
    {
65
        $this->options[ 'headers' ][ ] = $header;
66
        return $this;
67
    }
68
69
    /**
70
     * Setter for authentication headers.
71
     *
72
     * @param array $headers
73
     * @return Client
74
     */
75
    public function authHeaders(array $headers = [ ])
76
    {
77
        if (count($headers) == 0) {
78
            $headers = [ 'X-Application' => Auth::$appKey, 'X-Authentication' => Auth::$sessionToken ];
79
        }
80
        $this->options[ 'headers' ] = array_merge($this->options[ 'headers' ], $headers);
81
        return $this;
82
    }
83
84
    /**
85
     * Setter for request form data.
86
     *
87
     * @param array $formData
88
     * @return Client
89
     */
90
    public function setFormData(array $formData)
91
    {
92
        $this->options[ 'form_params' ] = $formData;
93
        return $this;
94
    }
95
96
    /**
97
     * Setter for request filter(s). mandatory, but can be empty
98
     *
99
     * @param  array $filter
100
     * @return Client
101
     */
102
    public function setFilter($filter = null)
103
    {
104
        $this->options[ 'json' ][ 'filter' ] = $filter ?: new stdClass;
105
        return $this;
106
    }
107
108
    /**
109
     * Setter for mandatory marketId(s).
110
     *
111
     * @param  array $marketIds
112
     * @return Client
113
     */
114
    public function setMarketIds($marketIds = null)
115
    {
116
        $this->options[ 'json' ][ 'marketIds' ] = $marketIds ?: new stdClass;
117
        return $this;
118
    }
119
120
    /**
121
     * Setter for optional market projection, i.e. what market-related data should be returned.
122
     *
123
     * @param  array $marketProjection
0 ignored issues
show
Documentation introduced by
There is no parameter named $marketProjection. Did you maybe mean $projection?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
124
     * @return Client
125
     */
126
    public function setProjection($name, $projection = null)
127
    {
128
        if ($projection) {
129
            $this->options[ 'json' ][ $name ] = $projection;
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * Setter for optional request sort.
136
     *
137
     * @param string $sort
138
     * @return Client
139
     */
140
    public function setSort($sort)
141
    {
142
        if ($sort) {
143
            $this->options[ 'json' ][ 'sort' ] = $sort;
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * Setter for mandatory request max results limiter.
150
     *
151
     * @param integer $maxResults
152
     * @return Client
153
     */
154
    public function setMaxresults($maxResults = 100)
155
    {
156
        if ($maxResults) {
157
            $this->options[ 'json' ][ 'maxResults' ] = $maxResults;
158
        }
159
        return $this;
160
    }
161
162
    /**
163
     * Setter for optional request locale.
164
     *
165
     * @param string $locale
166
     * @return Client
167
     */
168
    public function setLocale($locale)
169
    {
170
        if ($locale) {
171
            $this->options[ 'json' ][ 'locale' ] = $locale;
172
        }
173
        return $this;
174
    }
175
176
    /**
177
     * Setter for mandatory time granularity.
178
     *
179
     * @param string $timeGranularity
0 ignored issues
show
Documentation introduced by
There is no parameter named $timeGranularity. Did you maybe mean $granularity?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
180
     * @return Client
181
     */
182
    public function setTimeGranularity($granularity)
183
    {
184
        $this->options[ 'json' ][ 'granularity' ] = $granularity;
185
        return $this;
186
    }
187
188
    /**
189
     * Setter for optional currency code.
190
     *
191
     * @param string $currencyCode
192
     * @return Client
193
     */
194
    public function setCurrencyCode($currencyCode)
195
    {
196
        if ($currencyCode) {
197
            $this->options[ 'json' ][ 'currencyCode' ] = $currencyCode;
198
        }
199
        return $this;
200
    }
201
202
    /**
203
     * Setter for optional flag.
204
     *
205
     * @param boolean $flag
206
     * @return Client
207
     */
208
    public function setFlag($flag)
209
    {
210
        if ($flag) {
211
            $this->options[ 'json' ][ 'flag' ] = true;
212
        }
213
        return $this;
214
    }
215
216
    /**
217
     * Dispatch the request and provide hooks for error handling for the response.
218
     *
219
     * @return object stdClass
220
     */
221
    public function send()
222
    {
223
        $response = $this->guzzleClient->request($this->method, $this->uri, $this->options);
224
225
        $status = $this->getStatus($response);
226
227
        if ($status != 200) {
228
            $this->handleHttpException($status);
229
        }
230
231
        $body = $this->getBody($response);
232
233
        if (is_object($body) && $body->status != 'SUCCESS') {
234
            $this->handleApiException($body->error);
235
        }
236
237
        return $body;
238
    }
239
240
    /**
241
     * Get status code from http response.
242
     *
243
     * @param Response $response
244
     * @return integer
245
     */
246
    protected function getStatus(Response $response)
247
    {
248
        return (int) $response->getStatusCode();
249
    }
250
251
    /**
252
     * Get http response body, cast to json and decode.
253
     *
254
     * @param Response object $response
255
     * @return array
256
     */
257
    protected function getBody(Response $response)
258
    {
259
        return json_decode((string) $response->getBody());
260
    }
261
262
    /**
263
     * Stub for http exception handling.
264
     *
265
     * @param Integer $status
266
     * @return void
267
     */
268
    protected function handleHttpException($status) {
269
        throw new Exception('Http request failure. Http Exception Code: '.$status);
270
    }
271
272
    /**
273
     * Stub for API exception handling.
274
     *
275
     * @param String $exception
276
     * @return void
277
     */
278
    protected function handleApiException($exception) {
279
        throw new Exception('API request failure. API Exception Message: '.$exception);
280
    }
281
}
282