Completed
Push — master ( 92d5e3...dfec85 )
by Peter
02:21
created

Client::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PeterColes\Betfair\Http;
4
5
use Exception;
6
use GuzzleHttp\Client as GuzzleClient;
7
use Psr\Http\Message\ResponseInterface;
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 $httpClient
25
     */
26
    public function __construct($httpClient = null)
27
    {
28
        $this->guzzleClient = $httpClient ?: new GuzzleClient;
29
        $this->options[ 'headers' ] = [ 'Accept' => 'application/json' ];
30
    }
31
32
    /**
33
     * Setter for request method.
34
     *
35
     * @param string $method
36
     * @return Client
37
     */
38
    public function setMethod(string $method)
39
    {
40
        $this->method = $method;
41
        return $this;
42
    }
43
44
    /**
45
     * Setter for request end point URI.
46
     *
47
     * @param string $endPoint
48
     * @return Client
49
     */
50
    public function setEndPoint(string $endPoint)
51
    {
52
        $this->uri = $endPoint;
53
        return $this;
54
    }
55
56
    /**
57
     * Setter for request headers.
58
     *
59
     * @param array $header
60
     * @return Client
61
     */
62
    public function addHeader(array $header)
63
    {
64
        $this->options[ 'headers' ] += $header;
65
        return $this;
66
    }
67
68
    /**
69
     * Setter for authentication headers.
70
     *
71
     * @param array $headers
72
     * @return Client
73
     */
74
    public function authHeaders(array $headers = [ ])
75
    {
76
        if (count($headers) == 0) {
77
            $headers = [ 'X-Application' => Auth::$appKey, 'X-Authentication' => Auth::$sessionToken ];
78
        }
79
        $this->options[ 'headers' ] = array_merge($this->options[ 'headers' ], $headers);
80
        return $this;
81
    }
82
83
    /**
84
     * Setter for request form data.
85
     *
86
     * @param array $formData
87
     * @return Client
88
     */
89
    public function setFormData(array $formData)
90
    {
91
        $this->options[ 'form_params' ] = $formData;
92
        return $this;
93
    }
94
95
    /**
96
     * Setter for request filter(s). mandatory, but can be empty
97
     *
98
     * @param  array $filter
99
     * @return Client
100
     */
101
    public function setFilter($filter = null)
102
    {
103
        $this->options[ 'json' ][ 'filter' ] = $filter ?: new stdClass;
104
        return $this;
105
    }
106
107
    /**
108
     * Setter for marketId(s).
109
     * This param is sometime mandatory, but othertimes breaks the API if an empty value is provided
110
     *
111
     * @param  array $marketIds
112
     * @param  boolean $mandatory
113
     * @return Client
114
     */
115
    public function setMarketIds($marketIds = null, $mandatory = false)
116
    {
117
        if ($marketIds || $mandatory) {
118
            $this->options[ 'json' ][ 'marketIds' ] = $marketIds ?: new stdClass;
119
        }
120
        return $this;
121
    }
122
123
    /**
124
     * Setter for optional betId(s).
125
     *
126
     * @param  array $betIds
127
     * @return Client
128
     */
129
    public function setBetIds($betIds = null)
130
    {
131
        if ($betIds) {
132
            $this->options[ 'json' ][ 'betIds' ] = $betIds ?: new stdClass;
133
        }
134
        return $this;
135
    }
136
137
    /**
138
     * Setter for optional market projection, i.e. what market-related data should be returned.
139
     *
140
     * @param  string $name
141
     * @param  array $projection
142
     * @return Client
143
     */
144
    public function setProjection($name, $projection = null)
145
    {
146
        if ($projection) {
147
            $this->options[ 'json' ][ $name ] = $projection;
148
        }
149
        return $this;
150
    }
151
152
    /**
153
     * Setter for optional request sort.
154
     *
155
     * @param string $sort
156
     * @return Client
157
     */
158
    public function setSort($sort)
159
    {
160
        if ($sort) {
161
            $this->options[ 'json' ][ 'sort' ] = $sort;
162
        }
163
        return $this;
164
    }
165
166
    /**
167
     * Setter for mandatory request max results limiter.
168
     *
169
     * @param integer $maxResults
170
     * @return Client
171
     */
