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