Completed
Push — master ( ef5096...ded87a )
by Peter
02:24
created

Client::setwallet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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 marketId(s).
110
     * This param is sometime mandatory, but othertimes breaks the API if an empty value is provided
111
     *
112
     * @param  array $marketIds
113
     * @param  boolean $mandatory
114
     * @return Client
115
     */
116
    public function setMarketIds($marketIds = null, $mandatory = false)
117
    {
118
        if ($marketIds || $mandatory) {
119
            $this->options[ 'json' ][ 'marketIds' ] = $marketIds ?: new stdClass;
120
        }
121
        return $this;
122
    }
123
124
    /**
125
     * Setter for optional betId(s).
126
     *
127
     * @param  array $betIds
128
     * @return Client
129
     */
130
    public function setBetIds($betIds = null)
131
    {
132
        if ($betIds) {
133
            $this->options[ 'json' ][ 'betIds' ] = $betIds ?: new stdClass;
134
        }
135
        return $this;
136
    }
137
138
    /**
139
     * Setter for optional market projection, i.e. what market-related data should be returned.
140
     *
141
     * @param  string $name
142
     * @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...
143
     * @return Client
144
     */
145
    public function setProjection($name, $projection = null)
146
    {
147
        if ($projection) {
148
            $this->options[ 'json' ][ $name ] = $projection;
149
        }
150
        return $this;
151
    }
152
153
    /**
154
     * Setter for optional request sort.
155
     *
156
     * @param string $sort
157
     * @return Client
158
     */
159
    public function setSort($sort)
160
    {
161
        if ($sort) {
162
            $this->options[ 'json' ][ 'sort' ] = $sort;
163
        }
164
        return $this;
165
    }
166
167
    /**
168
     * Setter for mandatory request max results limiter.
169
     *
170
     * @param integer $maxResults
171
     * @return Client
172
     */
173
    public function setMaxresults($maxResults = 100)
174
    {
175
        if ($maxResults) {
176
            $this->options[ 'json' ][ 'maxResults' ] = $maxResults;
177
        }
178
        return $this;
179
    }
180
181
    /**
182
     * Setter for optional request locale.
183
     *
184
     * @param string $locale
185
     * @return Client
186
     */
187
    public function setLocale($locale)
188
    {
189
        if ($locale) {
190
            $this->options[ 'json' ][ 'locale' ] = $locale;
191
        }
192
        return $this;
193
    }
194
195
    /**
196
     * Setter for mandatory time granularity.
197
     *
198
     * @param string $granularity
199
     * @return Client
200
     */
201
    public function setTimeGranularity($granularity)
202
    {
203
        $this->options[ 'json' ][ 'granularity' ] = $granularity;
204
        return $this;
205
    }
206
207
    /**
208
     * Setter for optional currency code.
209
     *
210
     * @param string $currencyCode
211
     * @return Client
212
     */
213
    public function setCurrencyCode($currencyCode)
214
    {
215
        if ($currencyCode) {
216
            $this->options[ 'json' ][ 'currencyCode' ] = $currencyCode;
217
        }
218
        return $this;
219
    }
220
221
    /**
222
     * Setter for optional flag.
223
     *
224
     * @param string $name
225
     * @param boolean $flag
226
     * @return Client
227
     */
228
    public function setFlag($name, $flag)
229
    {
230
        if ($flag) {
231
            $this->options[ 'json' ][ $name ] = true;
232
        }
233
        return $this;
234
    }
235
236
    /**
237
     * Setter for date range.
238
     *
239
     * @param string $name
240
     * @param array $range
241
     * @return Client
242
     */
243
    public function setDateRange($name, $range)
244
    {
245
        if ($range) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $range of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
246
            $this->options[ 'json' ][ $name ] = $range;
247
        }
248
        return $this;
249
    }
250
251
    /**
252
     * Setter for sort order.
253
     *
254
     * @param string $by
255
     * @param string $direction
256
     * @return Client
257
     */
258
    public function setOrder($by, $direction = null)
259
    {
260
        if ($by) {
261
            $this->options[ 'json' ][ 'orderBy' ] = $by;
262
263
            if ($direction) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $direction of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
264
                $this->options[ 'json' ][ 'sortDir' ] = $direction;
265
            }
266
        }
267
        return $this;
268
    }
269
270
    /**
271
     * Setter for record range.
272
     *
273
     * @param string $from
274
     * @param string $count
275
     * @return Client
276
     */
277
    public function setRecordRange($from = null, $count = null)
278
    {
279
        if ($from) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $from of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
280
            $this->options[ 'json' ][ 'fromRecord' ] = $from;
281
        }
282
283
        if ($count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
284
            $this->options[ 'json' ][ 'recordCount' ] = $count;
285
        }
286
287
        return $this;
288
    }
289
290
    /**
291
     * Setter for optional wallet.
292
     *
293
     * @param string $wallet
294
     * @return Client
295
     */
296
    public function setwallet($wallet)
297
    {
298
        if ($wallet) {
299
            $this->options[ 'json' ][ 'wallet' ] = $wallet;
300
        }
301
        return $this;
302
    }
303
304
    /**
305
     * Dispatch the request and provide hooks for error handling for the response.
306
     *
307
     * @return object stdClass
308
     */
309
    public function send()
310
    {
311
        $response = $this->guzzleClient->request($this->method, $this->uri, $this->options);
312
313
        $status = $this->getStatus($response);
0 ignored issues
show
Unused Code introduced by
$status 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...
314
315
        $body = $this->getBody($response);
316
317
        if (is_object($body) && isset($body->status) && $body->status != 'SUCCESS') {
318
            $this->handleApiException($body->error);
319
        }
320
321
        return $body;
322
    }
323
324
    /**
325
     * Get status code from http response.
326
     *
327
     * @param Response $response
328
     * @return integer
329
     */
330
    protected function getStatus(Response $response)
331
    {
332
        return (int) $response->getStatusCode();
333
    }
334
335
    /**
336
     * Get http response body, cast to json and decode.
337
     *
338
     * @param Response $response
339
     * @return array
340
     */
341
    protected function getBody(Response $response)
342
    {
343
        return json_decode((string) $response->getBody());
344
    }
345
346
    /**
347
     * Stub for API exception handling.
348
     *
349
     * @param String $exception
350
     * @return void
351
     */
352
    protected function handleApiException($exception) {
353
        throw new Exception('API request failure. API Exception Message: '.$exception);
354
    }
355
}
356