AbstractGateway::get_issuers()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 25
nc 15
nop 0
dl 0
loc 53
ccs 0
cts 12
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Gateway
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 Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
15
/**
16
 * Gateway
17
 *
18
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.5
22
 * @since   1.0.0
23
 */
24
abstract class AbstractGateway extends Core_Gateway {
25
	/**
26
	 * Client.
27
	 *
28
	 * @var Client
29
	 */
30
	public $client;
31
32
	/**
33
	 * Constructs and initializes an Adyen gateway.
34
	 *
35
	 * @param Config $config Config.
36
	 */
37 3
	public function __construct( Config $config ) {
38 3
		parent::__construct( $config );
39
40 3
		$this->set_method( self::METHOD_HTTP_REDIRECT );
41
42
		// Supported features.
43 3
		$this->supports = array();
44
45
		// Client.
46 3
		$this->client = new Client( $config );
47 3
	}
48
49
	/**
50
	 * Get available payment methods.
51
	 *
52
	 * @return array<int, string>
53
	 * @see Core_Gateway::get_available_payment_methods()
54
	 */
55
	public function get_available_payment_methods() {
56
		$core_payment_methods = array();
57
58
		try {
59
			$payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) );
60
		} catch ( \Exception $e ) {
61
			$this->error = new \WP_Error( 'adyen_error', $e->getMessage() );
62
63
			return $core_payment_methods;
64
		}
65
66
		foreach ( $payment_methods_response->get_payment_methods() as $payment_method ) {
67
			$type = $payment_method->get_type();
68
69
			if ( null === $type ) {
70
				continue;
71
			}
72
73
			$core_payment_method = PaymentMethodType::to_wp( $type );
74
75
			$core_payment_methods[] = $core_payment_method;
76
		}
77
78
		$core_payment_methods = array_filter( $core_payment_methods );
79
		$core_payment_methods = array_unique( $core_payment_methods );
80
81
		return $core_payment_methods;
82
	}
83
84
	/**
85
	 * Get issuers.
86
	 *
87
	 * @return array<int, array<string, array<string, string>>>
88
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
89
	 */
90
	public function get_issuers() {
91
		$issuers = array();
92
93
		try {
94
			$payment_methods_response = $this->client->get_payment_methods( new PaymentMethodsRequest( $this->config->get_merchant_account() ) );
95
		} catch ( \Exception $e ) {
96
			$this->error = new \WP_Error( 'adyen_error', $e->getMessage() );
97
98
			return $issuers;
99
		}
100
101
		$payment_methods = $payment_methods_response->get_payment_methods();
102
103
		// Limit to iDEAL payment methods.
104
		$payment_methods = array_filter(
105
			$payment_methods,
106
			/**
107
			 * Check if payment method is iDEAL.
108
			 *
109
			 * @param PaymentMethod $payment_method Payment method.
110
			 *
111
			 * @return boolean True if payment method is iDEAL, false otherwise.
112
			 */
113
			function( $payment_method ) {
114
				return ( PaymentMethodType::IDEAL === $payment_method->get_type() );
115
			}
116
		);
117
118
		foreach ( $payment_methods as $payment_method ) {
119
			$details = $payment_method->get_details();
120
121
			if ( is_array( $details ) ) {
122
				foreach ( $details as $detail ) {
123
					if ( ! isset( $detail->key, $detail->type, $detail->items ) ) {
124
						continue;
125
					}
126
127
					if ( 'issuer' === $detail->key && 'select' === $detail->type ) {
128
						foreach ( $detail->items as $item ) {
129
							$issuers[ \strval( $item->id ) ] = \strval( $item->name );
130
						}
131
					}
132
				}
133
			}
134
		}
135
136
		if ( empty( $issuers ) ) {
137
			return $issuers;
138
		}
139
140
		return array(
141
			array(
142
				'options' => $issuers,
143
			),
144
		);
145
	}
146
}
147