Test Failed
Push — develop ( 4d80cd...5b7daa )
by Remco
04:34
created

Integration::__construct()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 59
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.2286

Importance

Changes 12
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 12
b 0
f 0
nc 20
nop 1
dl 0
loc 59
rs 8.7537
ccs 22
cts 27
cp 0.8148
crap 6.2286

How to fix   Long Method   

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
 * Mollie integration.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Core\PaymentMethods;
14
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\AbstractGatewayIntegration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
use WP_User;
17
18
/**
19
 * Title: Mollie integration
20
 * Description:
21
 * Copyright: 2005-2020 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.9
26
 * @since   1.0.0
27
 */
28
class Integration extends AbstractGatewayIntegration {
29
	/**
30
	 * Register URL.
31
	 *
32
	 * @var string
33
	 */
34
	public $register_url;
35
36
	/**
37
	 * Construct and intialize Mollie integration.
38
	 *
39 7
	 * @param array $args Arguments.
40 7
	 */
41 7
	public function __construct( $args = array() ) {
42 7
		$args = wp_parse_args(
43 7
			$args,
44 7
			array(
45 7
				'id'            =>  'mollie',
46 7
				'name'          =>  'Mollie',
47 7
				'url'           =>  'http://www.mollie.com/en/',
48
				'product_url'   =>  \__( 'https://www.mollie.com/en/pricing', 'pronamic_ideal' ),
49
				'dashboard_url' =>  'https://www.mollie.com/dashboard/',
50
				'provider'      =>  'mollie',
51
				'supports'      =>  array(
52
					'payment_status_request',
53
					'recurring_direct_debit',
54
					'recurring_credit_card',
55
					'recurring',
56
					'webhook',
57 7
					'webhook_log',
58
					'webhook_no_config',
59
				),
60 7
			)
61
		);
62 7
63 7
	    parent::__construct( $args );
64
65
		// Actions.
66 7
		$function = array( __NAMESPACE__ . '\Listener', 'listen' );
67
68
		if ( ! has_action( 'wp_loaded', $function ) ) {
69
			add_action( 'wp_loaded', $function );
70
		}
71
72
		if ( is_admin() ) {
73
			$function = array( __CLASS__, 'user_profile' );
74
75
			if ( ! has_action( 'show_user_profile', $function ) ) {
76
				add_action( 'show_user_profile', $function );
77
			}
78
79 7
			if ( ! has_action( 'edit_user_profile', $function ) ) {
80
				add_action( 'edit_user_profile', $function );
81 7
			}
82 7
		}
83
84
		// Filters.
85 7
		$function = array( $this, 'next_payment_delivery_date' );
86 7
87
		if ( ! \has_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function ) ) {
88
			\add_filter( 'pronamic_pay_subscription_next_payment_delivery_date', $function, 10, 2 );
89
		}
90
91
		add_filter( 'pronamic_payment_provider_url_mollie', array( $this, 'payment_provider_url' ), 10, 2 );
92
93 1
		// Tables.
94 1
		$this->register_tables();
95
96
		// Upgrades.
97 1
		$upgrades = $this->get_upgrades();
98 1
99 1
		$upgrades->add( new Upgrade300() );
100 1
	}
101 1
102 1
	/**
103
	 * Register tables.
104 1
	 *
105
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937
106
	 */
107
	private function register_tables() {
108 1
		global $wpdb;
109 1
110
		/**
111 1
		 * Tables.
112 1
		 */
113 1
		$wpdb->pronamic_pay_mollie_organisations  = $wpdb->base_prefix . 'pronamic_pay_mollie_organisations';
114 1
		$wpdb->pronamic_pay_mollie_customers      = $wpdb->base_prefix . 'pronamic_pay_mollie_customers';
115 1
		$wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users';
116
	}
117 1
118 1
	/**
119
	 * Get settings fields.
120 1
	 *
121 1
	 * @return array<int, array<string, array<int, string>|int|string|true>>
122 1
	 */
123 1
	public function get_settings_fields() {
124
		$fields = array();
125
126
		// API Key.
127
		$fields[] = array(
128 1
			'section'  => 'general',
129 1
			'filter'   => FILTER_SANITIZE_STRING,
130 1
			'meta_key' => '_pronamic_gateway_mollie_api_key',
131 1
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
132
			'type'     => 'text',
133 1
			'classes'  => array( 'regular-text', 'code' ),
134
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
135 1
		);
136
137
		// Due date days.
138 1
		$fields[] = array(
139
			'section'     => 'advanced',
140
			'filter'      => \FILTER_SANITIZE_NUMBER_INT,
141
			'meta_key'    => '_pronamic_gateway_mollie_due_date_days',
142
			'title'       => _x( 'Due date days', 'mollie', 'pronamic_ideal' ),
143
			'type'        => 'number',
144
			'min'         => 1,
145
			'max'         => 100,
146
			'classes'     => array( 'regular-text' ),
147
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
148
			'description' => sprintf(
149
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
150
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
151
				sprintf( '<code>%s</code>', '1' ),
152
				sprintf( '<code>%s</code>', '100' ),
153
				sprintf( '<code>%s</code>', '12' )
154
			),
155
		);
156
157
		// Webhook.
158
		$fields[] = array(
159
			'section'  => 'feedback',
160
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
161
			'type'     => 'text',
162
			'classes'  => array( 'large-text', 'code' ),
163
			'value'    => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ),
164
			'readonly' => true,
165
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
166
		);
