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
<?php
namespace App\Service ;
use GuzzleHttp\Client ;
use GuzzleHttp\Exception\GuzzleException ;
use GuzzleHttp\RequestOptions ;
use Psr\Http\Message\ResponseInterface ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface ;
use Symfony\Component\HttpFoundation\Request ;
class GoogleAPIService
{
private const BASE_URL = 'https://maps.googleapis.com/' ;
private const ENDPOINTS = array(
'autocomplete' => '/maps/api/place/autocomplete/json' ,
'placeDetail' => '/maps/api/place/details/json' ,
);
public function __construct (private ParameterBagInterface $parameterBag )
{
}
/**
* @param string $input
* @param string $sessionToken
* @param string|null $locale
* @return mixed
* @throws GuzzleException
*/
public function autocomplete ( string $input , string $sessionToken , ? string $locale = null ): mixed
{
if($this -> parameterBag -> get ( 'env' ) === 'dev' ) return json_encode ( $this -> placeholderAutocomplete ());
$queryParams = [
'input' => $input ,
'key' => $this -> parameterBag -> get ( 'google.maps.key' ),
'session_token' => $sessionToken ,
'language' => $locale ?? $this -> parameterBag -> get ( 'app.default_locale' )
];
$response = $this -> call ( 'GET' , self :: ENDPOINTS [ __FUNCTION__ ], queryParams : $queryParams );
return $response -> getBody ()-> getContents ();
}
/**
* @param string $place_id
* @param string $sessionToken
* @param string|null $locale
* @return mixed
* @throws GuzzleException
*/
public function placeDetail ( string $place_id , string $sessionToken , ? string $locale = null ): mixed
{
if($this -> parameterBag -> get ( 'env' ) === 'dev' ) return json_encode ( $this -> placeholderDetail ());
$queryParams = [
'place_id' => $place_id ,
'key' => $this -> parameterBag -> get ( 'google.maps.key' ),
'session_token' => $sessionToken ,
'fields' => 'address_component' ,
'language' => $locale ?? $this -> parameterBag -> get ( 'app.default_locale' )
];
$response = $this -> call ( 'GET' , self :: ENDPOINTS [ __FUNCTION__ ], queryParams : $queryParams );
return $response -> getBody ()-> getContents ();
}
/**
* @param string $method
* @param string $endpoint
* @param array|null $bodyParams
* @param array $queryParams
* @return ResponseInterface
* @throws GuzzleException
*/
private function call ( string $method , string $endpoint , array $bodyParams = null , array $queryParams = array()): ResponseInterface
{
$httpClient = new Client ([ 'base_uri' => self :: BASE_URL ]);
return $httpClient -> request ( $method , $endpoint . '?' . http_build_query ( $queryParams ), array( RequestOptions :: JSON => $bodyParams ));
}
private function placeholderDetail (){
return [
"html_attributions" => [
],
"result" => [
"address_components" => [
[
"long_name" => "Chemin des Petits Bois" ,
"short_name" => "Chemin des Petits Bois" ,
"types" => [
"route"
]
],
[
"long_name" => "Challes" ,
"short_name" => "Challes" ,
"types" => [
"locality" ,
"political"
]
],
[
"long_name" => "Sarthe" ,
"short_name" => "Sarthe" ,
"types" => [
"administrative_area_level_2" ,
"political"
]
],
[
"long_name" => "Pays de la Loire" ,
"short_name" => "Pays de la Loire" ,
"types" => [
"administrative_area_level_1" ,
"political"
]
],
[
"long_name" => "France" ,
"short_name" => "FR" ,
"types" => [
"country" ,
"political"
]
],
[
"long_name" => "72250" ,
"short_name" => "72250" ,
"types" => [
"postal_code"
]
]
],
"adr_address" => "" ,
"formatted_address" => "Chemin des Petits Bois, 72250 Challes, France" ,
"geometry" => [
"location" => [
"lat" => 47.931388 ,
"lng" => 0.413492
],
"viewport" => [
"northeast" => [
"lat" => 47.932736980292 ,
"lng" => 0.4148409802915
],
"southwest" => [
"lat" => 47.930039019709 ,
"lng" => 0.4121430197085
]
]
],
"icon" => "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png" ,
"icon_background_color" => "#7B9EB0" ,
"icon_mask_base_uri" => "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet" ,
"name" => "Chemin des Petits Bois" ,
"place_id" => "Ei1DaGVtaW4gZGVzIFBldGl0cyBCb2lzLCA3MjI1MCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ" ,
"reference" => "Ei1DaGVtaW4gZGVzIFBldGl0cyBCb2lzLCA3MjI1MCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ" ,
"types" => [
"route"
],
"url" => "https://maps.google.com/?q=Chemin+des+Petits+Bois,+72250+Challes,+France&ftid=0x47e2f21d313aadb9:0x2fb0ba2357157d13" ,
"utc_offset" => 120 ,
"vicinity" => "Challes"
],
"status" => "OK"
];
}
private function placeholderAutocomplete (){
return [
"predictions" => [
[
"description" => "20 Rue des Petits Bois, Challes, France" ,
"matched_substrings" => [
[
"length" => 2 ,
"offset" => 0
],
[
"length" => 19 ,
"offset" => 3
],
[
"length" => 7 ,
"offset" => 24
]
],
"place_id" => "ChIJK1W5N_bx4kcRm1UlNon2I-E" ,
"reference" => "ChIJK1W5N_bx4kcRm1UlNon2I-E" ,
"structured_formatting" => [
"main_text" => "20 Rue des Petits Bois" ,
"main_text_matched_substrings" => [
[
"length" => 2 ,
"offset" => 0
],
[
"length" => 19 ,
"offset" => 3
]
],
"secondary_text" => "Challes, France" ,
"secondary_text_matched_substrings" => [
[
"length" => 7 ,
"offset" => 0
]
]
],
"terms" => [
[
"offset" => 0 ,
"value" => "20"
],
[
"offset" => 3 ,
"value" => "Rue des Petits Bois"
],
[
"offset" => 24 ,
"value" => "Challes"
],
[
"offset" => 33 ,
"value" => "France"
]
],
"types" => [
"street_address" ,
"geocode"
]
],
[
"description" => "20 Chemin des Petits Bois, Challes, France" ,
"matched_substrings" => [
[
"length" => 22 ,
"offset" => 3
],
[
"length" => 7 ,
"offset" => 27
]
],
"place_id" => "EioyMCBDaGVtaW4gZGVzIFBldGl0cyBCb2lzLCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ" ,
"reference" => "EioyMCBDaGVtaW4gZGVzIFBldGl0cyBCb2lzLCBDaGFsbGVzLCBGcmFuY2UiLiosChQKEgm5rToxHfLiRxETfRVXI7qwLxIUChIJobmRCe7x4kcRAM4KHlI3DQQ" ,
"structured_formatting" => [
"main_text" => "20 Chemin des Petits Bois" ,
"main_text_matched_substrings" => [
[
"length" => 22 ,
"offset" => 3
]
],
"secondary_text" => "Challes, France" ,
"secondary_text_matched_substrings" => [
[
"length" => 7 ,
"offset" => 0
]
]
],
"terms" => [
[
"offset" => 0 ,
"value" => "20"
],
[
"offset" => 3 ,
"value" => "Chemin des Petits Bois"
],
[
"offset" => 27 ,
"value" => "Challes"
],
[
"offset" => 36 ,
"value" => "France"
]
],
"types" => [
"route" ,
"geocode"
]
]
],
"status" => "OK"
];
}
}