Passed
Push — develop ( 241920...38771c )
by Reüel
04:19
created

PaymentTestData::get_subscription()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 8
nop 0
dl 0
loc 63
rs 6.8825
c 0
b 0
f 0

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
 * Payment Test Data
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Payments;
12
13
use DateInterval;
14
use DatePeriod;
15
use DateTime;
16
use Pronamic\WordPress\Pay\CreditCard;
17
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
18
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
19
use WP_User;
20
21
/**
22
 * WordPress payment test data
23
 *
24
 * @author Remco Tolsma
25
 * @version 1.0
26
 */
27
class PaymentTestData extends PaymentData {
28
	/**
29
	 * WordPress user.
30
	 *
31
	 * @var WP_User
32
	 */
33
	private $user;
34
35
	/**
36
	 * Amount.
37
	 *
38
	 * @var float
39
	 */
40
	private $amount;
41
42
	/**
43
	 * Constructs and initializes an iDEAL test data proxy.
44
	 *
45
	 * @param WP_User $user   A WordPress user.
46
	 * @param float   $amount The amount to test.
47
	 */
48
	public function __construct( WP_User $user, $amount ) {
49
		parent::__construct();
50
51
		$this->user   = $user;
52
		$this->amount = $amount;
53
	}
54
55
	/**
56
	 * Get source indicator.
57
	 *
58
	 * @see Pronamic_Pay_PaymentDataInterface::get_source()
59
	 * @return string
60
	 */
61
	public function get_source() {
62
		return 'test';
63
	}
64
65
	/**
66
	 * Get description.
67
	 *
68
	 * @see Pronamic_Pay_PaymentDataInterface::get_description()
69
	 * @return string
70
	 */
71
	public function get_description() {
72
		return sprintf( __( 'Test %s', 'pronamic_ideal' ), $this->get_order_id() );
73
	}
74
75
	/**
76
	 * Get order ID.
77
	 *
78
	 * @see Pronamic_Pay_PaymentDataInterface::get_order_id()
79
	 * @return string
80
	 */
81
	public function get_order_id() {
82
		return time();
83
	}
84
85
	/**
86
	 * Get items.
87
	 *
88
	 * @see Pronamic_Pay_PaymentDataInterface::get_items()
89
	 * @return Items
90
	 */
91
	public function get_items() {
92
		// Items.
93
		$items = new Items();
94
95
		// Item.
96
		$item = new Item();
97
		$item->setNumber( $this->get_order_id() );
98
		$item->setDescription( sprintf( __( 'Test %s', 'pronamic_ideal' ), $this->get_order_id() ) );
99
		$item->setPrice( $this->amount );
100
		$item->setQuantity( 1 );
101
102
		$items->addItem( $item );
103
104
		return $items;
105
	}
106
107
	/**
108
	 * Get currency alphabetic code.
109
	 *
110
	 * @see Pronamic_Pay_PaymentDataInterface::get_currency_alphabetic_code()
111
	 * @return string
112
	 */
113
	public function get_currency_alphabetic_code() {
114
		return 'EUR';
115
	}
116
117
	/**
118
	 * Get address.
119
	 *
120
	 * @return string
121
	 */
122
	public function get_address() {
123
		return '';
124
	}
125
126
	/**
127
	 * Get city.
128
	 *
129
	 * @return string
130
	 */
131
	public function get_city() {
132
		return '';
133
	}
134
135
	/**
136
	 * Get ZIP.
137
	 *
138
	 * @return string
139
	 */
140
	public function get_zip() {
141
		return '';
142
	}
143
144
	/**
145
	 * Get subscription.
146
	 *
147
	 * @since 1.2.1
148
	 * @see https://github.com/woothemes/woocommerce/blob/v2.1.3/includes/abstracts/abstract-wc-payment-gateway.php#L52
149
	 * @see https://github.com/wp-premium/woocommerce-subscriptions/blob/2.0.18/includes/class-wc-subscriptions-renewal-order.php#L371-L398
150
	 * @return string|bool
151
	 */
152
	public function get_subscription() {
153
		$test_subscription = filter_input( INPUT_POST, 'pronamic_pay_test_subscription', FILTER_VALIDATE_BOOLEAN );
154
155
		if ( ! $test_subscription ) {
156
			return false;
157
		}
158
159
		$interval = filter_input( INPUT_POST, 'pronamic_pay_test_repeat_interval', FILTER_VALIDATE_INT );
160
161
		if ( empty( $interval ) ) {
162
			return false;
163
		}
164
165
		$interval_period = filter_input( INPUT_POST, 'pronamic_pay_test_repeat_frequency', FILTER_SANITIZE_STRING );
166
167
		if ( empty( $interval_period ) ) {
168
			return false;
169
		}
170
171
		// Ends on.
172
		$ends_on = filter_input( INPUT_POST, 'pronamic_pay_ends_on', FILTER_SANITIZE_STRING );
173
174
		$times = null;
175
176
		switch ( $ends_on ) {
177
			case 'count':
178
				$count = filter_input( INPUT_POST, 'pronamic_pay_ends_on_count', FILTER_VALIDATE_INT );
179
180
				if ( ! empty( $count ) ) {
181
					$times = $count;
182
				}
183
184
				break;
185
			case 'date':
186
				$end_date = filter_input( INPUT_POST, 'pronamic_pay_ends_on_date', FILTER_SANITIZE_STRING );
187
188
				if ( ! empty( $end_date ) ) {
189
					/* translators: 1: interval, 2: interval period */
190
					$interval_spec = sprintf( 'P%1$s%2$s', $interval, Core_Util::to_period( $interval_period ) );
191
192
					$period = new DatePeriod(
193
						new DateTime(),
194
						new DateInterval( $interval_spec ),
195
						new DateTime( $end_date )
196
					);
197
198
					$times = iterator_count( $period );
199
				}
200
201
				break;
202
		}
203
204
		// Subscription.
205
		$subscription = new Subscription();
206
207
		$subscription->currency        = $this->get_currency();
208
		$subscription->description     = $this->get_description();
209
		$subscription->amount          = $this->get_amount();
210
		$subscription->frequency       = $times;
211
		$subscription->interval        = $interval;
212
		$subscription->interval_period = Core_Util::to_period( $interval_period );
0 ignored issues
show
Documentation Bug introduced by
The property $interval_period was declared of type integer, but Pronamic\WordPress\Pay\C...eriod($interval_period) 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...
213
214
		return $subscription;
215
	}
216
217
	/**
218
	 * Get credit card.
219
	 *
220
	 * @return CreditCard
221
	 */
222
	public function get_credit_card() {
223
		$credit_card = new CreditCard();
224
225
		// @see http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
226
		// Test card to simulate a 3-D Secure registered card.
227
		// $credit_card->set_number( '5555555555554444' );
228
		// $credit_card->set_number( '4111111111111111' );
229
		// $credit_card->set_number( '4000000000000002' );
230
		$credit_card->set_number( '5300000000000006' );
231
232
		$credit_card->set_expiration_month( 12 );
233
234
		$credit_card->set_expiration_year( date( 'Y' ) + 5 );
235
236
		$credit_card->set_security_code( '123' );
237
238
		$credit_card->set_name( 'Pronamic' );
239
240
		return $credit_card;
241
	}
242
}
243