Failed Conditions
Push — master ( e22298...bcbd11 )
by Reüel
10:06 queued 11s
created

Error::get_consumer_message()   A

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 0
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-2019 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
	 */
77 7
	public function set_error_message( $error_message ) {
78 7
		$this->error_message = $error_message;
79 7
	}
80
81
	/**
82
	 * Get consumer message.
83
	 *
84
	 * @return string|null
85
	 */
86 7
	public function get_consumer_message() {
87 7
		return $this->consumer_message;
88
	}
89
90
	/**
91
	 * Set consumer message.
92
	 *
93
	 * @param string|null $consumer_message Consumer message.
94
	 */
95 7
	public function set_consumer_message( $consumer_message ) {
96 7
		$this->consumer_message = $consumer_message;
97 7
	}
98
99
	/**
100
	 * Create error from object.
101
	 *
102
	 * @param object $object Object.
103
	 * @return Error
104
	 * @throws \InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
105
	 */
106 7
	public static function from_object( $object ) {
107 7
		if ( ! isset( $object->errorCode ) ) {
108
			throw new \InvalidArgumentException( 'Object must contain `errorCode` property.' );
109
		}
110
111 7
		$error_code       = $object->errorCode;
112 7
		$error_message    = null;
113 7
		$consumer_message = null;
114
115 7
		$message = \strval( $error_code );
116
117 7
		if ( isset( $object->errorMessage ) ) {
118 1
			$error_message = $object->errorMessage;
119
120 1
			$message = $error_message;
121
		}
122
123 7
		if ( isset( $object->consumerMessage ) ) {
124 6
			$consumer_message = $object->consumerMessage;
125
126 6
			$message = $consumer_message;
127
		}
128
129 7
		$error = new self( $error_code, $message );
130
131 7
		$error->set_error_message( $error_message );
132 7
		$error->set_consumer_message( $consumer_message );
133
134 7
		return $error;
135
	}
136
137
	/**
138
	 * Create error from JSON string.
139
	 *
140
	 * @param string $json JSON string.
141
	 * @return Error
142
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
143
	 */
144 7
	public static function from_json( $json ) {
145 7
		$data = \json_decode( $json );
146
147 7
		$validator = new \JsonSchema\Validator();
148
149 7
		$validator->validate(
150 7
			$data,
151
			(object) array(
152 7
				'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/error.json' ),
153
			),
154 7
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
155
		);
156
157 7
		return self::from_object( $data );
158
	}
159
}
160