1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sarnado\Converter\API; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Sarnado\Converter\Exceptions\BadAPIResponseException; |
8
|
|
|
use Sarnado\Converter\Exceptions\BadRequestException; |
9
|
|
|
use Sarnado\Converter\Exceptions\ConverterServerErrorException; |
10
|
|
|
use Sarnado\Converter\Exceptions\DefaultAPIException; |
11
|
|
|
use Sarnado\Converter\Exceptions\ReachedRateLimitException; |
12
|
|
|
use Sarnado\Converter\Exceptions\UnauthorizedException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class APIResponse |
16
|
|
|
* @package Sarnado\Converter\API |
17
|
|
|
*/ |
18
|
|
|
class APIResponse |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
private $success; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private $result; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* APIResponse constructor. |
32
|
|
|
* @param int $statusCode |
33
|
|
|
* @param string $body |
34
|
|
|
* @throws ReachedRateLimitException|UnauthorizedException|DefaultAPIException|ConverterServerErrorException|BadRequestException|BadAPIResponseException |
35
|
|
|
*/ |
36
|
51 |
|
public function __construct(int $statusCode, string $body) |
37
|
|
|
{ |
38
|
51 |
|
$parsed = $this->responseParser($statusCode, $body); |
39
|
|
|
|
40
|
36 |
|
if (!$this->isValidResponseData($parsed)) |
41
|
|
|
{ |
42
|
6 |
|
throw new BadAPIResponseException(); |
43
|
|
|
} |
44
|
30 |
|
$this->setSuccess((bool) $parsed['success']); |
45
|
30 |
|
$this->setResult((array) $parsed['result']); |
46
|
30 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
3 |
|
public function getSuccess(): bool |
52
|
|
|
{ |
53
|
3 |
|
return $this->success; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param bool $success |
58
|
|
|
*/ |
59
|
30 |
|
public function setSuccess(bool $success) |
60
|
|
|
{ |
61
|
30 |
|
$this->success = $success; |
62
|
30 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
30 |
|
public function getResult(): array |
68
|
|
|
{ |
69
|
30 |
|
return $this->result; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $result |
74
|
|
|
*/ |
75
|
30 |
|
public function setResult(array $result) |
76
|
|
|
{ |
77
|
30 |
|
$this->result = $result; |
78
|
30 |
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $statusCode |
83
|
|
|
* @param string $body |
84
|
|
|
* @return mixed |
85
|
|
|
* @throws ReachedRateLimitException|UnauthorizedException|DefaultAPIException|ConverterServerErrorException|BadRequestException |
86
|
|
|
*/ |
87
|
51 |
|
private function responseParser(int $statusCode, string $body) |
88
|
|
|
{ |
89
|
34 |
|
switch ($statusCode) |
90
|
|
|
{ |
91
|
51 |
|
case 200: |
92
|
36 |
|
return json_decode($body, true); |
93
|
|
|
|
94
|
15 |
|
case 404: |
95
|
3 |
|
throw new BadRequestException(); |
96
|
|
|
|
97
|
12 |
|
case 429: |
98
|
3 |
|
throw new ReachedRateLimitException(); |
99
|
|
|
|
100
|
9 |
|
case 500: |
101
|
3 |
|
throw new ConverterServerErrorException(); |
102
|
|
|
|
103
|
6 |
|
case 401: |
104
|
3 |
|
throw new UnauthorizedException(); |
105
|
|
|
|
106
|
|
|
default: |
107
|
3 |
|
throw new DefaultAPIException($statusCode . ' http response status code'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $parsed |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
36 |
|
private function isValidResponseData($parsed): bool |
116
|
|
|
{ |
117
|
36 |
|
if (!isset($parsed['success'], $parsed['result'])) |
118
|
|
|
{ |
119
|
3 |
|
return false; |
120
|
|
|
} |
121
|
33 |
|
return $parsed['success'] === true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|