Failed Conditions
Push — develop ( 602393...5dc882 )
by Reüel
05:29
created

PaymentRequestHelper::complement()   B

Complexity

Conditions 11
Paths 96

Size

Total Lines 90
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 35.4737

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 45
c 3
b 0
f 0
nc 96
nop 2
dl 0
loc 90
ccs 19
cts 46
cp 0.413
crap 35.4737
rs 7.3166

How to fix   Long Method    Complexity   

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 request helper
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Pronamic\WordPress\Pay\Payments\Payment;
14
15
/**
16
 * Payment request helper
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.5
20
 * @since   1.0.0
21
 */
22
class PaymentRequestHelper {
23
	/**
24
	 * Complement WordPress Pay payment to Adyen payment request.
25
	 *
26
	 * @param Payment                $payment WordPress Pay payment to convert.
27
	 * @param AbstractPaymentRequest $request Adyen payment request.
28
	 * @return void
29
	 */
30 1
	public static function complement( Payment $payment, AbstractPaymentRequest $request ) {
31
		// Channel.
32 1
		$request->set_channel( Channel::WEB );
33
34
		// Shopper.
35 1
		$request->set_shopper_statement( $payment->get_description() );
36
37
		// Customer.
38 1
		$customer = $payment->get_customer();
39
40 1
		if ( null !== $customer ) {
41 1
			$request->set_shopper_reference( $customer->get_user_id() );
42 1
			$request->set_shopper_ip( $customer->get_ip_address() );
43 1
			$request->set_shopper_locale( $customer->get_locale() );
44 1
			$request->set_telephone_number( $customer->get_phone() );
45 1
			$request->set_shopper_email( $customer->get_email() );
46
47
			// Shopper name.
48 1
			$name = $customer->get_name();
49
50 1
			if ( null !== $name ) {
51
				$shopper_name = new Name(
52
					strval( $name->get_first_name() ),
53
					strval( $name->get_last_name() ),
54
					GenderTransformer::transform( $customer->get_gender() )
55
				);
56
57
				$request->set_shopper_name( $shopper_name );
58
			}
59
60
			// Date of birth.
61 1
			$request->set_date_of_birth( $customer->get_birth_date() );
62
		}
63
64
		// Billing address.
65 1
		$billing_address = $payment->get_billing_address();
66
67 1
		if ( null !== $billing_address ) {
68
			$address = AddressTransformer::transform( $billing_address );
69
70
			$request->set_billing_address( $address );
71
		}
72
73
		// Delivery address.
74 1
		$shipping_address = $payment->get_shipping_address();
75
76 1
		if ( null !== $shipping_address ) {
77
			$address = AddressTransformer::transform( $shipping_address );
78
79
			$request->set_delivery_address( $address );
80
		}
81
82
		// Lines.
83 1
		$lines = $payment->get_lines();
84
85 1
		if ( null !== $lines ) {
86
			$line_items = $request->new_line_items();
87
88
			$i = 1;
89
90
			foreach ( $lines as $line ) {
91
				// Description.
92
				$description = $line->get_description();
93
94
				// Use line item name as fallback for description.
95
				if ( null === $description ) {
96
					/* translators: %s: item index */
97
					$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i++ );
98
99
					if ( null !== $line->get_name() && '' !== $line->get_name() ) {
100
						$description = $line->get_name();
101
					}
102
				}
103
104
				$item = $line_items->new_item(
105
					(string) $description,
106
					(int) $line->get_quantity(),
107
					$line->get_total_amount()->get_including_tax()->get_minor_units()
108
				);
109
110
				$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() );
111
112
				$item->set_id( $line->get_id() );
113
114
				// Tax amount.
115
				$tax_amount = $line->get_total_amount()->get_tax_amount();
116
117
				if ( null !== $tax_amount ) {
118
					$item->set_tax_amount( $tax_amount->get_minor_units() );
119
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
120
				}
121
			}
122
		}
123 1
	}
124
}
125