Completed
Push — master ( be4840...fe49f7 )
by Peter
02:20
created

src/Http/Client.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 params.
97
     *
98
     * @param string $params
99
     * @return Client
100
     */
101
    public function setParams($params)
102
    {
103
        if ($params) {
104
            foreach ($params as $key => $value) {
0 ignored issues
show
The expression $params of type string is not traversable.
Loading history...
105
                $this->options[ 'json' ][ $key ] = $value;
106
            }
107
        }
108
        return $this;
109
    }
110
111
    /**
112
     * Dispatch the request and provide hooks for error handling for the response.
113
     *
114
     * @return object stdClass
115
     */
116
    public function send()
117
    {
118
        $response = $this->guzzleClient->request($this->method, $this->uri, $this->options);
119
120
        $body = $this->getBody($response);
121
122
        if (is_object($body) && isset($body->status) && $body->status != 'SUCCESS') {
123
            $this->handleApiException($body->error);
124
        }
125
126
        return $body;
127
    }
128
129
    /**
130
     * Get http response body, cast to json and decode.
131
     *
132
     * @param ResponseInterface $response
133
     * @return array
134
     */
135
    protected function getBody(ResponseInterface $response)
136
    {
137
        return json_decode((string) $response->getBody());
138
    }
139
140
    /**
141
     * Stub for API exception handling.
142
     *
143
     * @param String $exception
144
     */
145
    protected function handleApiException($exception) {
146
        throw new Exception('API request failure. API Exception Message: '.$exception);
147
    }
148
}
149