1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Waredesk\Exceptions\UnknownException; |
11
|
|
|
|
12
|
|
|
class RequestHandler |
13
|
|
|
{ |
14
|
|
|
private $mockHandler; |
15
|
|
|
private $accessToken; |
16
|
|
|
private $apiUrl; |
17
|
|
|
private $client; |
18
|
|
|
|
19
|
5 |
|
public function __construct(string $accessToken = null, string $apiUrl = null) |
20
|
|
|
{ |
21
|
5 |
|
$this->accessToken = $accessToken; |
22
|
5 |
|
$this->apiUrl = $apiUrl; |
23
|
5 |
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
24
|
5 |
|
} |
25
|
|
|
|
26
|
|
|
public function getAccessToken(): string |
27
|
|
|
{ |
28
|
|
|
return $this->accessToken; |
29
|
|
|
} |
30
|
|
|
|
31
|
5 |
|
public function setAccessToken(string $accessToken = null) |
32
|
|
|
{ |
33
|
5 |
|
$this->accessToken = $accessToken; |
34
|
5 |
|
} |
35
|
|
|
|
36
|
|
|
public function getApiUrl(): string |
37
|
|
|
{ |
38
|
|
|
return $this->apiUrl; |
39
|
|
|
} |
40
|
|
|
|
41
|
5 |
|
public function setApiUrl(string $apiUrl) |
42
|
|
|
{ |
43
|
5 |
|
$this->apiUrl = $apiUrl; |
44
|
5 |
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
45
|
5 |
|
} |
46
|
|
|
|
47
|
5 |
|
public function setMockHandler(HandlerStack $mockHandler = null) |
48
|
|
|
{ |
49
|
5 |
|
$this->mockHandler = $mockHandler; |
50
|
5 |
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
51
|
5 |
|
} |
52
|
|
|
|
53
|
1 |
|
private function handleBadResponse(ResponseInterface $response = null) |
54
|
|
|
{ |
55
|
1 |
|
if ($response) { |
56
|
1 |
|
$body = (string)$response->getBody(); |
57
|
1 |
|
$json = \GuzzleHttp\json_decode($body, true); |
58
|
1 |
|
(new ErrorHandler())($json); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
throw new UnknownException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
private function handleException(ClientException $exception) |
65
|
|
|
{ |
66
|
1 |
|
$this->handleBadResponse($exception->getResponse()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function get(string $endpoint, array $headers = [], array $params = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function post(string $endpoint, $params = null, array $headers = []): array |
74
|
|
|
{ |
75
|
2 |
|
$headers['Content-Type'] = 'application/json'; |
76
|
2 |
|
if ($this->accessToken !== null) { |
77
|
1 |
|
$headers['Authorization'] = 'Bearer ' . $this->accessToken; |
78
|
|
|
} |
79
|
2 |
|
$body = null; |
80
|
2 |
|
if ($params) { |
81
|
2 |
|
$body = \GuzzleHttp\json_encode($params); |
82
|
|
|
} |
83
|
2 |
|
$request = new GuzzleRequest('POST', $endpoint, $headers, $body); |
84
|
|
|
|
85
|
|
|
try { |
86
|
2 |
|
$response = $this->client->send($request); |
87
|
1 |
|
if ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399) { |
88
|
1 |
|
return \GuzzleHttp\json_decode((string)$response->getBody(), true); |
89
|
|
|
} |
90
|
|
|
$this->handleBadResponse($response); |
91
|
1 |
|
} catch (ClientException $e) { |
92
|
1 |
|
$this->handleException($e); |
93
|
|
|
} |
94
|
|
|
throw new UnknownException(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.