Passed
Push — develop ( 3af5b3...4680c2 )
by Remco
03:33
created

PaymentMethod   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 90
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_json() 0 3 1
A __construct() 0 2 1
A set_details() 0 2 1
A get_details() 0 2 1
A from_object() 0 20 2
A get_type() 0 2 1
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