Failed Conditions
Push — develop ( aa7202...c2ddef )
by Reüel
04:59
created

PaymentRequestHelper::complement()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 116
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 33.3655

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 12
eloc 50
nc 24
nop 2
dl 0
loc 116
ccs 24
cts 51
cp 0.4706
crap 33.3655
rs 6.9666
c 5
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 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
			$user_id = $customer->get_user_id();
42
43 1
			$request->set_shopper_reference( \is_null( $user_id ) ? null : \strval( $user_id ) );
44 1
			$request->set_shopper_ip( $customer->get_ip_address() );
45 1
			$request->set_shopper_locale( $customer->get_locale() );
46 1
			$request->set_telephone_number( $customer->get_phone() );
47 1
			$request->set_shopper_email( $customer->get_email() );
48
49
			// Shopper name.
50 1
			$name = $customer->get_name();
51
52 1
			if ( null !== $name ) {
53
				$shopper_name = new Name(
54
					strval( $name->get_first_name() ),
55
					strval( $name->get_last_name() ),
56
					GenderTransformer::transform( $customer->get_gender() )
57
				);
58
59
				$request->set_shopper_name( $shopper_name );
60
			}
61
62
			// Date of birth.
63 1
			$request->set_date_of_birth( $customer->get_birth_date() );
64
		}
65
66
		// Billing address.
67 1
		$billing_address = $payment->get_billing_address();
68
69 1
		if ( null !== $billing_address ) {
70
			$address = AddressTransformer::transform( $billing_address );
71
72
			$request->set_billing_address( $address );
73
		}
74
75
		// Delivery address.
76 1
		$shipping_address = $payment->get_shipping_address();
77
78 1
		if ( null !== $shipping_address ) {
79
			$address = AddressTransformer::transform( $shipping_address );
80
81
			$request->set_delivery_address( $address );
82
		}
83
84
		// Lines.
85 1
		$lines = $payment->get_lines();
86
87 1
		if ( null !== $lines ) {
88
			$line_items = $request->new_line_items();
89
90
			$i = 1;
91
92
			foreach ( $lines as $line ) {
93
				// Description.
94
				$description = $line->get_description();
95
96
				// Use line item name as fallback for description.
97
				if ( null === $description ) {
98
					/* translators: %s: item index */
99
					$description = sprintf( __( 'Item %s', 'pronamic_ideal' ), $i++ );
100
101
					if ( null !== $line->get_name() && '' !== $line->get_name() ) {
102
						$description = $line->get_name();
103
					}
104
				}
105
106
				$item = $line_items->new_item(
107
					(string) $description,
108
					(int) $line->get_quantity(),
109
					$line->get_total_amount()->get_including_tax()->get_minor_units()
110
				);
111
112
				$item->set_amount_excluding_tax( $line->get_total_amount()->get_excluding_tax()->get_minor_units() );
113
114
				$item->set_id( $line->get_id() );
115
116
				// Tax amount.
117
				$tax_amount = $line->get_total_amount()->get_tax_amount();
118
119
				if ( null !== $tax_amount ) {
120
					$item->set_tax_amount( $tax_amount->get_minor_units() );
121
					$item->set_tax_percentage( (int) $line->get_total_amount()->get_tax_percentage() * 100 );
122
				}
123
			}
124
		}
125
126
		// Metadata.
127 1
		$metadata = array();
128
129
		/**
130
		 * Filters the Adyen checkout configuration.
131
		 *
132
		 * @param array $metadata Payment request metadata.
133
		 * @since 1.1.1
134
		 */
135 1
		$metadata = apply_filters( 'pronamic_pay_adyen_payment_metadata', $metadata, $payment );
136
137
		/*
138
		 * Maximum 20 key-value pairs per request. When exceeding, the "177" error occurs: "Metadata size exceeds limit".
139
		 *
140
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/payments__reqParam_metadata
141
		 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/paymentSession__reqParam_metadata
142
		 */
143 1
		$metadata = \array_slice( $metadata, 0, 20, true );
144
145 1
		$request->set_metadata( $metadata );
146 1
	}
147
}
148