MAPG-43 rewrite controllers to use the new http classes

This commit is contained in:
Bence Pőcze 2020-05-28 21:05:55 +02:00
parent a33244fb56
commit 81d2c9e3fb

View File

@ -7,7 +7,6 @@ use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Util\Geo\Position; use MapGuesser\Util\Geo\Position;
use MapGuesser\View\JsonView; use MapGuesser\View\JsonView;
use MapGuesser\Interfaces\View\IView; use MapGuesser\Interfaces\View\IView;
use RestClient\Client;
class PositionController implements IController class PositionController implements IController
{ {
@ -134,17 +133,16 @@ class PositionController implements IController
private function getPanorama(Position $position): ?string private function getPanorama(Position $position): ?string
{ {
$query = [ $request = new Request('https://maps.googleapis.com/maps/api/streetview/metadata', Request::HTTP_GET);
$request->setQuery([
'key' => $_ENV['GOOGLE_MAPS_SERVER_API_KEY'], 'key' => $_ENV['GOOGLE_MAPS_SERVER_API_KEY'],
'location' => $position->getLat() . ',' . $position->getLng(), 'location' => $position->getLat() . ',' . $position->getLng(),
'source' => 'outdoor' 'source' => 'outdoor'
]; ]);
$client = new Client('https://maps.googleapis.com/maps/api/streetview'); $response = $request->send();
$request = $client->newRequest('metadata?' . http_build_query($query));
$response = $request->getResponse();
$panoData = json_decode($response->getParsedResponse(), true); $panoData = json_decode($response->getBody(), true);
if ($panoData['status'] !== 'OK') { if ($panoData['status'] !== 'OK') {
return null; return null;
@ -158,10 +156,10 @@ class PositionController implements IController
return $realPosition->calculateDistanceTo($guessPosition); return $realPosition->calculateDistanceTo($guessPosition);
} }
private function calculateScore(float $distance, float $area) private function calculateScore(float $distance, float $area): int
{ {
$goodness = 1.0 - ($distance / sqrt($area)); $goodness = 1.0 - ($distance / sqrt($area));
return round(pow(static::MAX_SCORE, $goodness)); return (int) round(pow(static::MAX_SCORE, $goodness));
} }
} }