1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | namespace Worldline\Connect\Sdk\V1; |
7: | |
8: | use RuntimeException; |
9: | use Worldline\Connect\Sdk\Domain\DataObject; |
10: | use Worldline\Connect\Sdk\V1\Domain\APIError; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | class ResponseException extends RuntimeException |
18: | { |
19: | |
20: | private $httpStatusCode; |
21: | |
22: | |
23: | |
24: | |
25: | private $response; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | public function __construct($httpStatusCode, DataObject $response, $message = null) |
33: | { |
34: | if (is_null($message)) { |
35: | $message = 'the Worldline Global Collect platform returned an error response'; |
36: | } |
37: | parent::__construct($message); |
38: | $this->httpStatusCode = $httpStatusCode; |
39: | $this->response = $response; |
40: | } |
41: | |
42: | public function __toString() |
43: | { |
44: | return sprintf( |
45: | "exception '%s' with message '%s'. in %s:%d\nHTTP status code: %s\nResponse:\n%s\nStack trace:\n%s", |
46: | __CLASS__, |
47: | $this->getMessage(), |
48: | $this->getFile(), |
49: | $this->getLine(), |
50: | $this->getHttpStatusCode(), |
51: | json_encode($this->getResponse(), JSON_PRETTY_PRINT), |
52: | $this->getTraceAsString() |
53: | ); |
54: | } |
55: | |
56: | |
57: | |
58: | |
59: | public function getHttpStatusCode() |
60: | { |
61: | return $this->httpStatusCode; |
62: | } |
63: | |
64: | |
65: | |
66: | |
67: | public function getResponse() |
68: | { |
69: | return $this->response; |
70: | } |
71: | |
72: | |
73: | |
74: | |
75: | public function getErrorId() |
76: | { |
77: | $responseVariables = get_object_vars($this->getResponse()); |
78: | if (!array_key_exists('errorId', $responseVariables)) { |
79: | return ''; |
80: | } |
81: | return $responseVariables['errorId']; |
82: | } |
83: | |
84: | |
85: | |
86: | |
87: | public function getErrors() |
88: | { |
89: | $responseVariables = get_object_vars($this->getResponse()); |
90: | if (!array_key_exists('errors', $responseVariables)) { |
91: | return array(); |
92: | } |
93: | $errors = $responseVariables['errors']; |
94: | if (!is_array($errors)) { |
95: | return array(); |
96: | } |
97: | foreach ($errors as $e) { |
98: | if (!($e instanceof APIError)) { |
99: | return array(); |
100: | } |
101: | } |
102: | return $errors; |
103: | } |
104: | } |
105: | |