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