Failed Conditions
Push — develop ( 7e55a1...24a52b )
by Remco
12:44 queued 04:26
created

Pronamic   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 18
Bugs 0 Features 0
Metric Value
eloc 97
c 18
b 0
f 0
dl 0
loc 202
ccs 0
cts 117
cp 0
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B get_payment() 0 121 4
B get_subscription() 0 64 6
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\TaxedMoney;
15
use Pronamic\WordPress\Pay\AddressHelper;
16
use Pronamic\WordPress\Pay\Customer;
17
use Pronamic\WordPress\Pay\ContactName;
18
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
use Pronamic\WordPress\Pay\Payments\PaymentLines;
21
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
22
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionInterval;
23
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionPhase;
24
25
/**
26
 * Pronamic
27
 *
28
 * @author  Remco Tolsma
29
 * @version 2.3.2
30
 * @since   2.0.5
31
 */
32
class Pronamic {
33
	/**
34
	 * Get Pronamic payment from MemberPress transaction.
35
	 *
36
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
37
	 *
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';
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
		 * Totals.
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
		 * @link https://github.com/wp-premium/memberpress-business/search?utf8=%E2%9C%93&q=mepr_vat_number&type=
117
		 * @todo
118
		 */
119
120
		/*
121
		 * Subscription.
122
		 * @link https://github.com/wp-premium/memberpress-business/blob/1.3.36/app/models/MeprTransaction.php#L603-L618
123
		 */
124
		$payment->subscription = self::get_subscription( $memberpress_transaction );
125
126
		if ( $payment->subscription ) {
127
			$payment->subscription_source_id = $memberpress_transaction->subscription_id;
128
129
			$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

129
			$payment->add_period( /** @scrutinizer ignore-type */ $payment->subscription->new_period() );
Loading history...
130
131
			if ( $memberpress_subscription->in_trial() ) {
132
				$payment->set_total_amount(
133
					new TaxedMoney(
134
						$memberpress_subscription->trial_total,
135
						MemberPress::get_currency(),
136
						$memberpress_subscription->trial_tax_amount,
137
						$memberpress_subscription->tax_rate
138
					)
139
				);
140
			}
141
		}
142
143
		/*
144
		 * Lines.
145
		 */
146
		$payment->lines = new PaymentLines();
147
148
		$line = $payment->lines->new_line();
149
150
		$line->set_id( $memberpress_product->ID );
151
		$line->set_name( $memberpress_product->post_title );
152
		$line->set_quantity( 1 );
153
		$line->set_unit_price( $payment->get_total_amount() );
154
		$line->set_total_amount( $payment->get_total_amount() );
155
		$line->set_product_url( get_permalink( $memberpress_product->ID ) );
0 ignored issues
show
Bug introduced by
It seems like get_permalink($memberpress_product->ID) can also be of type false; however, parameter $product_url of Pronamic\WordPress\Pay\P...Line::set_product_url() does only seem to accept null|string, 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

155
		$line->set_product_url( /** @scrutinizer ignore-type */ get_permalink( $memberpress_product->ID ) );
Loading history...
156
157
		/*
158
		 * Return.
159
		 */
160
		return $payment;
161
	}
162
163
	/**
164
	 * Get Pronamic subscription from MemberPress transaction.
165
	 *
166
	 * @param MeprTransaction $memberpress_transaction MemberPress transaction object.
167
	 *
168
	 * @return Subscription|null
169
	 */
170
	public static function get_subscription( MeprTransaction $memberpress_transaction ) {
171
		$memberpress_product = $memberpress_transaction->product();
172
173
		if ( $memberpress_product->is_one_time_payment() ) {
174
			return null;
175
		}
176
177
		$memberpress_subscription = $memberpress_transaction->subscription();
178
179
		if ( ! $memberpress_subscription ) {
180
			return null;
181
		}
182
183
		/**
184
		 * Subscription.
185
		 */
186
		$subscription = new Subscription();
187
188
		$start_date = new \DateTimeImmutable();
189
190
		// Trial phase.
191
		if ( $memberpress_subscription->in_trial() ) {
192
			$trial_phase = new SubscriptionPhase(
193
				$subscription,
194
				$start_date,
195
				new SubscriptionInterval( 'P' . $memberpress_subscription->trial_days . 'D' ),
196
				new TaxedMoney(
197
					$memberpress_subscription->trial_total,
198
					MemberPress::get_currency(),
199
					$memberpress_subscription->trial_tax_amount,
200
					$memberpress_subscription->tax_rate
201
				)
202
			);
203
204
			$trial_phase->set_total_periods( 1 );
205
			$trial_phase->set_trial( true );
206
207
			$subscription->add_phase( $trial_phase );
208
209
			$start_date = $trial_phase->get_end_date();
210
		}
211
212
		// Total periods.
213
		$total_periods = null;
214
215
		$limit_cycles_number = (int) $memberpress_subscription->limit_cycles_num;
216
217
		if ( $memberpress_subscription->limit_cycles && $limit_cycles_number > 0 ) {
218
			$total_periods = $limit_cycles_number;
219
		}
220
221
		// Regular phase.
222
		$regular_phase = new SubscriptionPhase(
223
			$subscription,
224
			$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

224
			/** @scrutinizer ignore-type */ $start_date,
Loading history...
225
			new SubscriptionInterval( 'P' . $memberpress_product->period . Core_Util::to_period( $memberpress_product->period_type ) ),
226
			new Money( $memberpress_transaction->total, MemberPress::get_currency() )
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\E...sions\MemberPress\Money 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...
227
		);
228
229
		$regular_phase->set_total_periods( $total_periods );
230
231
		$subscription->add_phase( $regular_phase );
232
233
		return $subscription;
234
	}
235
}
236