| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | namespace Worldline\Acquiring\Sdk\V1; |
| 6: | |
| 7: | use RuntimeException; |
| 8: | use Worldline\Acquiring\Sdk\Domain\DataObject; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | class ApiException extends RuntimeException |
| 16: | { |
| 17: | |
| 18: | |
| 19: | |
| 20: | private int $httpStatusCode; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | private DataObject $response; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public function __construct(int $httpStatusCode, DataObject $response, ?string $message = null) |
| 33: | { |
| 34: | if (is_null($message)) { |
| 35: | $message = 'The Worldline Acquiring platform returned an error response'; |
| 36: | } |
| 37: | parent::__construct($message); |
| 38: | $this->httpStatusCode = $httpStatusCode; |
| 39: | $this->response = $response; |
| 40: | } |
| 41: | |
| 42: | public function __toString(): string |
| 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(): int |
| 60: | { |
| 61: | return $this->httpStatusCode; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function getResponse(): DataObject |
| 68: | { |
| 69: | return $this->response; |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | public function getType(): ?string |
| 76: | { |
| 77: | $responseVariables = get_object_vars($this->getResponse()); |
| 78: | if (!array_key_exists('type', $responseVariables)) { |
| 79: | return ''; |
| 80: | } |
| 81: | return $responseVariables['type']; |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function getTitle(): ?string |
| 88: | { |
| 89: | $responseVariables = get_object_vars($this->getResponse()); |
| 90: | if (!array_key_exists('title', $responseVariables)) { |
| 91: | return ''; |
| 92: | } |
| 93: | return $responseVariables['title']; |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function getStatus(): ?int |
| 100: | { |
| 101: | $responseVariables = get_object_vars($this->getResponse()); |
| 102: | if (!array_key_exists('status', $responseVariables)) { |
| 103: | return 0; |
| 104: | } |
| 105: | return $responseVariables['status']; |
| 106: | } |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public function getDetail(): ?string |
| 112: | { |
| 113: | $responseVariables = get_object_vars($this->getResponse()); |
| 114: | if (!array_key_exists('detail', $responseVariables)) { |
| 115: | return ''; |
| 116: | } |
| 117: | return $responseVariables['detail']; |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | public function getInstance(): ?string |
| 124: | { |
| 125: | $responseVariables = get_object_vars($this->getResponse()); |
| 126: | if (!array_key_exists('instance', $responseVariables)) { |
| 127: | return ''; |
| 128: | } |
| 129: | return $responseVariables['instance']; |
| 130: | } |
| 131: | } |
| 132: | |