Test Failed
Push — main ( e0d6a9...e106fe )
by Reüel
07:26 queued 21s
created

PaymentResponse::get_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment Response
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Payvision
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Payvision;
12
13
/**
14
 * Payment Response
15
 *
16
 * @author  Remco Tolsma
17
 * @version 1.1.0
18
 * @since   1.0.0
19
 */
20
class PaymentResponse {
21
	/**
22
	 * Redirect.
23
	 *
24
	 * @var RedirectDetails|null
25
	 */
26
	public $redirect;
27
28
	/**
29
	 * Transaction.
30
	 *
31
	 * @var TransactionResponse|null
32
	 */
33
	public $transaction;
34
35
	/**
36
	 * The result of the payment.
37
	 *
38
	 * @link https://developers.acehubpaymentservices.com/v3.3/reference#result-codes-2
39
	 * @var int
40
	 */
41
	private $result;
42
43
	/**
44
	 * A short description of the result.
45
	 *
46
	 * @var string
47
	 */
48
	private $description;
49
50
	/**
51
	 * Header.
52
	 *
53
	 * @var ResponseHeader
54
	 */
55
	private $header;
56
57
	/**
58
	 * Error.
59
	 *
60
	 * @var Error|null
61
	 */
62
	private $error;
63
64
	/**
65
	 * Construct and initialize payment response
66
	 *
67
	 * @param int            $result      Result.
68
	 * @param string         $description Description.
69
	 * @param ResponseHeader $header      Header.
70
	 */
71 2
	public function __construct( $result, $description, $header ) {
72 2
		$this->result      = $result;
73 2
		$this->description = $description;
74 2
		$this->header      = $header;
75 2
	}
76
77
	/**
78
	 * From JSON.
79
	 *
80
	 * @param object $object Object.
81
	 * @return self
82
	 * @throws \JsonSchema\Exception\ValidationException Throws exception when JSON is not valid.
83
	 */
84 1
	public static function from_json( $object ) {
85 1
		$validator = new \JsonSchema\Validator();
86
87 1
		$validator->validate(
88 1
			$object,
89
			(object) array(
90 1
				'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/payment-response.json' ),
91
			),
92 1
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
93
		);
94
95 1
		$response = new self( $object->result, $object->description, ResponseHeader::from_json( $object->header ) );
96
97 1
		if ( \property_exists( $object->body, 'transaction' ) ) {
98 1
			$response->transaction = TransactionResponse::from_json( $object->body->transaction );
99
		}
100
101 1
		if ( \property_exists( $object->body, 'redirect' ) ) {
102 1
			$response->redirect = RedirectDetails::from_json( $object->body->redirect );
103
		}
104
105 1
		if ( \property_exists( $object->body, 'error' ) ) {
106
			$response->set_error( Error::from_json( $object->body->error ) );
107
		}
108
109 1
		return $response;
110
	}
111
112
	/**
113
	 * Get result.
114
	 *
115
	 * @return int
116
	 */
117 2
	public function get_result() {
118 2
		return $this->result;
119
	}
120
121
	/**
122
	 * Get description.
123
	 *
124
	 * @return string
125
	 */
126 1
	public function get_description() {
127 1
		return $this->description;
128
	}
129
130
	/**
131
	 * Get header.
132
	 *
133
	 * @return ResponseHeader
134
	 */
135 1
	public function get_header() {
136 1
		return $this->header;
137
	}
138
139
	/**
140
	 * Get error.
141
	 *
142
	 * @return Error|null
143
	 */
144
	public function get_error() {
145
		return $this->error;
146
	}
147
148
	/**
149
	 * Set error.
150
	 *
151
	 * @param Error|null $error Error.
152
	 * @return void
153
	 */
154
	public function set_error( Error $error = null ) {
155
		$this->error = $error;
156
	}
157
}
158