Adjust names
This commit is contained in:
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Service;
|
||||
|
||||
use Exception;
|
||||
use Pcm\GeocodeBundle\Entity\GeocodeData;
|
||||
use Pcm\GeocodeBundle\Model\LatLonModel;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
@@ -24,22 +23,22 @@ class Geocoder
|
||||
*/
|
||||
public function geocodePostcode(string $postcode): LatLonModel
|
||||
{
|
||||
$client = $this->setClientHeaders();
|
||||
$response = $this->makeApiRequest($client, $postcode);
|
||||
$client = $this->createClient();
|
||||
$response = $this->requestGeocodeData($client, $postcode);
|
||||
$data = $response->toArray();
|
||||
$this->throwIfNoResponseData($data);
|
||||
|
||||
return $this->createGeocodeDataObject($data);
|
||||
return $this->createLatLonObject($data);
|
||||
}
|
||||
|
||||
private function setClientHeaders(): HttpClientInterface
|
||||
private function createClient(): HttpClientInterface
|
||||
{
|
||||
return $this->client->withOptions([
|
||||
'headers' => ["User-Agent" => "Geocode"]
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
|
||||
private function requestGeocodeData(HttpClientInterface $client, string $postcode): ResponseInterface
|
||||
{
|
||||
return $client->request(
|
||||
method: 'GET',
|
||||
@@ -56,10 +55,10 @@ class Geocoder
|
||||
private function throwIfNoResponseData(array $data): void
|
||||
{
|
||||
if (empty($data))
|
||||
throw new Exception("No data was received from API response! Were the arguments valid?");
|
||||
throw new \Exception("No data was received from API response! Were the arguments valid?");
|
||||
}
|
||||
|
||||
private function createGeocodeDataObject(array $data): LatLonModel
|
||||
private function createLatLonObject(array $data): LatLonModel
|
||||
{
|
||||
return new LatLonModel((float) $data[0]['lat'], (float) $data[0]['lon']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user