167
168
		return $fields;
169
	}
170
171
	/**
172
	 * Save post.
173
	 *
174
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
175
	 * @param int $post_id Post ID.
176
	 * @return void
177
	 */
178
	public function save_post( $post_id ) {
179
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
180
181
		if ( ! is_string( $api_key ) ) {
182
			return;
183
		}
184
185
		$api_key_prefix = substr( $api_key, 0, 4 );
186
187
		switch ( $api_key_prefix ) {
188 1
			case 'live':
189 1
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
190
191 1
				return;
192
			case 'test':
193
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
194
195 1
				return;
196 1
		}
197
	}
198
199
	/**
200
	 * User profile.
201
	 *
202
	 * @since 1.1.6
203
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
204
	 * @param WP_User $user WordPress user.
205
	 * @return void
206 2
	 */
207 2
	public static function user_profile( $user ) {
208
		include __DIR__ . '/../views/html-admin-user-profile.php';
209 2
	}
210 2
211 2
	/**
212 2
	 * Payment provider URL.
213
	 *
214 2
	 * @param string|null $url     Payment provider URL.
215
	 * @param Payment     $payment Payment.
216
	 * @return string|null
217
	 */
218
	public function payment_provider_url( $url, Payment $payment ) {
219
		$transaction_id = $payment->get_transaction_id();
220
221
		if ( null === $transaction_id ) {
222
			return $url;
223 1
		}
224 1
225
		return sprintf(
226
			'https://www.mollie.com/dashboard/payments/%s',
227
			$transaction_id
228
		);
229
	}
230
	/**
231
	 * Get configuration by post ID.
232
	 *
233
	 * @param int $post_id Post ID.
234
	 * @return Config
235
	 */
236
	public function get_config( $post_id ) {
237
		$config = new Config();
238
239
		$config->id            = intval( $post_id );
240
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
241
		$config->mode          = $this->get_meta( $post_id, 'mode' );
242
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
243
244
		return $config;
245
	}
246
247
	/**
248
	 * Get gateway.
249
	 *
250
	 * @param int $post_id Post ID.
251
	 * @return Gateway
252
	 */
253
	public function get_gateway( $post_id ) {
254
		return new Gateway( $this->get_config( $post_id ) );
255
	}
256
257
	/**
258
	 * Next payment delivery date.
259
	 *
260
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
261
	 * @param Payment   $payment                    Payment.
262
	 * @return \DateTime
263
	 */
264
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
265
		$config_id = $payment->get_config_id();
266
267
		if ( null === $config_id ) {
268
			return $next_payment_delivery_date;
269
		}
270
271
		// Check gateway.
272
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
273
274
		if ( 'mollie' !== $gateway_id ) {
275
			return $next_payment_delivery_date;
276
		}
277
278
		// Check direct debit payment method.
279
		$method = $payment->get_method();
280
281
		if ( null === $method ) {
282
			return $next_payment_delivery_date;
283
		}
284
285
		if ( ! PaymentMethods::is_direct_debit_method( $method ) ) {
286
			return $next_payment_delivery_date;
287
		}
288
289
		// Check subscription.
290
		$subscription = $payment->get_subscription();
291
292
		if ( null === $subscription ) {
293
			return $next_payment_delivery_date;
294
		}
295
296
		// Base delivery date on next payment date.
297
		$next_payment_date = $subscription->get_next_payment_date();
298
299
		if ( null === $next_payment_date ) {
300
			return $next_payment_delivery_date;
301
		}
302
303
		$next_payment_delivery_date = clone $next_payment_date;
304
305
		// Textual representation of the day of the week, Sunday through Saturday.
306
		$day_of_week = $next_payment_delivery_date->format( 'l' );
307
308
		/*
309
		 * Subtract days from next payment date for earlier delivery.
310
		 *
311
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
312
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
313
		 */
314
		switch ( $day_of_week ) {
315
			case 'Monday':
316
				$next_payment_delivery_date->modify( '-3 days' );
317
318
				break;
319
			case 'Saturday':
320
				$next_payment_delivery_date->modify( '-2 days' );
321
322
				break;
323
			case 'Sunday':
324
				$next_payment_delivery_date->modify( '-3 days' );
325
326
				break;
327
			default:
328
				$next_payment_delivery_date->modify( '-1 day' );
329
330
				break;
331
		}
332
333
		$next_payment_delivery_date->setTime( 0, 0, 0 );
334
335
		return $next_payment_delivery_date;
336
	}
337
}
338