Change geocoder to use geocode data
This commit is contained in:
@@ -6,6 +6,7 @@ namespace Pcm\GeocodeBundle\Service;
|
|||||||
|
|
||||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||||
|
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
@@ -20,27 +21,28 @@ final class Geocoder
|
|||||||
public function __construct(private HttpClientInterface $client) {}
|
public function __construct(private HttpClientInterface $client) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find and return the geo-coordinates of a postcode
|
* Find and return the geo-coordinates of an entity
|
||||||
*
|
*
|
||||||
* @param string $postcode
|
* @param GeocodeInterface $entity
|
||||||
*
|
*
|
||||||
* @return GeoCoordinates
|
* @return GeoCoordinates
|
||||||
*
|
*
|
||||||
* @throws NoResultsFoundException when no results were found for the provided postcode
|
* @throws NoResultsFoundException when no results were found for the provided postcode
|
||||||
* @throws ApiErrorException when the API response contains an error
|
* @throws ApiErrorException when the API response contains an error
|
||||||
*/
|
*/
|
||||||
public function geocodePostcode(string $postcode): GeoCoordinates
|
public function geocode(GeocodeInterface $entity): GeoCoordinates
|
||||||
{
|
{
|
||||||
|
$geocodeData = $entity->getGeocodeData();
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
$response = $this->makeApiRequest($client, $geocodeData);
|
||||||
$data = $this->getDataFromResponse($response);
|
$data = $this->getDataFromResponse($response);
|
||||||
|
|
||||||
if (array_key_exists('error', $data)) {
|
if (array_key_exists('error', $data)) {
|
||||||
throw new ApiErrorException($postcode, $data['error']['message']);
|
throw new ApiErrorException($geocodeData, $data['error']['message']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
throw new NoResultsFoundException($postcode, "No results found.");
|
throw new NoResultsFoundException($geocodeData, "No results found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createGeoCoordinates($data);
|
return $this->createGeoCoordinates($data);
|
||||||
@@ -53,7 +55,7 @@ final class Geocoder
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
|
private function makeApiRequest(HttpClientInterface $client, string $geocodeData): ResponseInterface
|
||||||
{
|
{
|
||||||
return $client->request(
|
return $client->request(
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -61,7 +63,7 @@ final class Geocoder
|
|||||||
options: [
|
options: [
|
||||||
'query' => [
|
'query' => [
|
||||||
'format' => 'json',
|
'format' => 'json',
|
||||||
'postalcode' => $postcode
|
'q' => $geocodeData
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user