Passed
Push — develop ( e1258e...700cbd )
by Remco
03:11
created

AddressTransformer::transform()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 6
nop 1
dl 0
loc 35
ccs 18
cts 21
cp 0.8571
crap 5.0729
rs 9.2728
c 0
b 0
f 0
1
<?php
2
/**
3
 * Address 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 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 3
	public static function transform( Pay_Address $address ) {
31 3
		$country = $address->get_country();
32
33 3
		if ( null === $country ) {
34 1
			return null;
35
		}
36
37 2
		$country_code = $country->get_code();
38
39 2
		if ( null === $country_code ) {
40
			return null;
41
		}
42
43 2
		$state_or_province = null;
44
45 2
		$region = $address->get_region();
46
47 2
		if ( null !== $region ) {
48 1
			$state_or_province = $region->get_code();
49
		}
50
51
		try {
52 2
			$address = new Address(
53 2
				$country_code,
54 2
				$address->get_street_name(),
55 2
				strval( $address->get_house_number() ),
56 2
				$address->get_postal_code(),
57 2
				$address->get_city(),
58 2
				$state_or_province
59
			);
60
		} catch ( InvalidArgumentException $exception ) {
61
			return null;
62
		}
63
64 2
		return $address;
65
	}
66
}
67