1 | <?php namespace Arcanedev\Stripe\Utilities; |
||
12 | class ErrorsHandler implements ApiErrorsHandlerContract |
||
13 | { |
||
14 | /* ----------------------------------------------------------------- |
||
15 | | Properties |
||
16 | | ----------------------------------------------------------------- |
||
17 | */ |
||
18 | |||
19 | /** @var string */ |
||
20 | private $respBody; |
||
21 | |||
22 | /** @var int */ |
||
23 | private $respCode; |
||
24 | |||
25 | /** @var array */ |
||
26 | protected $respHeaders; |
||
27 | |||
28 | /** @var array */ |
||
29 | private $response = []; |
||
30 | |||
31 | /** @var array */ |
||
32 | private static $apiExceptions = [ |
||
33 | 400 => Exceptions\InvalidRequestException::class, |
||
34 | 401 => Exceptions\AuthenticationException::class, |
||
35 | 402 => Exceptions\CardException::class, |
||
36 | 403 => Exceptions\PermissionException::class, |
||
37 | 404 => Exceptions\InvalidRequestException::class, |
||
38 | 429 => Exceptions\RateLimitException::class, |
||
39 | 500 => Exceptions\ApiException::class, |
||
40 | ]; |
||
41 | |||
42 | /** @var array */ |
||
43 | private static $oauthExceptions = [ |
||
44 | 'invalid_grant' => Exceptions\OAuth\InvalidGrantException::class, |
||
45 | 'invalid_request' => Exceptions\OAuth\InvalidRequestException::class, |
||
46 | 'invalid_scope' => Exceptions\OAuth\InvalidScopeException::class, |
||
47 | 'unsupported_grant_type' => Exceptions\OAuth\UnsupportedGrantTypeException::class, |
||
48 | 'unsupported_response_type' => Exceptions\OAuth\UnsupportedResponseTypeException::class, |
||
49 | ]; |
||
50 | |||
51 | /* ----------------------------------------------------------------- |
||
52 | | Getters & Setters |
||
53 | | ----------------------------------------------------------------- |
||
54 | */ |
||
55 | |||
56 | /** |
||
57 | * Set Response body (JSON). |
||
58 | * |
||
59 | * @param string $respBody |
||
60 | * |
||
61 | * @return self |
||
62 | */ |
||
63 | 72 | private function setRespBody($respBody) |
|
69 | |||
70 | /** |
||
71 | * Set Response Code. |
||
72 | * |
||
73 | * @param int $respCode |
||
74 | * |
||
75 | * @return self |
||
76 | */ |
||
77 | 72 | private function setRespCode($respCode) |
|
83 | |||
84 | /** |
||
85 | * Set Response Headers. |
||
86 | * |
||
87 | * @param array $respHeaders |
||
88 | * |
||
89 | * @return self |
||
90 | */ |
||
91 | 72 | private function setRespHeaders($respHeaders) |
|
97 | |||
98 | /** |
||
99 | * Set Response. |
||
100 | * |
||
101 | * @param array $response |
||
102 | * |
||
103 | * @return self |
||
104 | */ |
||
105 | 72 | private function setResponse($response) |
|
113 | |||
114 | /** |
||
115 | * Get Exception class by status code. |
||
116 | * |
||
117 | * @param int $code |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 62 | private function getApiExceptionByCode($code) |
|
125 | |||
126 | /* ----------------------------------------------------------------- |
||
127 | | Main Methods |
||
128 | | ----------------------------------------------------------------- |
||
129 | */ |
||
130 | |||
131 | /** |
||
132 | * Handle API Errors. |
||
133 | * |
||
134 | * @param string $respBody |
||
135 | * @param int $respCode |
||
136 | * @param array $respHeaders |
||
137 | * @param array $response |
||
138 | * |
||
139 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
140 | * @throws \Arcanedev\Stripe\Exceptions\AuthenticationException |
||
141 | * @throws \Arcanedev\Stripe\Exceptions\CardException |
||
142 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
143 | * @throws \Arcanedev\Stripe\Exceptions\PermissionException |
||
144 | * @throws \Arcanedev\Stripe\Exceptions\RateLimitException |
||
145 | */ |
||
146 | 74 | public function handle($respBody, $respCode, $respHeaders, $response) |
|
162 | |||
163 | /* ----------------------------------------------------------------- |
||
164 | | Check Methods |
||
165 | | ----------------------------------------------------------------- |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * Check Response. |
||
170 | * |
||
171 | * @param mixed $response |
||
172 | * |
||
173 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
174 | */ |
||
175 | 72 | private function checkResponse($response) |
|
189 | |||
190 | /** |
||
191 | * Check if has Response. |
||
192 | * |
||
193 | * @param mixed $response |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | 72 | private function hasErrorResponse($response) |
|
201 | |||
202 | /** |
||
203 | * Check if has Exception. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 62 | private function hasException() |
|
211 | |||
212 | /* ------------------------------------------------------------------------------------------------ |
||
213 | | Other Functions |
||
214 | | ------------------------------------------------------------------------------------------------ |
||
215 | */ |
||
216 | /** |
||
217 | * Parse response error. |
||
218 | * |
||
219 | * @return \Arcanedev\Stripe\Exceptions\StripeException |
||
220 | */ |
||
221 | 62 | private function getAPIException() |
|
244 | |||
245 | /** |
||
246 | * Get the OAuth exception. |
||
247 | * |
||
248 | * @return \Arcanedev\Stripe\Exceptions\OAuth\OAuthException |
||
249 | */ |
||
250 | 8 | private function getOAuthException() |
|
266 | |||
267 | /** |
||
268 | * Get Error attribute. |
||
269 | * |
||
270 | * @param string $name |
||
271 | * |
||
272 | * @return mixed |
||
273 | */ |
||
274 | 62 | private function getResponseError($name) |
|
289 | } |
||
290 |