Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Service/GoogleAPIService.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\GuzzleException;
  5. use GuzzleHttp\RequestOptions;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class GoogleAPIService
  10. {
  11.     private const BASE_URL 'https://maps.googleapis.com/';
  12.     private const ENDPOINTS = array(
  13.         'autocomplete' => '/maps/api/place/autocomplete/json',
  14.         'placeDetail' => '/maps/api/place/details/json',
  15.     );
  16.     public function __construct(private ParameterBagInterface $parameterBag)
  17.     {
  18.     }
  19.     /**
  20.      * @param string $input
  21.      * @param string $sessionToken
  22.      * @param string|null $locale
  23.      * @return mixed
  24.      * @throws GuzzleException
  25.      */
  26.     public function autocomplete(string $inputstring $sessionToken, ?string $locale null): mixed
  27.     {
  28.         if($this->parameterBag->get('env') === 'dev') return json_encode($this->placeholderAutocomplete());
  29.         $queryParams = [
  30.             'input' => $input,
  31.             'key' => $this->parameterBag->get('google.maps.key'),
  32.             'session_token' => $sessionToken,
  33.             'language' => $locale ?? $this->parameterBag->get('app.default_locale')
  34.         ];
  35.         $response $this->call('GET'self::ENDPOINTS[__FUNCTION__], queryParams$queryParams);
  36.         return $response->getBody()->getContents();
  37.     }
  38.     /**
  39.      * @param string $place_id
  40.      * @param string $sessionToken
  41.      * @param string|null $locale
  42.      * @return mixed
  43.      * @throws GuzzleException
  44.      */
  45.     public function placeDetail(string $place_idstring $sessionToken, ?string $locale null): mixed
  46.     {
  47.         if($this->parameterBag->get('env') === 'dev') return json_encode($this->placeholderDetail());
  48.         $queryParams = [
  49.             'place_id' => $place_id,
  50.             'key' => $this->parameterBag->get('google.maps.key'),
  51.             'session_token' => $sessionToken,
  52.             'fields' => 'address_component',
  53.             'language' => $locale ?? $this->parameterBag->get('app.default_locale')
  54.         ];
  55.         $response $this->call('GET'self::ENDPOINTS[__FUNCTION__], queryParams$queryParams);
  56.         return $response->getBody()->getContents();
  57.     }
  58.     /**
  59.      * @param string $method
  60.      * @param string $endpoint
  61.      * @param array|null $bodyParams
  62.      * @param array $queryParams
  63.      * @return ResponseInterface
  64.      * @throws GuzzleException
  65.      */
  66.     private function call(string $methodstring $endpoint, array $bodyParams null, array $queryParams = array()): ResponseInterface
  67.     {
  68.         $httpClient = new Client(['base_uri' => self::BASE_URL]);
  69.         return $httpClient->request($method$endpoint '?' http_build_query($queryParams), array(RequestOptions::JSON => $bodyParams));
  70.     }
  71.     private function placeholderDetail(){
  72.         return [
  73.                 "html_attributions" => [
  74.                 ],
  75.                 "result" => [
  76.                     "address_components" => [
  77.                         [
  78.                             "long_name" => "Chemin des Petits Bois",
  79.                             "short_name" => "Chemin des Petits Bois",
  80.                             "types" => [
  81.                                 "route"
  82.                             ]
  83.                         ],
  84.                         [
  85.                             "long_name" => "Challes",
  86.                             "short_name" => "Challes",
  87.                             "types" => [
  88.                                 "locality",
  89.                                 "political"
  90.                             ]
  91.                         ],
  92.                         [
  93.                             "long_name" => "Sarthe",
  94.                             "short_name" => "Sarthe",
  95.                             "types" => [
  96.                                 "administrative_area_level_2",
  97.                                 "political"
  98.                             ]
  99.                         ],
  100.                         [
  101.                             "long_name" => "Pays de la Loire",
  102.                             "short_name" => "Pays de la Loire",
  103.                             "types" => [
  104.                                 "administrative_area_level_1",
  105.                                 "political"
  106.                             ]
  107.                         ],
  108.                         [
  109.                             "long_name" => "France",
  110.                             "short_name" => "FR",
  111.                             "types" => [
  112.                                 "country",
  113.                                 "political"
  114.                             ]
  115.                         ],
  116.                         [
  117.                             "long_name" => "72250",
  118.                             "short_name" => "72250",
  119.                             "types" => [
  120.                                 "postal_code"
  121.                             ]
  122.                         ]
  123.                     ],
  124.                     "adr_address" => "",
  125.                     "formatted_address" => "Chemin des Petits Bois, 72250 Challes, France",
  126.                     "geometry" => [
  127.                         "location" => [
  128.                             "lat" => 47.931388,
  129.                             "lng" => 0.413492
  130.                         ],
  131.                         "viewport" => [
  132.                             "northeast" => [
  133.                                 "lat" => 47.932736980292,
  134.                                 "lng" => 0.4148409802915
  135.                             ],
  136.                             "southwest" => [
  137.                                 "lat" => 47.930039019709,
  138.                                 "lng" => 0.4121430197085
  139.                             ]
  140.                         ]
  141.                     ],
  142.                     "icon" => "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png",
  143.                     "icon_background_color" => "#7B9EB0",
  144.                     "icon_mask_base_uri" => "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet",
  145.                     "name" => "Chemin des Petits Bois",
  146.                     "place_id" => "Ei1DaGVtaW4gZGVzIFBldGl0cyBCb2lzLCA3MjI1MCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ",
  147.                     "reference" => "Ei1DaGVtaW4gZGVzIFBldGl0cyBCb2lzLCA3MjI1MCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ",
  148.                     "types" => [
  149.                         "route"
  150.                     ],
  151.                     "url" => "https://maps.google.com/?q=Chemin+des+Petits+Bois,+72250+Challes,+France&ftid=0x47e2f21d313aadb9:0x2fb0ba2357157d13",
  152.                     "utc_offset" => 120,
  153.                     "vicinity" => "Challes"
  154.                 ],
  155.                 "status" => "OK"
  156.             ];
  157.     }
  158.     private function placeholderAutocomplete(){
  159.         return [
  160.                 "predictions" => [
  161.                     [
  162.                         "description" => "20 Rue des Petits Bois, Challes, France",
  163.                         "matched_substrings" => [
  164.                             [
  165.                                 "length" => 2,
  166.                                 "offset" => 0
  167.                             ],
  168.                             [
  169.                                 "length" => 19,
  170.                                 "offset" => 3
  171.                             ],
  172.                             [
  173.                                 "length" => 7,
  174.                                 "offset" => 24
  175.                             ]
  176.                         ],
  177.                         "place_id" => "ChIJK1W5N_bx4kcRm1UlNon2I-E",
  178.                         "reference" => "ChIJK1W5N_bx4kcRm1UlNon2I-E",
  179.                         "structured_formatting" => [
  180.                             "main_text" => "20 Rue des Petits Bois",
  181.                             "main_text_matched_substrings" => [
  182.                                 [
  183.                                     "length" => 2,
  184.                                     "offset" => 0
  185.                                 ],
  186.                                 [
  187.                                     "length" => 19,
  188.                                     "offset" => 3
  189.                                 ]
  190.                             ],
  191.                             "secondary_text" => "Challes, France",
  192.                             "secondary_text_matched_substrings" => [
  193.                                 [
  194.                                     "length" => 7,
  195.                                     "offset" => 0
  196.                                 ]
  197.                             ]
  198.                         ],
  199.                         "terms" => [
  200.                             [
  201.                                 "offset" => 0,
  202.                                 "value" => "20"
  203.                             ],
  204.                             [
  205.                                 "offset" => 3,
  206.                                 "value" => "Rue des Petits Bois"
  207.                             ],
  208.                             [
  209.                                 "offset" => 24,
  210.                                 "value" => "Challes"
  211.                             ],
  212.                             [
  213.                                 "offset" => 33,
  214.                                 "value" => "France"
  215.                             ]
  216.                         ],
  217.                         "types" => [
  218.                             "street_address",
  219.                             "geocode"
  220.                         ]
  221.                     ],
  222.                     [
  223.                         "description" => "20 Chemin des Petits Bois, Challes, France",
  224.                         "matched_substrings" => [
  225.                             [
  226.                                 "length" => 22,
  227.                                 "offset" => 3
  228.                             ],
  229.                             [
  230.                                 "length" => 7,
  231.                                 "offset" => 27
  232.                             ]
  233.                         ],
  234.                         "place_id" => "EioyMCBDaGVtaW4gZGVzIFBldGl0cyBCb2lzLCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ",
  235.                         "reference" => "EioyMCBDaGVtaW4gZGVzIFBldGl0cyBCb2lzLCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ",
  236.                         "structured_formatting" => [
  237.                             "main_text" => "20 Chemin des Petits Bois",
  238.                             "main_text_matched_substrings" => [
  239.                                 [
  240.                                     "length" => 22,
  241.                                     "offset" => 3
  242.                                 ]
  243.                             ],
  244.                             "secondary_text" => "Challes, France",
  245.                             "secondary_text_matched_substrings" => [
  246.                                 [
  247.                                     "length" => 7,
  248.                                     "offset" => 0
  249.                                 ]
  250.                             ]
  251.                         ],
  252.                         "terms" => [
  253.                             [
  254.                                 "offset" => 0,
  255.                                 "value" => "20"
  256.                             ],
  257.                             [
  258.                                 "offset" => 3,
  259.                                 "value" => "Chemin des Petits Bois"
  260.                             ],
  261.                             [
  262.                                 "offset" => 27,
  263.                                 "value" => "Challes"
  264.                             ],
  265.                             [
  266.                                 "offset" => 36,
  267.                                 "value" => "France"
  268.                             ]
  269.                         ],
  270.                         "types" => [
  271.                             "route",
  272.                             "geocode"
  273.                         ]
  274.                     ]
  275.                 ],
  276.                 "status" => "OK"
  277.             ];
  278.     }
  279. }