Test Failed
Push — develop ( 5d1492...ddf0fb )
by Remco
03:56
created

Integration::__construct()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 61
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7.392

Importance

Changes 12
Bugs 0 Features 0
Metric Value
cc 7
eloc 33
c 12
b 0
f 0
nc 20
nop 1
dl 0
loc 61
ccs 20
cts 25
cp 0.8
crap 7.392
rs 8.4586

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