AddressTransformer::transform()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 35
ccs 20
cts 20
cp 1
crap 5
rs 9.2728
1
<?php
2
/**
3
 * Address transformer
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 InvalidArgumentException;
14
use Pronamic\WordPress\Pay\Address as Pay_Address;
15
16
/**
17
 * Address transformer
18
 *
19
 * @author  Remco Tolsma
20
 * @version 1.0.0
21
 * @since   1.0.0
22
 */
23
class AddressTransformer {
24
	/**
25
	 * Transform WordPress Pay address to Adyen address.
26
	 *
27
	 * @param Pay_Address $address WordPress Pay address to convert.
28
	 * @return Address|null
29
	 */
30 5
	public static function transform( Pay_Address $address ) {
31 5
		$country = $address->get_country();
32
33 5
		if ( null === $country ) {
34 1
			return null;
35
		}
36
37 4
		$country_code = $country->get_code();
38
39 4
		if ( null === $country_code ) {
40 1
			return null;
41
		}
42
43 3
		$state_or_province = null;
44
45 3
		$region = $address->get_region();
46
47 3
		if ( null !== $region ) {
48 1
			$state_or_province = $region->get_code();
49
		}
50
51
		try {
52 3
			$address = new Address(
53 3
				$country_code,
54 3
				$address->get_street_name(),
55 3
				(string) $address->get_house_number(),
56 3
				$address->get_postal_code(),
57 3
				$address->get_city(),
58
				$state_or_province
59
			);
60 1
		} catch ( InvalidArgumentException $exception ) {
61 1
			return null;
62
		}
63
64 2
		return $address;
65
	}
66
}
67