1 | <?php |
||
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 |
||
30 | |||
31 | 5 | public function setAccessToken(string $accessToken = null) |
|
32 | { |
||
33 | 5 | $this->accessToken = $accessToken; |
|
34 | 5 | } |
|
35 | |||
36 | public function getApiUrl(): string |
||
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) |
||
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 | } |
||
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.