KOBASSoftware /
api-client-php
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Kobas\APIClient; |
||||
| 4 | |||||
| 5 | use Kobas\APIClient\Auth\Provider; |
||||
| 6 | use Kobas\APIClient\Exception\CurlException; |
||||
| 7 | use Kobas\APIClient\Exception\HttpException; |
||||
| 8 | use Kobas\APIClient\Request\Curl; |
||||
| 9 | use Kobas\APIClient\Request\HttpRequest; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Class Client |
||||
| 13 | * |
||||
| 14 | * @package Kobas |
||||
| 15 | */ |
||||
| 16 | class Client |
||||
| 17 | { |
||||
| 18 | /** |
||||
| 19 | * @var string |
||||
| 20 | */ |
||||
| 21 | protected $api_base_url = 'https://api.kobas.co.uk'; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * @var string |
||||
| 25 | */ |
||||
| 26 | protected $version = 'v3'; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * @var Provider |
||||
| 30 | */ |
||||
| 31 | protected $oAuthProvider; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * @var Curl|HttpRequest|null |
||||
| 35 | */ |
||||
| 36 | protected $request; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @var array |
||||
| 40 | */ |
||||
| 41 | protected $headers; |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * @var array |
||||
| 45 | */ |
||||
| 46 | protected $curl_options; |
||||
| 47 | |||||
| 48 | |||||
| 49 | protected $request_info; |
||||
| 50 | |||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * Client constructor. |
||||
| 54 | * |
||||
| 55 | * @param Provider $oAuthProvider |
||||
| 56 | * @param HttpRequest|null $request |
||||
| 57 | * @param array $headers |
||||
| 58 | * @param array $curl_options |
||||
| 59 | */ |
||||
| 60 | 6 | public function __construct( |
|||
| 61 | Provider $oAuthProvider, |
||||
| 62 | HttpRequest $request = null, |
||||
| 63 | $headers = array(), |
||||
| 64 | $curl_options = array() |
||||
| 65 | ) { |
||||
| 66 | 6 | $this->oAuthProvider = $oAuthProvider; |
|||
| 67 | 6 | if ($request == null) { |
|||
| 68 | 6 | $request = new Curl(); |
|||
| 69 | 4 | } |
|||
| 70 | 6 | $this->request = $request; |
|||
| 71 | 6 | $this->headers = $headers; |
|||
| 72 | 6 | $this->curl_options = $curl_options; |
|||
| 73 | 6 | } |
|||
| 74 | |||||
| 75 | /** |
||||
| 76 | * @return string |
||||
| 77 | */ |
||||
| 78 | 3 | public function getApiBaseUrl() |
|||
| 79 | { |
||||
| 80 | 3 | return $this->api_base_url; |
|||
| 81 | } |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * @return string |
||||
| 85 | */ |
||||
| 86 | 3 | public function getVersion() |
|||
| 87 | { |
||||
| 88 | 3 | return $this->version; |
|||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Allows you to set the API Base URL. Default value is 'https://api.kobas.co.uk' |
||||
| 93 | * |
||||
| 94 | * @param $url |
||||
| 95 | */ |
||||
| 96 | 3 | public function setAPIBaseURL($url) |
|||
| 97 | { |
||||
| 98 | 3 | $this->api_base_url = $url; |
|||
| 99 | 3 | } |
|||
| 100 | |||||
| 101 | /** |
||||
| 102 | * Allows you to set the API version to use. Default value is 'v2' |
||||
| 103 | * |
||||
| 104 | * @param $version |
||||
| 105 | */ |
||||
| 106 | 3 | public function setAPIVersion($version) |
|||
| 107 | { |
||||
| 108 | 3 | $this->version = $version; |
|||
| 109 | 3 | } |
|||
| 110 | |||||
| 111 | /** |
||||
| 112 | * @param $route |
||||
| 113 | * @param array $params |
||||
| 114 | * @param array $headers |
||||
| 115 | * @return mixed |
||||
| 116 | * @throws CurlException |
||||
| 117 | * @throws Exception\AuthenticationException |
||||
| 118 | * @throws HttpException |
||||
| 119 | */ |
||||
| 120 | public function get($route, array $params = array(), array $headers = array()) |
||||
| 121 | { |
||||
| 122 | return $this->call('GET', $route, $params, $headers); |
||||
| 123 | } |
||||
| 124 | |||||
| 125 | |||||
| 126 | /** |
||||
| 127 | * @param $http_method |
||||
| 128 | * @param $route |
||||
| 129 | * @param array $params |
||||
| 130 | * @param array $headers |
||||
| 131 | * @return mixed |
||||
| 132 | * @throws CurlException |
||||
| 133 | * @throws Exception\AuthenticationException |
||||
| 134 | * @throws HttpException |
||||
| 135 | */ |
||||
| 136 | protected function call($http_method, $route, array $params = array(), array $headers = array()) |
||||
| 137 | { |
||||
| 138 | $url = $this->api_base_url . '/'; |
||||
| 139 | if (!empty($this->version)) { |
||||
| 140 | $url .= $this->version . '/'; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | $url .= $route; |
||||
| 144 | |||||
| 145 | $this->request |
||||
| 146 | ->init() |
||||
|
0 ignored issues
–
show
|
|||||
| 147 | ->setOption(CURLOPT_CUSTOMREQUEST, strtoupper($http_method)) |
||||
| 148 | ->setOption(CURLOPT_RETURNTRANSFER, true) |
||||
| 149 | ->setOption(CURLOPT_FOLLOWLOCATION, true) |
||||
| 150 | ->setOption(CURLOPT_ENCODING, ''); |
||||
| 151 | |||||
| 152 | foreach ($this->curl_options as $option => $value) { |
||||
| 153 | $this->request->setOption($option, $value); |
||||
| 154 | } |
||||
| 155 | |||||
| 156 | $headers = array_merge($this->headers, $headers); |
||||
| 157 | $headers['Authorization'] = "Bearer " . $this->oAuthProvider->getAccessToken(); |
||||
| 158 | $headers['x-kobas-company-id'] = $this->oAuthProvider->getCompanyId(); |
||||
| 159 | $headers['Content-Type'] = 'application/json'; |
||||
| 160 | |||||
| 161 | switch ($http_method) { |
||||
| 162 | case 'POST': |
||||
| 163 | $this->request->setOption(CURLOPT_POSTFIELDS, json_encode($params)); |
||||
| 164 | break; |
||||
| 165 | case 'DELETE': |
||||
| 166 | case 'PUT': |
||||
| 167 | $this->request->setOption(CURLOPT_POSTFIELDS, json_encode($params)); |
||||
| 168 | break; |
||||
| 169 | case 'GET': |
||||
| 170 | if (count($params)) { |
||||
| 171 | $url .= "?" . http_build_query($params); |
||||
| 172 | } |
||||
| 173 | break; |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | $this->request->setUrl($url); |
||||
| 177 | |||||
| 178 | $requestHeaders = array(); |
||||
| 179 | foreach ($headers as $key => $value) { |
||||
| 180 | $requestHeaders[] = $key . ': ' . $value; |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | $this->request->setOption(CURLOPT_HTTPHEADER, $requestHeaders); |
||||
| 184 | |||||
| 185 | $result = $this->request->execute(); |
||||
| 186 | |||||
| 187 | if ($this->request->getErrorNumber()) { |
||||
| 188 | throw new CurlException($this->request->getErrorMessage(), $this->request->getErrorNumber()); |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | $this->request_info = $this->request->getAllInfo(); |
||||
| 192 | $last_response = $this->request->getInfo(CURLINFO_HTTP_CODE); |
||||
| 193 | |||||
| 194 | $this->request->close(); |
||||
| 195 | |||||
| 196 | if ($last_response >= 400) { |
||||
| 197 | throw new HttpException(json_encode(json_decode($result, true)), $last_response); |
||||
|
0 ignored issues
–
show
It seems like
$result can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 198 | } |
||||
| 199 | |||||
| 200 | return json_decode($result, true); |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | |||||
| 204 | /** |
||||
| 205 | * @param $route |
||||
| 206 | * @param array $params |
||||
| 207 | * @param array $headers |
||||
| 208 | * @return mixed |
||||
| 209 | * @throws CurlException |
||||
| 210 | * @throws Exception\AuthenticationException |
||||
| 211 | * @throws HttpException |
||||
| 212 | */ |
||||
| 213 | public function post($route, array $params = array(), array $headers = array()) |
||||
| 214 | { |
||||
| 215 | return $this->call('POST', $route, $params, $headers); |
||||
| 216 | } |
||||
| 217 | |||||
| 218 | |||||
| 219 | /** |
||||
| 220 | * @param $route |
||||
| 221 | * @param array $params |
||||
| 222 | * @param array $headers |
||||
| 223 | * @return mixed |
||||
| 224 | * @throws CurlException |
||||
| 225 | * @throws Exception\AuthenticationException |
||||
| 226 | * @throws HttpException |
||||
| 227 | */ |
||||
| 228 | public function put($route, array $params = array(), array $headers = array()) |
||||
| 229 | { |
||||
| 230 | return $this->call('PUT', $route, $params, $headers); |
||||
| 231 | } |
||||
| 232 | |||||
| 233 | |||||
| 234 | /** |
||||
| 235 | * @param $route |
||||
| 236 | * @param array $params |
||||
| 237 | * @param array $headers |
||||
| 238 | * @return mixed |
||||
| 239 | * @throws CurlException |
||||
| 240 | * @throws Exception\AuthenticationException |
||||
| 241 | * @throws HttpException |
||||
| 242 | */ |
||||
| 243 | public function delete($route, array $params = array(), array $headers = array()) |
||||
| 244 | { |
||||
| 245 | return $this->call('DELETE', $route, $params, $headers); |
||||
| 246 | } |
||||
| 247 | |||||
| 248 | /** |
||||
| 249 | * @return array|false |
||||
| 250 | */ |
||||
| 251 | public function getRequestInfo() |
||||
| 252 | { |
||||
| 253 | return $this->request_info; |
||||
| 254 | } |
||||
| 255 | } |
||||
| 256 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.