Passed
Push — develop ( 3af5b3...4680c2 )
by Remco
03:33
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-2019 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.0
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|null
36
	 */
37
	private $details;
38
39
	/**
40
	 * Construct a payment method.
41
	 *
42
	 * @param string $type Adyen payment method type.
43
	 */
44 5
	public function __construct( $type ) {
45 5
		$this->type = $type;
46 5
	}
47
48
	/**
49
	 * Get type.
50
	 *
51
	 * @return string
52
	 */
53 1
	public function get_type() {
54 1
		return $this->type;
55
	}
56
57
	/**
58
	 * Get details.
59
	 *
60
	 * @return array|null
61
	 */
62
	public function get_details() {
63
		return $this->details;
64
	}
65
66
	/**
67
	 * Set details.
68
	 *
69
	 * @param array|null $details Details.
70
	 * @return void
71
	 */
72 2
	public function set_details( $details ) {
73 2
		$this->details = $details;
74 2
	}
75
76
	/**
77
	 * Get JSON.
78
	 *
79
	 * @return object
80
	 */
81 3
	public function get_json() {
82
		return (object) array(
83 3
			'type' => $this->type,
84
		);
85
	}
86
87
	/**
88
	 * Create payment method from object.
89
	 *
90
	 * @param object $object Object.
91
	 * @return PaymentMethod
92
	 * @throws ValidationException Throws JSON schema validation exception when JSON is invalid.
93
	 */
94 2
	public static function from_object( $object ) {
95 2
		$validator = new Validator();
96
97 2
		$validator->validate(
98 2
			$object,
99
			(object) array(
100 2
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-method.json' ),
101
			),
102 2
			Constraint::CHECK_MODE_EXCEPTIONS
103
		);
104
105 2
		$payment_method = new self( $object->type );
106
107 2
		if ( isset( $object->details ) ) {
108 2
			$payment_method->set_details( $object->details );
109
		}
110
111 2
		$payment_method->set_original_object( $object );
112
113 2
		return $payment_method;
114
	}
115
}
116