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
|
|
|
public function __construct(string $accessToken = null, string $apiUrl = null) |
20
|
|
|
{ |
21
|
|
|
$this->accessToken = $accessToken; |
22
|
|
|
$this->apiUrl = $apiUrl; |
23
|
|
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getAccessToken(): string |
27
|
|
|
{ |
28
|
|
|
return $this->accessToken; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setAccessToken(string $accessToken = null) |
32
|
|
|
{ |
33
|
|
|
$this->accessToken = $accessToken; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getApiUrl(): string |
37
|
|
|
{ |
38
|
|
|
return $this->apiUrl; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setApiUrl(string $apiUrl) |
42
|
|
|
{ |
43
|
|
|
$this->apiUrl = $apiUrl; |
44
|
|
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setMockHandler(HandlerStack $mockHandler = null) |
48
|
|
|
{ |
49
|
|
|
$this->mockHandler = $mockHandler; |
50
|
|
|
$this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function handleBadResponse(ResponseInterface $response) |
54
|
|
|
{ |
55
|
|
|
$body = (string) $response->getBody(); |
56
|
|
|
$json = \GuzzleHttp\json_decode($body, true); |
57
|
|
|
ErrorHandler::error($json); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function handleException(ClientException $exception) |
61
|
|
|
{ |
62
|
|
|
$this->handleBadResponse($exception->getResponse()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get(string $endpoint, array $headers = [], array $params = null) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function post(string $endpoint, array $params = null, array $headers = []): array |
70
|
|
|
{ |
71
|
|
|
$headers['Content-Type'] = 'application/json'; |
72
|
|
|
$body = null; |
73
|
|
|
if ($params) $body = \GuzzleHttp\json_encode($params); |
74
|
|
|
$request = new GuzzleRequest('POST', $endpoint, $headers, $body); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$response = $this->client->send($request); |
78
|
|
|
if ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399) { |
79
|
|
|
return \GuzzleHttp\json_decode((string)$response->getBody(), true); |
80
|
|
|
} else { |
81
|
|
|
$this->handleBadResponse($response); |
82
|
|
|
} |
83
|
|
|
} catch (ClientException $e) { |
84
|
|
|
$this->handleException($e); |
85
|
|
|
} |
86
|
|
|
throw new UnknownException(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: