ServiceException::from_object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Service exception
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Exception;
14
use JsonSchema\Constraints\Constraint;
15
use JsonSchema\Exception\ValidationException;
16
use JsonSchema\Validator;
17
18
/**
19
 * Service exception
20
 *
21
 * @link https://docs.adyen.com/developers/api-reference/common-api/serviceexception
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class ServiceException extends Exception {
28
	/**
29
	 * The HTTP response status code.
30
	 *
31
	 * @var string
32
	 */
33
	private $status;
34
35
	/**
36
	 * The Adyen code that is mapped to the error message.
37
	 *
38
	 * @var string
39
	 */
40
	private $error_code;
41
42
	/**
43
	 * The type of error that was encountered.
44
	 *
45
	 * @var string
46
	 */
47
	private $error_type;
48
49
	/**
50
	 * Construct service exception.
51
	 *
52
	 * @param string $status     Status.
53
	 * @param string $error_code Error code.
54
	 * @param string $message    Message.
55
	 * @param string $error_type Error type.
56
	 */
57 6
	public function __construct( $status, $error_code, $message, $error_type ) {
58 6
		parent::__construct( $message, intval( $error_code ) );
59
60 6
		$this->status     = $status;
61 6
		$this->error_code = $error_code;
62 6
		$this->error_type = $error_type;
63 6
	}
64
65
	/**
66
	 * Get status.
67
	 *
68
	 * @return string
69
	 */
70 1
	public function get_status() {
71 1
		return $this->status;
72
	}
73
74
	/**
75
	 * Get error code.
76
	 *
77
	 * @return string
78
	 */
79 1
	public function get_error_code() {
80 1
		return $this->error_code;
81
	}
82
83
	/**
84
	 * Get message.
85
	 *
86
	 * @return string
87
	 */
88 1
	public function get_message() {
89 1
		return $this->getMessage();
90
	}
91
92
	/**
93
	 * Get error type.
94
	 *
95
	 * @return string
96
	 */
97 1
	public function get_error_type() {
98 1
		return $this->error_type;
99
	}
100
101
	/**
102
	 * Create service exception from object.
103
	 *
104
	 * @param object $object Object.
105
	 * @return ServiceException
106
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
107
	 */
108 6
	public static function from_object( $object ) {
109 6
		$validator = new Validator();
110
111 6
		$validator->validate(
112 6
			$object,
113
			(object) array(
114 6
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/service-exception.json' ),
115
			),
116 6
			Constraint::CHECK_MODE_EXCEPTIONS
117
		);
118
119
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
120 6
		return new self( $object->status, $object->errorCode, $object->message, $object->errorType );
121
	}
122
}
123