Test Failed
Push — develop ( 13fc2a...744f0f )
by Reüel
04:50
created

Integration::__construct()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 67
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8.2685

Importance

Changes 12
Bugs 0 Features 0
Metric Value
cc 8
eloc 36
c 12
b 0
f 0
nc 40
nop 1
dl 0
loc 67
rs 8.0995
ccs 26
cts 31
cp 0.8387
crap 8.2685

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
		 * CLI.
103
		 *
104 1
		 * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-woocommerce.php#L453-L455
105
		 */
106
		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...
107
			$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...
108 1
		}
109 1
	}
110
111 1
	/**
112 1
	 * Register tables.
113 1
	 *
114 1
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L894-L937
115 1
	 */
116
	private function register_tables() {
117 1
		global $wpdb;
118 1
119
		/**
120 1
		 * Tables.
121 1
		 */
122 1
		$wpdb->pronamic_pay_mollie_organizations  = $wpdb->base_prefix . 'pronamic_pay_mollie_organizations';
123 1
		$wpdb->pronamic_pay_mollie_profiles       = $wpdb->base_prefix . 'pronamic_pay_mollie_profiles';
124
		$wpdb->pronamic_pay_mollie_customers      = $wpdb->base_prefix . 'pronamic_pay_mollie_customers';
125
		$wpdb->pronamic_pay_mollie_customer_users = $wpdb->base_prefix . 'pronamic_pay_mollie_customer_users';
126
	}
127
128 1
	/**
129 1
	 * Get settings fields.
130 1
	 *
131 1
	 * @return array<int, array<string, array<int, string>|int|string|true>>
132
	 */
133 1
	public function get_settings_fields() {
134
		$fields = array();
135 1
136
		// API Key.
137
		$fields[] = array(
138 1
			'section'  => 'general',
139
			'filter'   => FILTER_SANITIZE_STRING,
140
			'meta_key' => '_pronamic_gateway_mollie_api_key',
141
			'title'    => _x( 'API Key', 'mollie', 'pronamic_ideal' ),
142
			'type'     => 'text',
143
			'classes'  => array( 'regular-text', 'code' ),
144
			'tooltip'  => __( 'API key as mentioned in the payment provider dashboard', 'pronamic_ideal' ),
145
		);
146
147
		// Due date days.
148
		$fields[] = array(
149
			'section'     => 'advanced',
150
			'filter'      => \FILTER_SANITIZE_NUMBER_INT,
151
			'meta_key'    => '_pronamic_gateway_mollie_due_date_days',
152
			'title'       => _x( 'Due date days', 'mollie', 'pronamic_ideal' ),
153
			'type'        => 'number',
154
			'min'         => 1,
155
			'max'         => 100,
156
			'classes'     => array( 'regular-text' ),
157
			'tooltip'     => __( 'Number of days after which a bank transfer payment expires.', 'pronamic_ideal' ),
158
			'description' => sprintf(
159
				/* translators: 1: <code>1</code>, 2: <code>100</code>, 3: <code>12</code> */
160
				__( 'Minimum %1$s and maximum %2$s days. Default: %3$s days.', 'pronamic_ideal' ),
161
				sprintf( '<code>%s</code>', '1' ),
162
				sprintf( '<code>%s</code>', '100' ),
163
				sprintf( '<code>%s</code>', '12' )
164
			),
165
		);
166
167
		// Webhook.
168
		$fields[] = array(
169
			'section'  => 'feedback',
170
			'title'    => __( 'Webhook URL', 'pronamic_ideal' ),
171
			'type'     => 'text',
172
			'classes'  => array( 'large-text', 'code' ),
173
			'value'    => add_query_arg( 'mollie_webhook', '', home_url( '/' ) ),
174
			'readonly' => true,
175
			'tooltip'  => __( 'The Webhook URL as sent with each transaction to receive automatic payment status updates on.', 'pronamic_ideal' ),
176
		);
177
178
		return $fields;
179
	}
