Test Failed
Push — develop ( 61c5d3...602393 )
by Reüel
06:05
created

PaymentMethod::get_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment method
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 JsonSchema\Constraints\Constraint;
14
use JsonSchema\Exception\ValidationException;
15
use JsonSchema\Validator;
16
17
/**
18
 * Payment method
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.5
22
 * @since   1.0.0
23
 */
24
class PaymentMethod extends ResponseObject {
25
	/**
26
	 * Type.
27
	 *
28
	 * @var string
29
	 */
30
	private $type;
31
32
	/**
33
	 * Details.
34
	 *
35
	 * @var array<int, object>|null
36
	 */
37
	private $details;
38
39
	/**
40
	 * Construct a payment method.
41
	 *
42
	 * @param object $payment_method_object Original object.
43
	 */
44 7
	public function __construct( $payment_method_object ) {
45 7
		// Set type.
46 7
		if ( isset( $payment_method_object->type ) ) {
47
			$this->type = $payment_method_object->type;
48
		}
49
50
		$this->set_original_object( $payment_method_object );
51
	}
52
53 2
	/**
54 2
	 * Get type.
55
	 *
56
	 * @return string
57
	 */
58
	public function get_type() {
59
		return $this->type;
60
	}
61
62 1
	/**
63 1
	 * Get details.
64
	 *
65
	 * @return array<int, object>|null
66
	 */
67
	public function get_details() {
68
		return $this->details;
69
	}
70
71
	/**
72 3
	 * Set details.
73 3
	 *
74 3
	 * @param array<int, object> $details Details.
75
	 * @return void
76
	 */
77
	public function set_details( $details ) {
78
		$this->details = $details;
79
	}
80
81 3
	/**
82
	 * Create payment method from object.
83 3
	 *
84
	 * @param object $object Object.
85
	 * @return PaymentMethod
86
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
87
	 */
88
	public static function from_object( $object ) {
89
		$validator = new Validator();
90
91
		$validator->validate(
92
			$object,
93
			(object) array(
94 2
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-method.json' ),
95 2
			),
96
			Constraint::CHECK_MODE_EXCEPTIONS
97 2
		);
98 2
99
		$payment_method = new self( $object );
100 2
101
		if ( isset( $object->details ) ) {
102 2
			$payment_method->set_details( $object->details );
103
		}
104
105 2
		$payment_method->set_original_object( $object );
106
107 2
		return $payment_method;
108 2
	}
109
}
110