172
    public function setMaxresults($maxResults = 100)
173
    {
174
        if ($maxResults) {
175
            $this->options[ 'json' ][ 'maxResults' ] = $maxResults;
176
        }
177
        return $this;
178
    }
179
180
    /**
181
     * Setter for optional request locale.
182
     *
183
     * @param string $locale
184
     * @return Client
185
     */
186
    public function setLocale($locale)
187
    {
188
        if ($locale) {
189
            $this->options[ 'json' ][ 'locale' ] = $locale;
190
        }
191
        return $this;
192
    }
193
194
    /**
195
     * Setter for mandatory time granularity.
196
     *
197
     * @param string $granularity
198
     * @return Client
199
     */
200
    public function setTimeGranularity($granularity)
201
    {
202
        $this->options[ 'json' ][ 'granularity' ] = $granularity;
203
        return $this;
204
    }
205
206
    /**
207
     * Setter for optional currency code.
208
     *
209
     * @param string $currencyCode
210
     * @return Client
211
     */
212
    public function setCurrencyCode($currencyCode)
213
    {
214
        if ($currencyCode) {
215
            $this->options[ 'json' ][ 'currencyCode' ] = $currencyCode;
216
        }
217
        return $this;
218
    }
219
220
    /**
221
     * Setter for optional flag.
222
     *
223
     * @param string $name
224
     * @param boolean $flag
225
     * @return Client
226
     */
227
    public function setFlag($name, $flag)
228
    {
229
        if ($flag) {
230
            $this->options[ 'json' ][ $name ] = true;
231
        }
232
        return $this;
233
    }
234
235
    /**
236
     * Setter for date range.
237
     *
238
     * @param string $name
239
     * @param array $range
240
     * @return Client
241
     */
242
    public function setDateRange($name, $range)
243
    {
244
        if (!empty($range)) {
245
            $this->options[ 'json' ][ $name ] = $range;
246
        }
247
        return $this;
248
    }
249
250
    /**
251
     * Setter for sort order.
252
     *
253
     * @param string $by
254
     * @param string $direction
255
     * @return Client
256
     */
257
    public function setOrder($by, $direction = null)
258
    {
259
        if ($by) {
260
            $this->options[ 'json' ][ 'orderBy' ] = $by;
261
262
            if (!empty($direction)) {
263
                $this->options[ 'json' ][ 'sortDir' ] = $direction;
264
            }
265
        }
266
        return $this;
267
    }
268
269
    /**
270
     * Setter for record range.
271
     *
272
     * @param int $from
273
     * @param int $count
274
     * @return Client
275
     */
276
    public function setRecordRange($from = 0, $count = 1000)
277
    {
278
        $this->options[ 'json' ][ 'fromRecord' ] = $from;
279
        $this->options[ 'json' ][ 'recordCount' ] = $count;
280
        return $this;
281
    }
282
283
    /**
284
     * Setter for optional wallet.
285
     *
286
     * @param string $wallet
287
     * @return Client
288
     */
289
    public function setwallet($wallet)
290
    {
291
        if ($wallet) {
292
            $this->options[ 'json' ][ 'wallet' ] = $wallet;
293
        }
294
        return $this;
295
    }
296
297
    /**
298
     * Dispatch the request and provide hooks for error handling for the response.
299
     *
300
     * @return object stdClass
301
     */
302
    public function send()
303
    {
304
        $response = $this->guzzleClient->request($this->method, $this->uri, $this->options);
305
306
        $body = $this->getBody($response);
307
308
        if (is_object($body) && isset($body->status) && $body->status != 'SUCCESS') {
309
            $this->handleApiException($body->error);
310
        }
311
312
        return $body;
313
    }
314
315
    /**
316
     * Get http response body, cast to json and decode.
317
     *
318
     * @param ResponseInterface $response
319
     * @return array
320
     */
321
    protected function getBody(ResponseInterface $response)
322
    {
323
        return json_decode((string) $response->getBody());
324
    }
325
326
    /**
327
     * Stub for API exception handling.
328
     *
329
     * @param String $exception
330
     */
331
    protected function handleApiException($exception) {
332
        throw new Exception('API request failure. API Exception Message: '.$exception);
333
    }
334
}
335