Skip to content

Commit

Permalink
Exceptions, response mapping objects
Browse files Browse the repository at this point in the history
  • Loading branch information
cirovargas committed Jul 10, 2017
1 parent 11a48a3 commit 7b10146
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 17 deletions.
8 changes: 0 additions & 8 deletions src/Exceptions.php

This file was deleted.

54 changes: 45 additions & 9 deletions src/GoogleDistanceMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Valerian\GoogleDistanceMatrix;

use GuzzleHttp\Client;
use Valerian\GoogleDistanceMatrix\Response\GoogleDistanceMatrixResponse;

class GoogleDistanceMatrix
{

/**
* @var string
*/
Expand Down Expand Up @@ -211,7 +213,7 @@ public function getAvoid()
*/
public function sendRequest()
{
$this->validate();
$this->validateRequest();
$data = [
'key' => $this->apiKey,
'language' => $this->language,
Expand All @@ -224,11 +226,9 @@ public function sendRequest()
$parameters = http_build_query($data);
$url = self::URL.'?'.$parameters;
$response = $this->request('GET', $url);
if ($response->getStatusCode() != 200) {
throw new \Exception('Response with status code '.$response->getStatusCode());
}

return json_decode(($response->getBody()->getContents()));

return $response;
}

/**
Expand All @@ -240,16 +240,52 @@ private function request($type = 'GET', $url)
{
$client = new Client();
$response = $client->request($type, $url);
return $response;

if ($response->getStatusCode() != 200) {
throw new \Exception('Response with status code '.$response->getStatusCode());
}

$responseObject = new GoogleDistanceMatrixResponse(json_decode($response->getBody()->getContents()));

$this->validateResponse($responseObject);

return $responseObject;
}

private function validateResponse(GoogleDistanceMatrixResponse $response)
{

switch ($response->getStatus()) {
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_OK:
break;
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_INVALID_REQUEST:
throw new Exception\ResponseException("Invalid request.", 1);
break;
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED:
throw new Exception\ResponseException("The product of the origin and destination exceeds the limit per request.", 2);
break;
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_OVER_QUERY_LIMIT:
throw new Exception\ResponseException("The service has received too many requests from your application in the allowed time range.", 3);
break;
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_REQUEST_DENIED:
throw new Exception\ResponseException("The service denied the use of the Distance Matrix API service by your application.", 4);
break;
case GoogleDistanceMatrixResponse::RESPONSE_STATUS_UNKNOWN_ERROR:
throw new Exception\ResponseException("Unknown error.", 5);
break;
default:
throw new Exception\ResponseException("Unknown status code.", 6);
break;
}
}

private function validate()
private function validateRequest()
{
if (empty($this->getOrigin())) {
throw new Exception('Origin must be set.');
throw new Exception\OriginException('Origin must be set.');
}
if (empty($this->getDestination())) {
throw new Exception('Destination must be set.');
throw new Exception\DestinationException('Destination must be set.');
}
}
}
29 changes: 29 additions & 0 deletions src/Response/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Response;


class Address
{
/**
* @var string
*/
private $address;

/**
* Address constructor.
* @param $address string
*/
public function __construct($address)
{
$this->address = $address;
}

/**
* @return string
*/
public function __toString()
{
return $this->address;
}
}
44 changes: 44 additions & 0 deletions src/Response/Distance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Response;


class Distance
{

private $text;

private $value;

/**
* Distance constructor.
* @param $text
* @param $value
*/
public function __construct($text, $value)
{
$this->text = $text;
$this->value = $value;
}

public function __toString()
{
return $this->text;
}

/**
* @return mixed
*/
public function getText()
{
return $this->text;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}
43 changes: 43 additions & 0 deletions src/Response/Duration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Valerian\GoogleDistanceMatrix\Response;


class Duration
{

private $text;

private $value;

/**
* Distance constructor.
* @param $text
* @param $value
*/
public function __construct($text, $value)
{
$this->text = $text;
$this->value = $value;
}

public function __toString()
{
return $this->text;
}

/**
* @return mixed
*/
public function getText()
{
return $this->text;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}
67 changes: 67 additions & 0 deletions src/Response/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Response;

use Valerian\GoogleDistanceMatrix\Exception\Exception;


class Element
{
const STATUS_OK = 'OK';
const STATUS_NOT_FOUND = 'NOT_FOUND' ;
const STATUS_ZERO_RESULTS = 'ZERO_RESULTS';

const STATUS = [
self::STATUS_OK,
self::STATUS_NOT_FOUND,
self::STATUS_ZERO_RESULTS
];

private $status;

private $duration;

private $distance;

/**
* Element constructor.
* @param $status
* @param Duration $duration
* @param Distance $distance
*/
public function __construct($status, Duration $duration, Distance $distance)
{
if(!in_array($status,self::STATUS)) {
throw new Exception('Unknown status code');
}


$this->status = $status;
$this->duration = $duration;
$this->distance = $distance;
}

/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}

/**
* @return Duration
*/
public function getDuration()
{
return $this->duration;
}

/**
* @return Distance
*/
public function getDistance()
{
return $this->distance;
}
}

0 comments on commit 7b10146

Please sign in to comment.