Failed Conditions
Push — master ( 84f509...0a235e )
by Remco
14:29 queued 05:04
created

PaymentMethodsResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 72.41%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 93
ccs 21
cts 29
cp 0.7241
rs 10
c 3
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_payment_methods() 0 2 1
A __construct() 0 3 1
A get_payment_method_types() 0 15 3
A from_object() 0 29 3
1
<?php
2
/**
3
 * Payment methods response
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 methods response
19
 *
20
 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession
21
 *
22
 * @author  Remco Tolsma
23
 * @version 1.0.5
24
 * @since   1.0.0
25
 */
26
class PaymentMethodsResponse extends ResponseObject {
27
	/**
28
	 * Groups of payment methods.
29
	 *
30
	 * @var array<string, string|array<int, string>>
31
	 */
32
	private $groups;
33
34
	/**
35
	 * Detailed list of payment methods required to generate payment forms.
36
	 *
37
	 * @var PaymentMethod[]
38
	 */
39
	private $payment_methods;
40
41
	/**
42
	 * Construct payment session response object.
43
	 *
44
	 * @param array<string, string|array<int, string>> $groups          Groups.
45
	 * @param PaymentMethod[]                          $payment_methods Payment methods.
46
	 */
47 2
	public function __construct( $groups, $payment_methods ) {
48 2
		$this->groups          = $groups;
49 2
		$this->payment_methods = $payment_methods;
50 2
	}
51
52
	/**
53
	 * Get payment methods.
54
	 *
55
	 * @return PaymentMethod[]
56
	 */
57 2
	public function get_payment_methods() {
58 2
		return $this->payment_methods;
59
	}
60
61
	/**
62
	 * Get payment method types.
63
	 *
64
	 * @return array<int, string>
65
	 */
66
	public function get_payment_method_types() {
67
		$types = array();
68
69
		// Loop payment methods.
70
		$payment_methods = $this->payment_methods;
71
72
		foreach ( $payment_methods as $payment_method ) {
73
			$type = $payment_method->get_type();
74
75
			if ( null !== $type ) {
76
				$types[] = $type;
77
			}
78
		}
79
80
		return $types;
81
	}
82
83
	/**
84
	 * Create payment methods repsonse from object.
85
	 *
86
	 * @param object $object Object.
87
	 * @return PaymentMethodsResponse
88
	 * @throws ValidationException Throws validation exception when object does not contains the required properties.
89
	 */
90 2
	public static function from_object( $object ) {
91 2
		$validator = new Validator();
92
93 2
		$validator->validate(
94 2
			$object,
95
			(object) array(
96 2
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-methods-response.json' ),
97
			),
98 2
			Constraint::CHECK_MODE_EXCEPTIONS
99
		);
100
101 2
		$payment_methods = array();
102
103
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
104 2
		foreach ( $object->paymentMethods as $payment_method_object ) {
105 2
			$payment_methods[] = PaymentMethod::from_object( $payment_method_object );
106
		}
107
108 2
		$groups = array();
109
110 2
		if ( isset( $object->groups ) ) {
111 2
			$groups = $object->groups;
112
		}
113
114 2
		$response = new self( $groups, $payment_methods );
115
116 2
		$response->set_original_object( $object );
117
118 2
		return $response;
119
	}
120
}
121