-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostcodeApiNu.php
More file actions
142 lines (122 loc) · 4.04 KB
/
PostcodeApiNu.php
File metadata and controls
142 lines (122 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace DevMob\Postcodes\Providers\PostcodeApiNu;
use DevMob\Postcodes\Exceptions\BadRequestException;
use DevMob\Postcodes\Exceptions\BadResponseException;
use DevMob\Postcodes\Exceptions\JsonException;
use DevMob\Postcodes\Exceptions\NoSuchCombinationException;
use DevMob\Postcodes\Providers\HttpProvider;
use DevMob\Postcodes\Providers\PostcodeApiNu\Exceptions\RequestLimitExceededException;
use DevMob\Postcodes\Util\Json;
use DevMob\Postcodes\Util\Uri;
use GuzzleHttp\Psr7\Request;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* @link https://www.postcodeapi.nu/docs/
*/
class PostcodeApiNu extends HttpProvider
{
protected const HOST = 'https://api.postcodeapi.nu/v2/addresses';
/**
* @var array
*/
private $config;
/**
* @var \DevMob\Postcodes\Providers\PostcodeApiNu\AddressFactory
*/
private $addressFactory;
/**
* Constructor.
*
* @param array $config
* @param \DevMob\Postcodes\Providers\PostcodeApiNu\AddressFactory|null $addressFactory
*/
public function __construct(array $config, ?AddressFactory $addressFactory = null)
{
$this->config = $config;
$this->addressFactory = $addressFactory ?: new AddressFactory();
$this->validateConfig();
}
/**
* Create http lookup request.
*
* @param array $input
* @return \Psr\Http\Message\RequestInterface
*/
protected function request(array $input): RequestInterface
{
$query = [
'postcode' => $input['postcode'],
'number' => $input['number'],
];
$host = isset($this->config['host']) ? $this->config['host'] : self::HOST;
$uri = Uri::create($host, $query);
$headers = [
'X-Api-Key' => $this->config['key'],
];
return new Request('GET', $uri, $headers);
}
/**
* Parse http lookup response.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $input
* @return \DevMob\Postcodes\Address\Address[]
* @throws \DevMob\Postcodes\Exceptions\NoSuchCombinationException
* @throws \DevMob\Postcodes\Exceptions\ProviderException
*/
protected function parse(ResponseInterface $response, array $input): array
{
switch ($response->getStatusCode()) {
case 400:
case 401:
case 403:
throw new BadRequestException($this->getError($response));
case 429:
throw new RequestLimitExceededException($this->getError($response));
}
try {
$decoded = Json::decode($response);
} catch (JsonException $e) {
throw new BadResponseException('Received a malformed JSON response.', $response, $e);
}
if (! isset($decoded['_embedded']['addresses'])) {
throw new BadResponseException('Missing addresses array in response.', $response);
}
if (count($decoded['_embedded']['addresses']) === 0) {
throw new NoSuchCombinationException($input['postcode'], $input['number']);
}
$addresses = [];
foreach ($decoded['_embedded']['addresses'] as $address) {
$addresses[] = $this->addressFactory->create($address);
}
return $addresses;
}
/**
* Validate configuration.
*
* @return void
*/
private function validateConfig(): void
{
if (! isset($this->config['key'])) {
throw new InvalidArgumentException('Configuration is missing the \'key\' key.');
}
}
/**
* Get the error message from the response.
*
* @param \Psr\Http\Message\ResponseInterface $response
* @return string
*/
private function getError(ResponseInterface $response): string
{
$default = 'Unknown error: no error message in response.';
try {
return Json::decode($response)['error'] ?? $default;
} catch (JsonException $e) {
return $default;
}
}
}