1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ups; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use Exception; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SimpleXMLElement; |
9
|
|
|
use stdClass; |
10
|
|
|
use Ups\Entity\Address; |
11
|
|
|
use Ups\Entity\AddressValidationResponse; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Address Validation API Wrapper to use the basic Address Validation endpoints. |
15
|
|
|
* |
16
|
|
|
* This functionality is more basic, but available in more countries than the 'extended' Address Validation methods. |
17
|
|
|
*/ |
18
|
|
|
class SimpleAddressValidation extends Ups |
19
|
|
|
{ |
20
|
|
|
const ENDPOINT = '/AV'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RequestInterface |
24
|
|
|
*/ |
25
|
|
|
private $request; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ResponseInterface |
29
|
|
|
* |
30
|
|
|
* @todo make private |
31
|
|
|
*/ |
32
|
|
|
public $response; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Address |
36
|
|
|
*/ |
37
|
|
|
private $address; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string|null $accessKey UPS License Access Key |
41
|
|
|
* @param string|null $userId UPS User ID |
42
|
|
|
* @param string|null $password UPS User Password |
43
|
|
|
* @param bool $useIntegration Determine if we should use production or CIE URLs. |
44
|
|
|
* @param RequestInterface|null $request |
45
|
|
|
* @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
46
|
|
|
*/ |
47
|
2 |
View Code Duplication |
public function __construct( |
|
|
|
|
48
|
|
|
$accessKey = null, |
49
|
|
|
$userId = null, |
50
|
|
|
$password = null, |
51
|
|
|
$useIntegration = false, |
52
|
|
|
RequestInterface $request = null, |
53
|
|
|
LoggerInterface $logger = null |
54
|
|
|
) { |
55
|
2 |
|
if (null !== $request) { |
56
|
|
|
$this->setRequest($request); |
57
|
|
|
} |
58
|
2 |
|
parent::__construct($accessKey, $userId, $password, $useIntegration, $logger); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get address suggestions from UPS using the default Address Validation API (/AV) |
63
|
|
|
* |
64
|
|
|
* @param Address $address |
65
|
|
|
* |
66
|
|
|
* @throws Exception |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
2 |
|
public function validate(Address $address) |
71
|
|
|
{ |
72
|
2 |
|
$this->address = $address; |
73
|
|
|
|
74
|
2 |
|
$access = $this->createAccess(); |
75
|
2 |
|
$request = $this->createRequest(); |
76
|
|
|
|
77
|
2 |
|
$this->response = $this->getRequest()->request($access, $request, $this->compileEndpointUrl(self::ENDPOINT)); |
78
|
2 |
|
$response = $this->response->getResponse(); |
79
|
|
|
|
80
|
2 |
|
if (null === $response) { |
81
|
1 |
|
throw new Exception('Failure (0): Unknown error', 0); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) { |
85
|
|
|
throw new Exception( |
86
|
|
|
"Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}", |
87
|
|
|
(int)$response->Response->Error->ErrorCode |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $this->formatResponse($response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create the AV request. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
2 |
|
private function createRequest() |
100
|
|
|
{ |
101
|
2 |
|
$xml = new DOMDocument(); |
102
|
2 |
|
$xml->formatOutput = true; |
103
|
|
|
|
104
|
2 |
|
$avRequest = $xml->appendChild($xml->createElement('AddressValidationRequest')); |
105
|
2 |
|
$avRequest->setAttribute('xml:lang', 'en-US'); |
106
|
|
|
|
107
|
2 |
|
$request = $avRequest->appendChild($xml->createElement('Request')); |
108
|
|
|
|
109
|
2 |
|
$node = $xml->importNode($this->createTransactionNode(), true); |
110
|
2 |
|
$request->appendChild($node); |
111
|
|
|
|
112
|
2 |
|
$request->appendChild($xml->createElement('RequestAction', 'AV')); |
113
|
|
|
|
114
|
2 |
|
if (null !== $this->address) { |
115
|
2 |
|
$addressNode = $avRequest->appendChild($xml->createElement('Address')); |
116
|
|
|
|
117
|
2 |
|
if ($this->address->getStateProvinceCode()) { |
118
|
2 |
|
$addressNode->appendChild($xml->createElement('StateProvinceCode', $this->address->getStateProvinceCode())); |
119
|
2 |
|
} |
120
|
2 |
|
if ($this->address->getCity()) { |
121
|
2 |
|
$addressNode->appendChild($xml->createElement('City', $this->address->getCity())); |
122
|
2 |
|
} |
123
|
2 |
|
if ($this->address->getCountryCode()) { |
124
|
2 |
|
$addressNode->appendChild($xml->createElement('CountryCode', $this->address->getCountryCode())); |
125
|
2 |
|
} |
126
|
2 |
|
if ($this->address->getPostalCode()) { |
127
|
2 |
|
$addressNode->appendChild($xml->createElement('PostalCode', $this->address->getPostalCode())); |
128
|
2 |
|
} |
129
|
2 |
|
} |
130
|
|
|
|
131
|
2 |
|
return $xml->saveXML(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Format the response. |
136
|
|
|
* |
137
|
|
|
* @param SimpleXMLElement $response |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
1 |
|
private function formatResponse(SimpleXMLElement $response) |
142
|
|
|
{ |
143
|
1 |
|
$result = $this->convertXmlObject($response); |
144
|
|
|
|
145
|
1 |
|
if (!is_array($result->AddressValidationResult)) { |
146
|
|
|
return [$result->AddressValidationResult]; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $result->AddressValidationResult; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return RequestInterface |
154
|
|
|
*/ |
155
|
2 |
|
public function getRequest() |
156
|
|
|
{ |
157
|
2 |
|
if (null === $this->request) { |
158
|
|
|
$this->request = new Request($this->logger); |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
return $this->request; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param RequestInterface $request |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
2 |
|
public function setRequest(RequestInterface $request) |
170
|
|
|
{ |
171
|
2 |
|
$this->request = $request; |
172
|
|
|
|
173
|
2 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return ResponseInterface |
178
|
|
|
*/ |
179
|
|
|
public function getResponse() |
180
|
|
|
{ |
181
|
|
|
return $this->response; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param ResponseInterface $response |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function setResponse(ResponseInterface $response) |
190
|
|
|
{ |
191
|
|
|
$this->response = $response; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.