-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApiException.php
More file actions
120 lines (98 loc) · 3.02 KB
/
ApiException.php
File metadata and controls
120 lines (98 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Pushpad\Exception;
/**
* Represents an error response returned by the Pushpad API.
*/
class ApiException extends PushpadException
{
private int $statusCode;
/** @var mixed */
private $responseBody;
/**
* @var array<string, array<int, string>>|null
*/
private ?array $responseHeaders;
private ?string $rawBody;
/**
* @param mixed $responseBody
* @param array<string, array<int, string>>|null $responseHeaders
*/
public function __construct(
string $message,
int $statusCode,
$responseBody = null,
?array $responseHeaders = null,
?string $rawBody = null
) {
parent::__construct($message, $statusCode);
$this->statusCode = $statusCode;
$this->responseBody = $responseBody;
$this->responseHeaders = $responseHeaders;
$this->rawBody = $rawBody;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* @return mixed
*/
public function getResponseBody()
{
return $this->responseBody;
}
/**
* @return array<string, array<int, string>>|null
*/
public function getResponseHeaders(): ?array
{
return $this->responseHeaders;
}
public function getRawBody(): ?string
{
return $this->rawBody;
}
/**
* @param array{status?:int, body?:mixed, headers?:array<string, array<int, string>>, raw_body?:?string} $response
*/
public static function fromResponse(array $response): self
{
$status = isset($response['status']) ? (int) $response['status'] : 0;
$body = $response['body'] ?? null;
$headers = $response['headers'] ?? null;
$rawBody = $response['raw_body'] ?? null;
$message = self::buildMessage($status, $body);
return new self($message, $status, $body, $headers, $rawBody);
}
/**
* @param mixed $body
*/
private static function buildMessage(int $status, $body): string
{
$baseMessage = sprintf('Pushpad API responded with status %d.', $status);
$details = '';
if (is_array($body)) {
foreach (['error_description', 'error', 'message'] as $key) {
if (isset($body[$key]) && is_scalar($body[$key])) {
$details = (string) $body[$key];
break;
}
}
if ($details === '' && isset($body['errors'])) {
$encoded = json_encode($body['errors']);
$details = $encoded !== false ? $encoded : '';
}
} elseif (is_scalar($body) && $body !== '') {
$details = (string) $body;
}
if ($details === '' && $body !== null) {
$encoded = json_encode($body);
$details = $encoded !== false ? $encoded : '';
}
if ($details === '' || $details === 'null') {
return $baseMessage;
}
return $baseMessage . ' ' . $details;
}
}