Error::set_error_message()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Error
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
/**
14
 * Error
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.1.10
18
 * @since   2.0.2
19
 */
20
class Error extends \Exception {
21
	/**
22
	 * Error code.
23
	 *
24
	 * @var int
25
	 */
26
	private $error_code;
27
28
	/**
29
	 * Error message.
30
	 *
31
	 * @var string|null
32
	 */
33
	private $error_message;
34
35
	/**
36
	 * Consumer message.
37
	 *
38
	 * @var string|null
39
	 */
40
	private $consumer_message;
41
42
	/**
43
	 * Construct error.
44
	 *
45
	 * @param int    $error_code Error code.
46
	 * @param string $message    Consumer or error message.
47
	 */
48 8
	public function __construct( $error_code, $message ) {
49 8
		parent::__construct( $message, $error_code );
50
51 8
		$this->error_code = $error_code;
52 8
	}
53
54
	/**
55
	 * Get error code.
56
	 *
57
	 * @return int
58
	 */
59 8
	public function get_error_code() {
60 8
		return $this->error_code;
61
	}
62
63
	/**
64
	 * Get error message.
65
	 *
66
	 * @return string|null
67
	 */
68 7
	public function get_error_message() {
69 7
		return $this->error_message;
70
	}
71
72
	/**
73
	 * Set error message.
74
	 *
75
	 * @param string|null $error_message Error message.
76
	 * @return void
77
	 */
78 7
	public function set_error_message( $error_message ) {
79 7
		$this->error_message = $error_message;
80 7
	}
81
82
	/**
83
	 * Get consumer message.
84
	 *
85
	 * @return string|null
86
	 */
87 7
	public function get_consumer_message() {
88 7
		return $this->consumer_message;
89
	}
90
91
	/**
92
	 * Set consumer message.
93
	 *
94
	 * @param string|null $consumer_message Consumer message.
95
	 * @return void
96
	 */
97 7
	public function set_consumer_message( $consumer_message ) {
98 7
		$this->consumer_message = $consumer_message;
99 7
	}
100
101
	/**
102
	 * Create error from object.
103
	 *
104
	 * @param object $object Object.
105
	 * @return Error
106
	 * @throws \InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
107
	 */
108 7
	public static function from_object( $object ) {
109 7
		if ( ! isset( $object->errorCode ) ) {
110
			throw new \InvalidArgumentException( 'Object must contain `errorCode` property.' );
111
		}
112
113 7
		$error_code       = $object->errorCode;
114 7
		$error_message    = null;
115 7
		$consumer_message = null;
116
117 7
		$message = \strval( $error_code );
118
119 7
		if ( isset( $object->errorMessage ) ) {
120 1
			$error_message = $object->errorMessage;
121
122 1
			$message = $error_message;
123
		}
124
125 7
		if ( isset( $object->consumerMessage ) ) {
126 6
			$consumer_message = $object->consumerMessage;
127
128 6
			$message = $consumer_message;
129
		}
130
131 7
		$error = new self( $error_code, $message );
132
133 7
		$error->set_error_message( $error_message );
134 7
		$error->set_consumer_message( $consumer_message );
135
136 7
		return $error;
137
	}
138
139
	/**
140
	 * Create error from JSON string.
141
	 *
142
	 * @param string $json JSON string.
143
	 * @return Error
144
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
145
	 */
146 7
	public static function from_json( $json ) {
147 7
		$data = \json_decode( $json );
148
149 7
		$validator = new \JsonSchema\Validator();
150
151 7
		$validator->validate(
152 7
			$data,
153
			(object) array(
154 7
				'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/error.json' ),
155
			),
156 7
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
157
		);
158
159 7
		return self::from_object( $data );
160
	}
161
}
162