Failed Conditions
Push — develop ( 5891c0...b4c81e )
by Remco
03:13
created

PaymentRequestTransformer::transform()   C

Complexity

Conditions 12
Paths 160

Size

Total Lines 88
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 44
nc 160
nop 2
dl 0
loc 88
ccs 0
cts 54
cp 0
crap 156
rs 6.4666
c 0
b 0
f 0

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 transformer
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\Address as Pay_Address;
14
15
/**
16
 * Payment request transformer
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.0
20
 * @since   1.0.0
21
 */
22
class PaymentRequestTransformer {
23
	/**
24
	 * Transform 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 AbstractPaymentRequest
29
	 */
30
	public static function transform( Payment $payment, AbstractPaymentRequest $request ) {
31
		// Channel.
32
		$request->set_channel( Channel::WEB );
33
34
		// Shopper.
35
		$request->set_shopper_statement( $payment->get_description() );
36
37
		if ( null !== $payment->get_customer() ) {
38
			$customer = $payment->get_customer();
39
40
			$request->set_shopper_ip( $customer->get_ip_address() );
41
			$request->set_shopper_locale( $customer->get_locale() );
42
			$request->set_shopper_reference( $customer->get_user_id() );
43
			$request->set_telephone_number( $customer->get_phone() );
44
45
			// Shopper name.
46
			if ( null !== $customer->get_name() ) {
47
				$shopper_name = new Name(
48
					$customer->get_name()->get_first_name(),
49
					$customer->get_name()->get_last_name(),
50
					GenderTransformer::transform( $customer->get_gender() )
51
				);
52
53
				$request->set_shopper_name( $shopper_name );
54
			}
55
56
			// Date of birth.
57
			if ( null !== $customer->get_birth_date() ) {
58
				$request->set_date_of_birth( $customer->get_birth_date()->format( 'YYYY-MM-DD' ) );
59
			}
60
		}
61
62
		// Billing address.
63
		$billing_address = $payment->get_billing_address();
64
65
		if ( null !== $billing_address ) {
66
			$address = AddressTransformer::transform( $billing_address );
67
68
			$request->set_billing_address( $address );
69
		}
70
71
		// Delivery address.
72
		$shipping_address = $payment->get_shipping_address();
73
74
		if ( null !== $shipping_address ) {
75
			$address = AddressTransformer::transform( $shipping_address );
76
77
			$request->set_delivery_address( $address );
78
		}
79
80
		// Lines.
81
		$lines = $payment->get_lines();
82
83
		if ( null !== $lines ) {
84
			$line_items = $request->new_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
					$description,
104
					$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_unit_price()->get_tax_amount();
114
115
				if ( null !== $tax_amount ) {
116
					$item->set_tax_amount( $line->get_total_amount()->get_tax_amount()->get_minor_units() );
117
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
118
				}
119
			}
120
		}
121
	}
122
}
123