Passed
Push — develop ( c2f1df...779f55 )
by Reüel
05:07
created

PaymentRequestHelper::complement()   B

Complexity

Conditions 11
Paths 96

Size

Total Lines 88
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 38.9538

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 43
c 1
b 0
f 0
nc 96
nop 2
dl 0
loc 88
ccs 17
cts 44
cp 0.3864
crap 38.9538
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-2019 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.0
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_ip( $customer->get_ip_address() );
42 1
			$request->set_shopper_locale( $customer->get_locale() );
43 1
			$request->set_telephone_number( $customer->get_phone() );
44
45
			// Shopper name.
46 1
			$name = $customer->get_name();
47
48 1
			if ( null !== $name ) {
49
				$shopper_name = new Name(
50
					strval( $name->get_first_name() ),
51
					strval( $name->get_last_name() ),
52
					GenderTransformer::transform( $customer->get_gender() )
53
				);
54
55
				$request->set_shopper_name( $shopper_name );
56
			}
57
58
			// Date of birth.
59 1
			$request->set_date_of_birth( $customer->get_birth_date() );
60
		}
61
62
		// Billing address.
63 1
		$billing_address = $payment->get_billing_address();
64
65 1
		if ( null !== $billing_address ) {
66
			$address = AddressTransformer::transform( $billing_address );
67
68
			$request->set_billing_address( $address );
69
		}
70
71
		// Delivery address.
72 1
		$shipping_address = $payment->get_shipping_address();
73
74 1
		if ( null !== $shipping_address ) {
75
			$address = AddressTransformer::transform( $shipping_address );
76
77
			$request->set_delivery_address( $address );
78
		}
79
80
		// Lines.
81 1
		$lines = $payment->get_lines();
82
83 1
		if ( null !== $lines ) {
84
			$line_items = $request->new_line_items();
85
86
			$i = 1;
87
88
			foreach ( $lines as $line ) {
89
				// Description.
90
				$description = $line->get_description();
91
92
				// Use line item name as fallback for description.
93
				if ( null === $description ) {
94
					/* translators: %s: item index */
95
					$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i++ );
96
97
					if ( null !== $line->get_name() && '' !== $line->get_name() ) {
98
						$description = $line->get_name();
99
					}
100
				}
101
102
				$item = $line_items->new_item(
103
					(string) $description,
104
					(int) $line->get_quantity(),
105
					$line->get_total_amount()->get_including_tax()->get_minor_units()
106
				);
107
108
				$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() );
109
110
				$item->set_id( $line->get_id() );
111
112
				// Tax amount.
113
				$tax_amount = $line->get_total_amount()->get_tax_amount();
114
115
				if ( null !== $tax_amount ) {
116
					$item->set_tax_amount( $tax_amount->get_minor_units() );
117
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
118
				}
119
			}
120
		}
121 1
	}
122
}
123