Error::from_object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 1
rs 9.9332
1
<?php
2
/**
3
 * Error
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
 * Error
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class Error extends Exception {
26
	/**
27
	 * The requested URI.
28
	 *
29
	 * @var string
30
	 */
31
	private $requested_uri;
32
33
	/**
34
	 * Construct error.
35
	 *
36
	 * @param int    $code          Code.
37
	 * @param string $message       Message.
38
	 * @param string $requested_uri Requested URI.
39
	 */
40 3
	public function __construct( $code, $message, $requested_uri ) {
41 3
		parent::__construct( $message, $code );
42
43 3
		$this->requested_uri = $requested_uri;
44 3
	}
45
46
	/**
47
	 * Get code.
48
	 *
49
	 * @return int
50
	 */
51 2
	public function get_code() {
52 2
		return intval( $this->getCode() );
53
	}
54
55
	/**
56
	 * Get message.
57
	 *
58
	 * @return string
59
	 */
60 2
	public function get_message() {
61 2
		return $this->getMessage();
62
	}
63
64
	/**
65
	 * Get requested URI.
66
	 *
67
	 * @return string
68
	 */
69 2
	public function get_requested_uri() {
70 2
		return $this->requested_uri;
71
	}
72
73
	/**
74
	 * Create error from object.
75
	 *
76
	 * @param object $object Object.
77
	 * @return Error
78
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
79
	 */
80 2
	public static function from_object( $object ) {
81 2
		$validator = new Validator();
82
83 2
		$validator->validate(
84 2
			$object,
85
			(object) array(
86 2
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/error.json' ),
87
			),
88 2
			Constraint::CHECK_MODE_EXCEPTIONS
89
		);
90
91 2
		return new self(
92 2
			$object->code,
93 2
			$object->message,
94 2
			$object->{'requested URI'}
95
		);
96
	}
97
}
98