Completed
Push — master ( e749c4...0ba39a )
by Peter
02:21
created

Client::setFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
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 PeterColes\Betfair\Api\BettingTypes\MarketFilter;
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 $headers
61
     * @return Client
62
     */
63
    public function addHeaders(array $headers)
64
    {
65
        $this->options[ 'headers' ] = array_merge($this->options[ 'headers' ], $headers);
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).
98
     *
99
     * @param  MarketFilter $filter
100
     * @return Client
101
     */
102
    public function setFilter(MarketFilter $filter) {
103
        $this->options[ 'json' ][ 'filter' ] = $filter;
104
        return $this;
105
    }
106
107
    /**
108
     * Setter for request locale.
109
     * It's optional, so we only pass a value if there is one
110
     *
111
     * @param string|null $locale
112
     * @return Client
113
     */
114
    public function setLocale($locale) {
115
        if ($locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale 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...
116
            $this->options[ 'json' ][ 'locale' ] = $locale;
117
        }
118
        return $this;
119
    }
120
121
    /**
122
     * Dispatch the request and provide hooks for error handling for the response.
123
     *
124
     * @return object stdClass
125
     */
126
    public function send()
127
    {
128
        $response = $this->guzzleClient->request($this->method, $this->uri, $this->options);
129
130
        $status = $this->getStatus($response);
131
132
        if ($status != 200) {
133
            $this->handleHttpException($status);
134
        }
135
136
        $body = $this->getBody($response);
137
138
        if (is_object($body) && $body->status != 'SUCCESS') {
139
            $this->handleApiException($body->error);
140
        }
141
142
        return $body;
143
    }
144
145
    /**
146
     * Get status code from http response.
147
     *
148
     * @param Response $response
149
     * @return integer
150
     */
151
    protected function getStatus(Response $response)
152
    {
153
        return (int) $response->getStatusCode();
154
    }
155
156
    /**
157
     * Get http response body, cast to json and decode.
158
     *
159
     * @param Response object $response
160
     * @return array
161
     */
162
    protected function getBody(Response $response)
163
    {
164
        return json_decode((string) $response->getBody());
165
    }
166
167
    /**
168
     * Stub for http exception handling.
169
     *
170
     * @param Integer $status
171
     * @return void
172
     */
173
    protected function handleHttpException($status) {
174
        throw new Exception('Http request failure. Http Exception Code: '.$status);
175
    }
176
177
    /**
178
     * Stub for API exception handling.
179
     *
180
     * @param String $exception
181
     * @return void
182
     */
183
    protected function handleApiException($exception) {
184
        throw new Exception('API request failure. API Exception Message: '.$exception);
185
    }
186
}
187