1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013 Mailgun |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Mailgun\Exception; |
11
|
|
|
|
12
|
|
|
use Mailgun\Exception; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class HttpClientException extends \RuntimeException implements Exception |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ResponseInterface|null |
22
|
|
|
*/ |
23
|
|
|
private $response; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $responseBody = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $responseCode; |
34
|
|
|
|
35
|
2 |
|
public function __construct(string $message, int $code, ResponseInterface $response) |
36
|
|
|
{ |
37
|
2 |
|
parent::__construct($message, $code); |
38
|
|
|
|
39
|
2 |
|
$this->response = $response; |
40
|
2 |
|
$this->responseCode = $response->getStatusCode(); |
41
|
2 |
|
$body = $response->getBody()->__toString(); |
42
|
2 |
|
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { |
43
|
1 |
|
$this->responseBody['message'] = $body; |
44
|
|
|
} else { |
45
|
1 |
|
$this->responseBody = json_decode($body, true); |
46
|
|
|
} |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
public static function badRequest(ResponseInterface $response) |
50
|
|
|
{ |
51
|
2 |
|
$body = $response->getBody()->__toString(); |
52
|
2 |
|
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) { |
53
|
1 |
|
$validationMessage = $body; |
54
|
|
|
} else { |
55
|
1 |
|
$jsonDecoded = json_decode($body, true); |
56
|
1 |
|
$validationMessage = isset($jsonDecoded['message']) ? $jsonDecoded['message'] : $body; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$message = sprintf("The parameters passed to the API were invalid. Check your inputs!\n\n%s", $validationMessage); |
60
|
|
|
|
61
|
2 |
|
return new self($message, 400, $response); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function unauthorized(ResponseInterface $response) |
65
|
|
|
{ |
66
|
|
|
return new self('Your credentials are incorrect.', 401, $response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function requestFailed(ResponseInterface $response) |
70
|
|
|
{ |
71
|
|
|
return new self('Parameters were valid but request failed. Try again.', 402, $response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function notFound(ResponseInterface $response) |
75
|
|
|
{ |
76
|
|
|
return new self('The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.', 404, $response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function payloadTooLarge(ResponseInterface $response) |
80
|
|
|
{ |
81
|
|
|
return new self('Payload too large, your total attachment size is too big.', 413, $response); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getResponse(): ?ResponseInterface |
85
|
|
|
{ |
86
|
|
|
return $this->response; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getResponseBody(): array |
90
|
|
|
{ |
91
|
|
|
return $this->responseBody; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getResponseCode(): int |
95
|
|
|
{ |
96
|
|
|
return $this->responseCode; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|