Failed Conditions
Push — develop ( e03969...b62619 )
by Remco
04:53
created

Integration::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 59
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0164

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 5
eloc 32
c 13
b 0
f 0
nc 8
nop 1
dl 0
loc 59
ccs 21
cts 23
cp 0.913
crap 5.0164
rs 9.0968

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
	/**
248
	 * Get configuration by post ID.
249
	 *
250
	 * @param int $post_id Post ID.
251
	 * @return Config
252
	 */
253 2
	public function get_config( $post_id ) {
254 2
		$config = new Config();
255
256 2
		$config->id            = intval( $post_id );
257 2
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
258 2
		$config->mode          = $this->get_meta( $post_id, 'mode' );
259 2
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
260
261 2
		return $config;
262
	}
263
264
	/**
265
	 * Get gateway.
266
	 *
267
	 * @param int $post_id Post ID.
268
	 * @return Gateway
269
	 */
270 1
	public function get_gateway( $post_id ) {
271 1
		return new Gateway( $this->get_config( $post_id ) );
272
	}
273
274
	/**
275
	 * Next payment delivery date.
276
	 *
277
	 * @param DateTime         $next_payment_delivery_date Next payment delivery date.
278
	 * @param CoreSubscription $subscription               Subscription.
279
	 * @return DateTime
280
	 */
281 10
	public function next_payment_delivery_date( DateTime $next_payment_delivery_date, CoreSubscription $subscription ) {
282 10
		$config_id = $subscription->get_config_id();
283
284 10
		if ( null === $config_id ) {
285
			return $next_payment_delivery_date;
286
		}
287
288
		// Check gateway.
289 10
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
290
291 10
		if ( 'mollie' !== $gateway_id ) {
292 10
			return $next_payment_delivery_date;
293
		}
294
295
		// Check direct debit payment method.
296
		$payment_method = $subscription->get_payment_method();
0 ignored issues
show
Bug introduced by
The method get_payment_method() does not exist on Pronamic\WordPress\Pay\Subscriptions\Subscription. Did you maybe mean get_payments()? ( Ignorable by Annotation )

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

296
		/** @scrutinizer ignore-call */ 
297
  $payment_method = $subscription->get_payment_method();

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...
297
298
		if ( null === $payment_method ) {
299
			return $next_payment_delivery_date;
300
		}
301
302
		if ( ! PaymentMethods::is_direct_debit_method( $payment_method ) ) {
303
			return $next_payment_delivery_date;
304
		}
305
306
		// Base delivery date on next payment date.
307
		$next_payment_date = $subscription->get_next_payment_date();
308
309
		if ( null === $next_payment_date ) {
310
			return $next_payment_delivery_date;
311
		}
312
313
		$next_payment_delivery_date = clone $next_payment_date;
314
315
		// Textual representation of the day of the week, Sunday through Saturday.
316
		$day_of_week = $next_payment_delivery_date->format( 'l' );
317
318
		/*
319
		 * Subtract days from next payment date for earlier delivery.
320
		 *
321
		 * @link https://help.mollie.com/hc/en-us/articles/115000785649-When-are-direct-debit-payments-processed-and-paid-out-
322
		 * @link https://help.mollie.com/hc/en-us/articles/115002540294-What-are-the-payment-methods-processing-times-
323
		 */
324
		switch ( $day_of_week ) {
325
			case 'Monday':
326
				$next_payment_delivery_date->modify( '-3 days' );
327
328
				break;
329
			case 'Saturday':
330
				$next_payment_delivery_date->modify( '-2 days' );
331
332
				break;
333
			case 'Sunday':
334
				$next_payment_delivery_date->modify( '-3 days' );
335
336
				break;
337
			default:
338
				$next_payment_delivery_date->modify( '-1 day' );
339
340
				break;
341
		}
342
343
		$next_payment_delivery_date->setTime( 0, 0, 0 );
344
345
		return $next_payment_delivery_date;
346
	}
347
}
348