Failed Conditions
Push — develop ( dcdad5...3d8e5d )
by Remco
06:27
created

Pronamic::get_subscription()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 68
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 12
Bugs 0 Features 0
Metric Value
cc 6
eloc 36
c 12
b 0
f 0
nc 6
nop 1
dl 0
loc 68
ccs 0
cts 44
cp 0
crap 42
rs 8.7217

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
 * Pronamic
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress;
12
13
use MeprTransaction;
14
use Pronamic\WordPress\Money\Money;
15
use Pronamic\WordPress\Money\TaxedMoney;
16
use Pronamic\WordPress\Pay\AddressHelper;
17
use Pronamic\WordPress\Pay\Customer;
18
use Pronamic\WordPress\Pay\ContactName;
19
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
20
use Pronamic\WordPress\Pay\Payments\Payment;
21
use Pronamic\WordPress\Pay\Payments\PaymentLines;
22
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
23
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionInterval;
24
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase;
25
26
/**
27
 * Pronamic
28
 *
29
 * @author  Remco Tolsma
30
 * @version 3.0.1
31
 * @since   2.0.5
32
 */
33
class Pronamic {
34
	/**
35
	 * Get Pronamic payment from MemberPress transaction.
36
	 *
37
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
38
	 * @return Payment
39
	 */
40
	public static function get_payment( MeprTransaction $memberpress_transaction ) {
41
		$payment = new Payment();
42
43
		// MemberPress.
44
		$memberpress_user         = $memberpress_transaction->user();
45
		$memberpress_product      = $memberpress_transaction->product();
46
		$memberpress_subscription = $memberpress_transaction->subscription();
47
48
		// Title.
49
		$title = sprintf(
50
			/* translators: %s: payment data title */
51
			__( 'Payment for %s', 'pronamic_ideal' ),
52
			sprintf(
53
				/* translators: %s: order id */
54
				__( 'MemberPress transaction %s', 'pronamic_ideal' ),
55
				$memberpress_transaction->id
56
			)
57
		);
58
59
		$payment->order_id    = $memberpress_transaction->id;
60
		$payment->title       = $title;
61
		$payment->description = $memberpress_product->post_title;
62
		$payment->user_id     = $memberpress_user->ID;
63
		$payment->source      = 'memberpress_transaction';
64
		$payment->source_id   = $memberpress_transaction->id;
65
		$payment->issuer      = null;
66
67
		// Contact.
68
		$contact_name = new ContactName();
69
		$contact_name->set_first_name( $memberpress_user->first_name );
70
		$contact_name->set_last_name( $memberpress_user->last_name );
71
72
		$customer = new Customer();
73
		$customer->set_name( $contact_name );
74
		$customer->set_email( $memberpress_user->user_email );
75
		$customer->set_user_id( $memberpress_user->ID );
76
77
		$payment->set_customer( $customer );
78
79
		/**
80
		 * Address.
81
		 *
82
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprUser.php#L1191-L1216
83
		 */
84
		$address = AddressHelper::from_array(
85
			array(
86
				'line_1'       => $memberpress_user->address( 'one', false ),
87
				'line_2'       => $memberpress_user->address( 'two', false ),
88
				'postal_code'  => $memberpress_user->address( 'zip', false ),
89
				'city'         => $memberpress_user->address( 'city', false ),
90
				'region'       => $memberpress_user->address( 'state', false ),
91
				'country_code' => $memberpress_user->address( 'country', false ),
92
			)
93
		);
94
95
		if ( null !== $address ) {
96
			$address->set_name( $contact_name );
97
		}
98
99
		$payment->set_billing_address( $address );
100
		$payment->set_shipping_address( $address );
101
102
		/**
103
		 * Total.
104
		 */
105
		$payment->set_total_amount(
106
			new TaxedMoney(
107
				$memberpress_transaction->total,
108
				MemberPress::get_currency(),
109
				$memberpress_transaction->tax_amount,
110
				$memberpress_transaction->tax_rate
111
			)
112
		);
113
114
		/**
115
		 * Vat number.
116
		 *
117
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
118
		 * @todo
119
		 */
120
121
		/**
122
		 * Subscription.
123
		 */
124
		$payment->subscription = self::get_subscription( $memberpress_transaction );
125
126
		if ( $payment->subscription ) {
127
			/**
128
			 * MemberPress transaction subscription ID.
129
			 * 
130
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L27
131
			 */
132
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
133
134
			$payment->add_period( $payment->subscription->new_period() );
0 ignored issues
show
Bug introduced by
It seems like $payment->subscription->new_period() can also be of type null; however, parameter $period of Pronamic\WordPress\Pay\P...s\Payment::add_period() does only seem to accept Pronamic\WordPress\Pay\S...ions\SubscriptionPeriod, maybe add an additional type check? ( Ignorable by Annotation )

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

134
			$payment->add_period( /** @scrutinizer ignore-type */ $payment->subscription->new_period() );
Loading history...
135
136
			if ( $memberpress_subscription->in_trial() ) {
137
				$payment->set_total_amount(
138
					new TaxedMoney(
139
						$memberpress_subscription->trial_total,
140
						MemberPress::get_currency(),
141
						$memberpress_subscription->trial_tax_amount,
142
						$memberpress_subscription->tax_rate
143
					)
144
				);
145
			}
146
		}
147
148
		/*
149
		 * Lines.
150
		 */
151
		$payment->lines = new PaymentLines();
152
153
		$line = $payment->lines->new_line();
154
155
		$line->set_id( $memberpress_product->ID );
156
		$line->set_name( $memberpress_product->post_title );
157
		$line->set_quantity( 1 );
158
		$line->set_unit_price( $payment->get_total_amount() );
159
		$line->set_total_amount( $payment->get_total_amount() );
160
161
		$product_url = \get_permalink( $memberpress_product->ID );
162
163
		if ( false !== $product_url ) {     
164
			$line->set_product_url( $product_url );
165
		}
166
167
		/*
168
		 * Return.
169
		 */
170
		return $payment;
171
	}
172
173
	/**
174
	 * Get Pronamic subscription from MemberPress transaction.
175
	 *
176
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
177
	 * @return Subscription|null
178
	 */
179
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
180
		$memberpress_product = $memberpress_transaction->product();
181
182
		if ( $memberpress_product->is_one_time_payment() ) {
183
			return null;
184
		}
185
186
		$memberpress_subscription = $memberpress_transaction->subscription();
187
188
		if ( ! $memberpress_subscription ) {
189
			return null;
190
		}
191
192
		/**
193
		 * Subscription.
194
		 */
195
		$subscription = new Subscription();
196
197
		$start_date = new \DateTimeImmutable();
198
199
		// Trial phase.
200
		if ( $memberpress_subscription->in_trial() ) {
201
			$trial_phase = new SubscriptionPhase(
202
				$subscription,
203
				$start_date,
204
				new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ),
205
				new TaxedMoney(
206
					$memberpress_subscription->trial_total,
207
					MemberPress::get_currency(),
208
					$memberpress_subscription->trial_tax_amount,
209
					$memberpress_subscription->tax_rate
210
				)
211
			);
212
213
			$trial_phase->set_total_periods( 1 );
214
			$trial_phase->set_trial( true );
215
216
			$subscription->add_phase( $trial_phase );
217
218
			$start_date = $trial_phase->get_end_date();
219
		}
220
221
		// Total periods.
222
		$total_periods = null;
223
224
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
225
226
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
227
			$total_periods = $limit_cycles_number;
228
		}
229
230
		// Regular phase.
231
		$regular_phase = new SubscriptionPhase(
232
			$subscription,
233
			$start_date,
0 ignored issues
show
Bug introduced by
It seems like $start_date can also be of type null; however, parameter $start_date of Pronamic\WordPress\Pay\S...ionPhase::__construct() does only seem to accept DateTimeImmutable, maybe add an additional type check? ( Ignorable by Annotation )

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

233
			/** @scrutinizer ignore-type */ $start_date,
Loading history...
234
			new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ),
235
			new Money( $memberpress_transaction->total, MemberPress::get_currency() )
236
		);
237
238
		$regular_phase->set_total_periods( $total_periods );
239
240
		$subscription->add_phase( $regular_phase );
241
242
		// Source.
243
		$subscription->source    = 'memberpress_subscription';
244
		$subscription->source_id = $memberpress_subscription->id;
245
246
		return $subscription;
247
	}
248
}
249