180
181
	/**
182
	 * Save post.
183
	 *
184
	 * @link https://developer.wordpress.org/reference/functions/get_post_meta/
185
	 * @param int $post_id Post ID.
186
	 * @return void
187
	 */
188 1
	public function save_post( $post_id ) {
189 1
		$api_key = get_post_meta( $post_id, '_pronamic_gateway_mollie_api_key', true );
190
191 1
		if ( ! is_string( $api_key ) ) {
192
			return;
193
		}
194
195 1
		$api_key_prefix = substr( $api_key, 0, 4 );
196 1
197
		switch ( $api_key_prefix ) {
198
			case 'live':
199
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_LIVE );
200
201
				return;
202
			case 'test':
203
				update_post_meta( $post_id, '_pronamic_gateway_mode', Gateway::MODE_TEST );
204
205
				return;
206 2
		}
207 2
	}
208
209 2
	/**
210 2
	 * User profile.
211 2
	 *
212 2
	 * @since 1.1.6
213
	 * @link https://github.com/WordPress/WordPress/blob/4.5.2/wp-admin/user-edit.php#L578-L600
214 2
	 * @param WP_User $user WordPress user.
215
	 * @return void
216
	 */
217
	public static function user_profile( $user ) {
218
		include __DIR__ . '/../views/html-admin-user-profile.php';
219
	}
220
221
	/**
222
	 * Payment provider URL.
223 1
	 *
224 1
	 * @param string|null $url     Payment provider URL.
225
	 * @param Payment     $payment Payment.
226
	 * @return string|null
227
	 */
228
	public function payment_provider_url( $url, Payment $payment ) {
229
		$transaction_id = $payment->get_transaction_id();
230
231
		if ( null === $transaction_id ) {
232
			return $url;
233
		}
234
235
		return sprintf(
236
			'https://www.mollie.com/dashboard/payments/%s',
237
			$transaction_id
238
		);
239
	}
240
	/**
241
	 * Get configuration by post ID.
242
	 *
243
	 * @param int $post_id Post ID.
244
	 * @return Config
245
	 */
246
	public function get_config( $post_id ) {
247
		$config = new Config();
248
249
		$config->id            = intval( $post_id );
250
		$config->api_key       = $this->get_meta( $post_id, 'mollie_api_key' );
251
		$config->mode          = $this->get_meta( $post_id, 'mode' );
252
		$config->due_date_days = $this->get_meta( $post_id, 'mollie_due_date_days' );
253
254
		return $config;
255
	}
256
257
	/**
258
	 * Get gateway.
259
	 *
260
	 * @param int $post_id Post ID.
261
	 * @return Gateway
262
	 */
263
	public function get_gateway( $post_id ) {
264
		return new Gateway( $this->get_config( $post_id ) );
265
	}
266
267
	/**
268
	 * Next payment delivery date.
269
	 *
270
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
271
	 * @param Payment   $payment                    Payment.
272
	 * @return \DateTime
273
	 */
274
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
275
		$config_id = $payment->get_config_id();
276
277
		if ( null === $config_id ) {
278
			return $next_payment_delivery_date;
279
		}
280
281
		// Check gateway.
282
		$gateway_id = \get_post_meta( $config_id, '_pronamic_gateway_id', true );
283
284
		if ( 'mollie' !== $gateway_id ) {
285
			return $next_payment_delivery_date;
286
		}
287
288
		// Check direct debit payment method.
289
		$method = $payment->get_method();
290
291
		if ( null === $method ) {
292
			return $next_payment_delivery_date;
293
		}
294
295
		if ( ! PaymentMethods::is_direct_debit_method( $method ) ) {
296
			return $next_payment_delivery_date;
297
		}
298
299
		// Check subscription.
300
		$subscription = $payment->get_subscription();
301
302
		if ( null === $subscription ) {
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