Completed
Push — master ( dfec85...be4840 )
by Peter
02:18
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 27
Bugs 0 Features 3
Metric Value
wmc 17
c 27
b 0
f 3
lcom 1
cbo 3
dl 0
loc 138
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setMethod() 0 5 1
A setEndPoint() 0 5 1
A addHeader() 0 5 1
A authHeaders() 0 8 2
A setFormData() 0 5 1
A __construct() 0 5 2
A setParams() 0 9 3
A send() 0 12 4
A getBody() 0 4 1
A handleApiException() 0 3 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 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
Bug introduced by
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