Passed
Push — develop ( cefaa3...56ef53 )
by Remco
05:09
created

PaymentData::get_country()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Payment data
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 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 MeprOptions;
14
use MeprTransaction;
15
use MeprUser;
16
use Pronamic\WordPress\Money\Money;
17
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
18
use Pronamic\WordPress\Pay\Payments\PaymentData as Pay_PaymentData;
19
use Pronamic\WordPress\Pay\Payments\Item;
20
use Pronamic\WordPress\Pay\Payments\Items;
21
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
22
23
/**
24
 * WordPress pay MemberPress payment data
25
 *
26
 * @author  Remco Tolsma
27
 * @version 2.0.0
28
 * @since   1.0.0
29
 */
30
class PaymentData extends Pay_PaymentData {
31
	/**
32
	 * MemberPress transaction.
33
	 *
34
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
35
	 *
36
	 * @var MeprTransaction
37
	 */
38
	private $txn;
39
40
	/**
41
	 * MemberPress transaction user.
42
	 *
43
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php
44
	 *
45
	 * @var MeprUser
46
	 */
47
	private $member;
48
49
	/**
50
	 * Constructs and initialize payment data object.
51
	 *
52
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php
53
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprTransaction.php#L596-L600
54
	 *
55
	 * @param MeprTransaction $txn MemberPress transaction object.
56
	 */
57
	public function __construct( MeprTransaction $txn ) {
58
		parent::__construct();
59
60
		$this->txn       = $txn;
61
		$this->member    = $this->txn->user();
62
		$this->recurring = ( $txn->subscription() && $txn->subscription()->txn_count > 1 );
0 ignored issues
show
Documentation Bug introduced by
It seems like $txn->subscription() && ...iption()->txn_count > 1 of type boolean is incompatible with the declared type Pronamic\WordPress\Pay\Payments\TODO of property $recurring.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
	}
64
65
	/**
66
	 * Get source slug.
67
	 *
68
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L56-L61
69
	 *
70
	 * @return string
71
	 */
72
	public function get_source() {
73
		return 'memberpress';
74
	}
75
76
	/**
77
	 * Get source ID.
78
	 *
79
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L63-L70
80
	 *
81
	 * @return string|int
82
	 */
83
	public function get_source_id() {
84
		return $this->txn->id;
85
	}
86
87
	/**
88
	 * Get order ID.
89
	 *
90
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L88-L93
91
	 *
92
	 * @return string|int
93
	 */
94
	public function get_order_id() {
95
		return $this->txn->id;
96
	}
97
98
	/**
99
	 * Get description.
100
	 *
101
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L81-L86
102
	 *
103
	 * @return string
104
	 */
105
	public function get_description() {
106
		return $this->txn->product()->post_title;
107
	}
108
109
	/**
110
	 * Get items.
111
	 *
112
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L95-L100
113
	 *
114
	 * @return Items
115
	 */
116
	public function get_items() {
117
		$items = new Items();
118
119
		$item = new Item();
120
		$item->setNumber( $this->get_order_id() );
121
		$item->setDescription( $this->get_description() );
122
		$item->setPrice( $this->txn->total );
123
		$item->setQuantity( 1 );
124
125
		$items->addItem( $item );
126
127
		return $items;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $items returns the type Pronamic\WordPress\Pay\Payments\Items which is incompatible with the return type mandated by Pronamic\WordPress\Pay\P...aymentData::get_items() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
128
	}
129
130
	/**
131
	 * Get currency alphabetic code.
132
	 *
133
	 * @link https://github.com/pronamic/wp-pronamic-ideal/blob/5.0.0/classes/Payments/AbstractPaymentData.php#L213-L218
134
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprOptions.php#L162-L163
135
	 *
136
	 * @return string
137
	 */
138
	public function get_currency_alphabetic_code() {
139
		$mepr_options = MeprOptions::fetch();
140
141
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L136-137
142
		return $mepr_options->currency_code;
143
	}
144
145
	/**
146
	 * Get email.
147
	 *
148
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L1103-L1105
149
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1229-L1231
150
	 *
151
	 * @return string
152
	 */
153
	public function get_email() {
154
		return $this->member->user_email;
155
	}
156
157
	/**
158
	 * Get first name.
159
	 *
160
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
161
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
162
	 *
163
	 * @return string
164
	 */
165
	public function get_first_name() {
166
		return $this->member->first_name;
167
	}
168
169
	/**
170
	 * Get last name.
171
	 *
172
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
173
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
174
	 *
175
	 * @return string
176
	 */
177
	public function get_last_name() {
178
		return $this->member->last_name;
179
	}
180
181
	/**
182
	 * Get customer name.
183
	 *
184
	 * @link https://github.com/wp-premium/memberpress-business/blob/1.2.7/app/models/MeprUser.php#L316-L319
185
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L376-L378
186
	 *
187
	 * @return string
188
	 */
189
	public function get_customer_name() {
190
		return $this->member->get_full_name();
191
	}
192
193
	/**
194
	 * Get address.
195
	 *
196
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
197
	 *
198
	 * @return string|null
199
	 */
200
	public function get_address() {
201
		$value = $this->member->address( 'one', false );
202
203
		if ( false === $value ) {
204
			return null;
205
		}
206
207
		return $value;
208
	}
209
210
	/**
211
	 * Get city.
212
	 *
213
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
214
	 *
215
	 * @return string|null
216
	 */
217
	public function get_city() {
218
		$value = $this->member->address( 'city', false );
219
220
		if ( false === $value ) {
221
			return null;
222
		}
223
224
		return $value;
225
	}
226
227
	/**
228
	 * Get ZIP.
229
	 *
230
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
231
	 *
232
	 * @return string|null
233
	 */
234
	public function get_zip() {
235
		$value = $this->member->address( 'zip', false );
236
237
		if ( false === $value ) {
238
			return null;
239
		}
240
241
		return $value;
242
	}
243
244
	/**
245
	 * Get country.
246
	 *
247
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprUser.php#L1115-L1140
248
	 *
249
	 * @return string|null
250
	 */
251
	public function get_country() {
252
		$value = $this->member->address( 'country', false );
253
254
		if ( false === $value ) {
255
			return null;
256
		}
257
258
		return $value;
259
	}
260
261
	/**
262
	 * Get normal return URL.
263
	 *
264
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/gateways/MeprPayPalStandardGateway.php#L1121
265
	 *
266
	 * @return string
267
	 */
268
	public function get_normal_return_url() {
269
		$mepr_options = MeprOptions::fetch();
270
271
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
272
		return $mepr_options->thankyou_page_url( 'trans_num=' . $this->txn->id );
273
	}
274
275
	/**
276
	 * Get cancel URL.
277
	 *
278
	 * @return string
279
	 */
280
	public function get_cancel_url() {
281
		return $this->get_normal_return_url();
282
	}
283
284
	/**
285
	 * Get success URL.
286
	 *
287
	 * @return string
288
	 */
289
	public function get_success_url() {
290
		return $this->get_normal_return_url();
291
	}
292
293
	/**
294
	 * Get error URL.
295
	 *
296
	 * @return string
297
	 */
298
	public function get_error_url() {
299
		return $this->get_normal_return_url();
300
	}
301
302
	/**
303
	 * Get subscription.
304
	 *
305
	 * @since 2.0.0
306
	 *
307
	 * @return Subscription|bool
308
	 */
309
	public function get_subscription() {
310
		$product = $this->txn->product();
311
312
		if ( $product->is_one_time_payment() ) {
313
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Pronamic\WordPress\Pay\P...ace::get_subscription() of Pronamic\WordPress\Pay\Subscriptions\Subscription.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
314
		}
315
316
		$mp_subscription = $this->txn->subscription();
317
318
		if ( ! $mp_subscription ) {
319
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Pronamic\WordPress\Pay\P...ace::get_subscription() of Pronamic\WordPress\Pay\Subscriptions\Subscription.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
320
		}
321
322
		$frequency = '';
323
324
		if ( $mp_subscription->limit_cycles ) {
325
			$frequency = $mp_subscription->limit_cycles;
326
		}
327
328
		$subscription                  = new Subscription();
329
		$subscription->frequency       = $frequency;
330
		$subscription->interval        = $product->period;
331
		$subscription->interval_period = Core_Util::to_period( $product->period_type );
0 ignored issues
show
Documentation Bug introduced by
The property $interval_period was declared of type integer, but Pronamic\WordPress\Pay\C...($product->period_type) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
332
		$subscription->description     = sprintf(
333
			'Order #%s - %s',
334
			$this->get_source_id(),
335
			$this->get_description()
336
		);
337
338
		$subscription->set_amount( new Money(
339
			$this->txn->total,
340
			$this->get_currency_alphabetic_code()
341
		) );
342
343
		return $subscription;
344
	}
345
346
	/**
347
	 * Get subscription source ID.
348
	 *
349
	 * @since  2.0.0
350
	 * @return string
351
	 */
352
	public function get_subscription_source_id() {
353
		$subscription = $this->get_subscription();
354
355
		if ( ! $subscription ) {
356
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
357
		}
358
359
		return $this->get_source_id();
360
	}
361
}
362