GpsCoordinateUtils.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use App\ApiResources\Utils\GpsCoordinate;
  5. use App\Tests\Service\Utils\GpsCoordinateUtilsTest;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. /**
  9. * Class GpsCoordinateSearching : service aidant à la manipulation de l'API OpenStreetMap
  10. * @package App\Service\Utils
  11. */
  12. class GpsCoordinateUtils
  13. {
  14. private HttpClientInterface $clientOpenStreetMap;
  15. public function __construct(
  16. HttpClientInterface $openstreetmap
  17. )
  18. {
  19. $this->clientOpenStreetMap = $openstreetmap;
  20. }
  21. /**
  22. * Renvoie un tableau d'adresse existante correspondant à la recherche d'adresse demandée
  23. * @param string|null $street
  24. * @param string|null $cp
  25. * @param string|null $city
  26. * @return array
  27. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  28. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  29. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  30. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  31. * @see GpsCoordinateUtilsTest::testSearchGpsCoordinates()
  32. */
  33. public function searchGpsCoordinates(?string $street, ?string $cp, ?string $city): array {
  34. try{
  35. $url = sprintf('search?addressdetails=1&format=json&limit=10&street=%s&postalcode=%s&city=%s', $street, $cp, $city);
  36. $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
  37. }catch(\Exception $e){
  38. throw new NotFoundHttpException('no_reverse_gps_coordinate', $e, 404);
  39. }
  40. return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
  41. }
  42. /**
  43. * Renvoi l'adresse correspondant à la latitude et longitude demandée
  44. * @param $latitude
  45. * @param $longitude
  46. * @return array
  47. * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
  48. * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
  49. * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
  50. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  51. * @see GpsCoordinateUtilsTest::testReverseGpsCoordinates()
  52. */
  53. public function reverseGpsCoordinates(float $latitude, float $longitude): array {
  54. try{
  55. $url = sprintf('reverse?addressdetails=1&format=json&lat=%s&lon=%s', $latitude, $longitude);
  56. $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
  57. }catch(\Exception $e){
  58. throw new NotFoundHttpException('no_reverse_gps_coordinate', $e, 404);
  59. }
  60. return json_decode($response, true, 512, JSON_THROW_ON_ERROR);
  61. }
  62. /**
  63. * Transforme une réponse d'API en ressource GpsCoordinate
  64. * @param array $gpsApiResponse
  65. * @return GpsCoordinate
  66. * @see GpsCoordinateUtilsTest::testCreateGpsCoordinate()
  67. */
  68. public function createGpsCoordinate(array $gpsApiResponse):GpsCoordinate{
  69. $address = $this->transformAddress($gpsApiResponse['address']);
  70. $gpsCoordinate = new GpsCoordinate();
  71. $gpsCoordinate
  72. ->setLatitude(floatval($gpsApiResponse['lat']))
  73. ->setLongitude(floatval($gpsApiResponse['lon']))
  74. ->setCity($address['city'])
  75. ->setCp($address['cp'])
  76. ->setStreetAddress($address['streetAddress'])
  77. ->setStreetAddressSecond($address['streetAddressSecond']);
  78. return $gpsCoordinate;
  79. }
  80. /**
  81. * Permet de faire correspondre le bloc adresse renvoyé par l'API avec des éléments plus communs
  82. * @param array $address
  83. * @return array
  84. */
  85. public function transformAddress(array $address): array{
  86. $addressTransformed['streetAddress'] = key_exists('road', $address) ? $address['road'] : (key_exists('hamlet', $address) ? $address['hamlet'] : null);
  87. $addressTransformed['streetAddressSecond'] = key_exists('road', $address) && key_exists('hamlet', $address) ? $address['hamlet'] : null;
  88. $addressTransformed['city'] = key_exists('town', $address) ? $address['town'] : (key_exists('village', $address) ? $address['village'] : null);
  89. $addressTransformed['cp'] = key_exists('postcode', $address) ? $address['postcode'] : null;
  90. return $addressTransformed;
  91. }
  92. }