| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Utils;
- use App\ApiResources\Utils\GpsCoordinate;
- use App\Tests\Service\Utils\GpsCoordinateUtilsTest;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- /**
- * Class GpsCoordinateSearching : service aidant à la manipulation de l'API OpenStreetMap
- * @package App\Service\Utils
- */
- class GpsCoordinateUtils
- {
- private HttpClientInterface $clientOpenStreetMap;
- public function __construct(
- HttpClientInterface $openstreetmap
- )
- {
- $this->clientOpenStreetMap = $openstreetmap;
- }
- /**
- * Renvoie un tableau d'adresse existante correspondant à la recherche d'adresse demandée
- * @param string|null $street
- * @param string|null $cp
- * @param string|null $city
- * @return array
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
- * @see GpsCoordinateUtilsTest::testSearchGpsCoordinates()
- */
- public function searchGpsCoordinates(?string $street, ?string $cp, ?string $city): array {
- try{
- $url = sprintf('search?addressdetails=1&format=json&limit=10&street=%s&postalcode=%s&city=%s', $street, $cp, $city);
- $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
- }catch(\Exception $e){
- throw new NotFoundHttpException('no_reverse_gps_coordinate', $e, 404);
- }
- return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
- }
- /**
- * Renvoi l'adresse correspondant à la latitude et longitude demandée
- * @param $latitude
- * @param $longitude
- * @return array
- * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
- * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
- * @see GpsCoordinateUtilsTest::testReverseGpsCoordinates()
- */
- public function reverseGpsCoordinates(float $latitude, float $longitude): array {
- try{
- $url = sprintf('reverse?addressdetails=1&format=json&lat=%s&lon=%s', $latitude, $longitude);
- $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
- }catch(\Exception $e){
- throw new NotFoundHttpException('no_reverse_gps_coordinate', $e, 404);
- }
- return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
- }
- /**
- * Transforme une réponse d'API en ressource GpsCoordinate
- * @param array $gpsApiResponse
- * @return GpsCoordinate
- * @see GpsCoordinateUtilsTest::testCreateGpsCoordinate()
- */
- public function createGpsCoordinate(array $gpsApiResponse):GpsCoordinate{
- $address = $this->transformAddress($gpsApiResponse['address']);
- $gpsCoordinate = new GpsCoordinate();
- $gpsCoordinate
- ->setLatitude(floatval($gpsApiResponse['lat']))
- ->setLongitude(floatval($gpsApiResponse['lon']))
- ->setCity($address['city'])
- ->setCp($address['cp'])
- ->setStreetAddress($address['streetAddress'])
- ->setStreetAddressSecond($address['streetAddressSecond']);
- return $gpsCoordinate;
- }
- /**
- * Permet de faire correspondre le bloc adresse renvoyé par l'API avec des éléments plus communs
- * @param array $address
- * @return array
- */
- public function transformAddress(array $address): array{
- $addressTransformed['streetAddress'] = key_exists('road', $address) ? $address['road'] : (key_exists('hamlet', $address) ? $address['hamlet'] : null);
- $addressTransformed['streetAddressSecond'] = key_exists('road', $address) && key_exists('hamlet', $address) ? $address['hamlet'] : null;
- $addressTransformed['city'] = key_exists('town', $address) ? $address['town'] : (key_exists('village', $address) ? $address['village'] : null);
- $addressTransformed['cp'] = key_exists('postcode', $address) ? $address['postcode'] : null;
- return $addressTransformed;
- }
- }
|