Failed Conditions
Push — develop ( 308757...5d1bfa )
by Remco
04:32
created

Integration::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 58
Code Lines 31

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 31
c 13
b 0
f 0
nc 8
nop 1
dl 0
loc 58
ccs 21
cts 23
cp 0.913
crap 5.0164
rs 9.1128

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