|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaignApi\Client; |
|
7
|
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\BadRequestHttpException; |
|
9
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\ClientErrorHttpException; |
|
10
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\NotFoundHttpException; |
|
11
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\RedirectionHttpException; |
|
12
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\ServerErrorHttpException; |
|
13
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\UnauthorizedHttpException; |
|
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException; |
|
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class HttpExceptionHandler |
|
20
|
|
|
*/ |
|
21
|
|
|
class HttpExceptionHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param RequestInterface $request |
|
25
|
|
|
* @param ResponseInterface $response |
|
26
|
|
|
* @return ResponseInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
public function transformResponseToException( |
|
29
|
|
|
RequestInterface $request, |
|
30
|
|
|
ResponseInterface $response |
|
31
|
|
|
): ResponseInterface { |
|
32
|
|
|
if ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) { |
|
33
|
|
|
throw new RedirectionHttpException($response->getReasonPhrase(), $request, $response); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($response->getStatusCode() === 400) { |
|
37
|
|
|
throw new BadRequestHttpException($response->getReasonPhrase(), $request, $response); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($response->getStatusCode() === 401) { |
|
41
|
|
|
throw new UnauthorizedHttpException($response->getReasonPhrase(), $request, $response); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($response->getStatusCode() === 404) { |
|
45
|
|
|
throw new NotFoundHttpException($response->getReasonPhrase(), $request, $response); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($response->getStatusCode() === 422) { |
|
49
|
|
|
throw new UnprocessableEntityHttpException($response->getReasonPhrase(), $request, $response); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) { |
|
53
|
|
|
throw new ClientErrorHttpException($response->getReasonPhrase(), $request, $response); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { |
|
57
|
|
|
throw new ServerErrorHttpException($response->getReasonPhrase(), $request, $response); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $response; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|