Passed
Push — develop ( 44f8ef...36711b )
by Reüel
04:20
created

GatewaySettings::get_sections()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateway Settings
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Admin
9
 */
10
11
namespace Pronamic\WordPress\Pay\Admin;
12
13
use Pronamic\WordPress\Pay\Core\ConfigProvider;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\Plugin;
16
17
/**
18
 * WordPress admin gateway settings
19
 *
20
 * @author Remco Tolsma
21
 * @version 3.8.0
22
 * @since 3.8.0
23
 */
24
class GatewaySettings {
25
	/**
26
	 * Settings.
27
	 *
28
	 * @var array
29
	 */
30
	private $settings;
31
32
	/**
33
	 * Constructs and initializes an admin gateway settings object.
34
	 */
35
	public function __construct() {
36
		$this->settings = array();
37
38
		// Classes.
39
		$classes = apply_filters( 'pronamic_pay_gateway_settings', array() );
40
41
		foreach ( $classes as $class ) {
42
			$this->settings[] = new $class();
43
		}
44
	}
45
46
	/**
47
	 * Get sections.
48
	 *
49
	 * @return array
50
	 */
51
	public function get_sections() {
52
		// Default sections.
53
		$sections = array(
54
			'general' => array(),
55
		);
56
57
		$sections = apply_filters( 'pronamic_pay_gateway_sections', $sections );
58
59
		// Payment methods section.
60
		$config_id = get_the_ID();
61
62
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
It seems like $config_id can also be of type false; however, parameter $config_id of Pronamic\WordPress\Pay\Plugin::get_gateway() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
		$gateway = Plugin::get_gateway( /** @scrutinizer ignore-type */ $config_id );
Loading history...
63
64
		if ( $gateway ) {
65
			$gateway_id = get_post_meta( $config_id, '_pronamic_gateway_id', true );
0 ignored issues
show
Bug introduced by
It seems like $config_id can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
			$gateway_id = get_post_meta( /** @scrutinizer ignore-type */ $config_id, '_pronamic_gateway_id', true );
Loading history...
66
67
			// Payment methods.
68
			$sections['payment_methods'] = array(
69
				'title'       => __( 'Payment Methods', 'pronamic_ideal' ),
70
				'methods'     => array(
71
					$gateway_id,
72
					str_replace( '-', '_', $gateway_id ),
73
				),
74
				'description' => __( 'Overview of supported payment methods. A green checkmark icon indicates that the payment method seems to be activated for your account.', 'pronamic_ideal' ),
75
			);
76
		}
77
78
		return $sections;
79
	}
80
81
	/**
82
	 * Get fields.
83
	 *
84
	 * @return array
85
	 */
86
	public function get_fields() {
87
		// Default fields.
88
		$fields = array(
89
			array(
90
				'filter'   => FILTER_SANITIZE_STRING,
91
				'section'  => 'general',
92
				'meta_key' => '_pronamic_gateway_mode',
93
				'name'     => 'mode',
94
				'id'       => 'pronamic_ideal_mode',
95
				'title'    => __( 'Mode', 'pronamic_ideal' ),
96
				'type'     => 'select',
97
				'options'  => array(
98
					array(
99
						'options' => array(
100
							'test' => __( 'Test', 'pronamic_ideal' ),
101
							'live' => __( 'Live', 'pronamic_ideal' ),
102
						),
103
					),
104
				),
105
			),
106
		);
107
108
		// Payment methods.
109
		$fields[] = array(
110
			'section'  => 'payment_methods',
111
			'type'     => 'html',
112
			'callback' => array( $this, 'gateway_payment_methods' ),
113
		);
114
115
		return apply_filters( 'pronamic_pay_gateway_fields', $fields );
116
	}
117
118
119
	/**
120
	 * Available payment methods overview.
121
	 */
122
	public function gateway_payment_methods() {
123
		$gateway = Plugin::get_gateway( get_the_ID() );
0 ignored issues
show
Bug introduced by
It seems like get_the_ID() can also be of type false; however, parameter $config_id of Pronamic\WordPress\Pay\Plugin::get_gateway() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
		$gateway = Plugin::get_gateway( /** @scrutinizer ignore-type */ get_the_ID() );
Loading history...
124
125
		$supported = $gateway->get_supported_payment_methods();
0 ignored issues
show
Bug introduced by
The method get_supported_payment_methods() does not exist on Pronamic\WordPress\Pay\Core\Gateway. It seems like you code against a sub-type of said class. However, the method does not exist in Pronamic\WordPress\Pay\G...nico\DirectLink\Gateway. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
		/** @scrutinizer ignore-call */ 
126
  $supported = $gateway->get_supported_payment_methods();
Loading history...
126
		$available = $gateway->get_transient_available_payment_methods();
0 ignored issues
show
Bug introduced by
The method get_transient_available_payment_methods() does not exist on Pronamic\WordPress\Pay\Core\Gateway. Did you maybe mean get_transient_payment_methods()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
		/** @scrutinizer ignore-call */ 
127
  $available = $gateway->get_transient_available_payment_methods();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
		$payment_methods = array();
129
		$sort            = array();
130
131
		foreach ( $supported as $payment_method ) {
132
			$name = PaymentMethods::get_name( $payment_method );
133
134
			$payment_methods[ $payment_method ] = array(
135
				'id'        => $payment_method,
136
				'name'      => $name,
137
				'available' => in_array( $payment_method, $available, true ),
138
			);
139
140
			$sort[ $payment_method ] = $name;
141
		}
142
143
		$sort_flags = SORT_STRING;
144
145
		if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
146
			// SORT_FLAG_CASE is available since PHP 5.4.
147
			$sort_flags = SORT_STRING | SORT_FLAG_CASE;
148
		}
149
150
		array_multisort( $sort, $sort_flags, $payment_methods );
151
152
		require Plugin::$dirname . '/admin/meta-box-gateway-config-payment-methods.php';
153
	}
154
}
155