Address   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 237
ccs 62
cts 62
cp 1
rs 10
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_postal_code() 0 2 1
A get_country() 0 2 1
A get_state_or_province() 0 2 1
A get_city() 0 2 1
A get_house_number_or_name() 0 2 1
A get_street() 0 2 1
C __construct() 0 106 17
A get_json() 0 15 1
1
<?php
2
/**
3
 * Address
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
15
/**
16
 * Address
17
 *
18
 * @link https://docs.adyen.com/developers/api-reference/common-api/address
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.0
22
 * @since   1.0.0
23
 */
24
class Address {
25
	/**
26
	 * City.
27
	 *
28
	 * @var string|null
29
	 */
30
	private $city;
31
32
	/**
33
	 * Country.
34
	 *
35
	 * @var string
36
	 */
37
	private $country;
38
39
	/**
40
	 * House number or name.
41
	 *
42
	 * @var string|null
43
	 */
44
	private $house_number_or_name;
45
46
	/**
47
	 * Postal code.
48
	 *
49
	 * @var string|null
50
	 */
51
	private $postal_code;
52
53
	/**
54
	 * State or province.
55
	 *
56
	 * @var string|null
57
	 */
58
	private $state_or_province;
59
60
	/**
61
	 * Street.
62
	 *
63
	 * @var string|null
64
	 */
65
	private $street;
66
67
	/**
68
	 * Construct address.
69
	 *
70
	 * @param string $country              Country.
71
	 * @param string $street               Street.
72
	 * @param string $house_number_or_name House number or name.
73
	 * @param string $postal_code          Postal code.
74
	 * @param string $city                 City.
75
	 * @param string $state_or_province    State or province.
76
	 *
77
	 * @throws InvalidArgumentException Throws invalid argument exception when Adyen address requirements are not met.
78
	 */
79 14
	public function __construct( $country, $street = null, $house_number_or_name = null, $postal_code = null, $city = null, $state_or_province = null ) {
80
		/*
81
		 * The two-character country code of the address.
82
		 *
83
		 * The permitted country codes are defined in ISO-3166-1 alpha-2 (e.g. 'NL').
84
		 */
85 14
		if ( 2 !== strlen( $country ) ) {
86 1
			throw new InvalidArgumentException(
87 1
				sprintf(
88 1
					'Given country `%s` not ISO 3166-1 alpha-2 value.',
89
					$country
90
				)
91
			);
92
		}
93
94
		/*
95
		 * The name of the street.
96
		 *
97
		 * > The house number should not be included in this field; it should be separately provided via houseNumberOrName.
98
		 *
99
		 * Required if either `houseNumberOrName`, `city`, `postalCode`, or `stateOrProvince` are provided.
100
		 */
101 13
		$fields = array_filter( array( $house_number_or_name, $city, $postal_code, $state_or_province ) );
102
103 13
		if ( empty( $street ) && ! empty( $fields ) ) {
104 1
			throw new InvalidArgumentException(
105 1
				'The name of the street is required if either `houseNumberOrName`, `city`, `postalCode`, or `stateOrProvince` are provided.'
106
			);
107
		}
108
109
		/*
110
		 * The postal code.
111
		 *
112
		 * A maximum of five (5) digits for an address in the USA, or a maximum of ten (10) characters for an address in all other countries.
113
		 *
114
		 * Required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided.
115
		 */
116 12
		$fields = array_filter( array( $house_number_or_name, $street, $city, $state_or_province ) );
117
118 12
		if ( empty( $postal_code ) && ! empty( $fields ) ) {
119 2
			throw new InvalidArgumentException(
120 2
				'The postal code is required if either `houseNumberOrName`, `street`, `city`, or `stateOrProvince` are provided.'
121
			);
122
		}
123
124 10
		if ( ! empty( $postal_code ) ) {
125 7
			$max = ( 'US' === $country ) ? 5 : 10;
126
127 7
			if ( strlen( $postal_code ) > $max ) {
128 1
				throw new InvalidArgumentException(
129 1
					sprintf(
130 1
						'Given postal code `%s` is longer then `%d` digits.',
131
						$postal_code,
132
						$max
133
					)
134
				);
135
			}
136
		}
137
138
		/*
139
		 * The name of the city.
140
		 *
141
		 * Required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided.
142
		 */
143 9
		$fields = array_filter( array( $house_number_or_name, $street, $postal_code, $state_or_province ) );
144
145 9
		if ( empty( $city ) && ! empty( $fields ) ) {
146 1
			throw new InvalidArgumentException(
147 1
				'The name of the city is required if either `houseNumberOrName`, `street`, `postalCode`, or `stateOrProvince` are provided.'
148
			);
149
		}
150
151
		/*
152
		 * Two (2) characters for an address in the USA or Canada, or a maximum of three (3) characters for an address in all other countries.
153
		 *
154
		 * Required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided.
155
		 */
156 8
		$fields = array_filter( array( $house_number_or_name, $street, $city, $postal_code ) );
157
158 8
		if ( empty( $state_or_province ) && in_array( $country, array( 'CA', 'US' ), true ) && ! empty( $fields ) ) {
159 1
			throw new InvalidArgumentException(
160 1
				'State or province is required for an address in the USA or Canada if either `houseNumberOrName`, `street`, `city`, or `postalCode` are provided.'
161
			);
162
		}
163
164 7
		if ( ! empty( $state_or_province ) ) {
165 3
			$max = in_array( $country, array( 'CA', 'US' ), true ) ? 2 : 3;
166
167 3
			if ( strlen( $state_or_province ) > $max ) {
168 1
				throw new InvalidArgumentException(
169 1
					sprintf(
170 1
						'Given state or province `%s` is longer then `%d` digits.',
171
						$state_or_province,
172
						$max
173
					)
174
				);
175
			}
176
		}
177
178
		// Ok.
179 6
		$this->country              = $country;
180 6
		$this->street               = $street;
181 6
		$this->house_number_or_name = $house_number_or_name;
182 6
		$this->postal_code          = $postal_code;
183 6
		$this->city                 = $city;
184 6
		$this->state_or_province    = $state_or_province;
185 6
	}
186
187
	/**
188
	 * Get city.
189
	 *
190
	 * @return string|null
191
	 */
192 4
	public function get_city() {
193 4
		return $this->city;
194
	}
195
196
	/**
197
	 * Get country.
198
	 *
199
	 * @return string
200
	 */
201 6
	public function get_country() {
202 6
		return $this->country;
203
	}
204
205
	/**
206
	 * Get house number or name.
207
	 *
208
	 * @return string|null
209
	 */
210 4
	public function get_house_number_or_name() {
211 4
		return $this->house_number_or_name;
212
	}
213
214
	/**
215
	 * Get postal code.
216
	 *
217
	 * @return string|null
218
	 */
219 4
	public function get_postal_code() {
220 4
		return $this->postal_code;
221
	}
222
223
	/**
224
	 * Get state or province.
225
	 *
226
	 * @return string|null
227
	 */
228 4
	public function get_state_or_province() {
229 4
		return $this->state_or_province;
230
	}
231
232
	/**
233
	 * Get street.
234
	 *
235
	 * @return string|null
236
	 */
237 4
	public function get_street() {
238 4
		return $this->street;
239
	}
240
241
	/**
242
	 * Get JSON.
243
	 *
244
	 * @return object
245
	 */
246 1
	public function get_json() {
247 1
		$properties = Util::filter_null(
248
			array(
249 1
				'country'           => $this->country,
250 1
				'city'              => $this->city,
251 1
				'houseNumberOrName' => $this->house_number_or_name,
252 1
				'postalCode'        => $this->postal_code,
253 1
				'stateOrProvince'   => $this->state_or_province,
254 1
				'street'            => $this->street,
255
			)
256
		);
257
258 1
		$object = (object) $properties;
259
260 1
		return $object;
261
	}
262
}
263