Failed Conditions
Push — master ( faa7df...d58e58 )
by Reüel
04:49 queued 11s
created

Integration::next_payment_delivery_date()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 65
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 51.4911

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
nc 9
nop 2
dl 0
loc 65
ccs 6
cts 31
cp 0.1935
crap 51.4911
rs 8.0555
c 3
b 0
f 0

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-2021 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\DateTime\DateTime;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
16
use Pronamic\WordPress\Pay\Payments\Payment;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\Gateways\Mollie\Payment. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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