Skip to content

Commit

Permalink
PSR Code Style, unknown status code added to message
Browse files Browse the repository at this point in the history
  • Loading branch information
cirovargas committed Jul 10, 2017
1 parent 25453b3 commit 68a6932
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 34 deletions.
7 changes: 7 additions & 0 deletions src/Exception/DestinationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Exception;

class DestinationException extends Exception
{
}
7 changes: 7 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Exception;

class Exception extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Exception/OriginException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Exception;

class OriginException extends Exception
{
}
7 changes: 7 additions & 0 deletions src/Exception/ResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Valerian\GoogleDistanceMatrix\Exception;

class ResponseException extends Exception
{
}
2 changes: 1 addition & 1 deletion src/GoogleDistanceMatrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function validateResponse(GoogleDistanceMatrixResponse $response)
throw new Exception\ResponseException("Unknown error.", 5);
break;
default:
throw new Exception\ResponseException("Unknown status code.", 6);
throw new Exception\ResponseException(sprintf("Unknown status code: %s",$response->getStatus()), 6);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Response/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Valerian\GoogleDistanceMatrix\Response;


class Address
{
/**
Expand All @@ -12,6 +11,7 @@ class Address

/**
* Address constructor.
*
* @param $address string
*/
public function __construct($address)
Expand All @@ -26,4 +26,4 @@ public function __toString()
{
return $this->address;
}
}
}
5 changes: 2 additions & 3 deletions src/Response/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Valerian\GoogleDistanceMatrix\Response;


class Distance
{

private $text;

private $value;

/**
* Distance constructor.
*
* @param $text
* @param $value
*/
Expand Down Expand Up @@ -41,4 +40,4 @@ public function getValue()
{
return $this->value;
}
}
}
6 changes: 3 additions & 3 deletions src/Response/Duration.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
namespace Valerian\GoogleDistanceMatrix\Response;

namespace Valerian\GoogleDistanceMatrix\Response;

class Duration
{

private $text;

private $value;

/**
* Distance constructor.
*
* @param $text
* @param $value
*/
Expand Down Expand Up @@ -40,4 +40,4 @@ public function getValue()
{
return $this->value;
}
}
}
13 changes: 6 additions & 7 deletions src/Response/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

use Valerian\GoogleDistanceMatrix\Exception\Exception;


class Element
{
const STATUS_OK = 'OK';
const STATUS_NOT_FOUND = 'NOT_FOUND' ;
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
self::STATUS_ZERO_RESULTS,
];

private $status;
Expand All @@ -25,17 +24,17 @@ class Element

/**
* 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');
if (!in_array($status, self::STATUS)) {
throw new Exception(sprintf('Unknown status code: %s', $status));
}


$this->status = $status;
$this->duration = $duration;
$this->distance = $distance;
Expand Down Expand Up @@ -64,4 +63,4 @@ public function getDistance()
{
return $this->distance;
}
}
}
24 changes: 10 additions & 14 deletions src/Response/GoogleDistanceMatrixResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class GoogleDistanceMatrixResponse
{

const RESPONSE_STATUS_OK = 'OK';
const RESPONSE_STATUS_INVALID_REQUEST = 'INVALID_REQUEST';
const RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED = 'MAX_ELEMENTS_EXCEEDED';
Expand All @@ -18,7 +17,7 @@ class GoogleDistanceMatrixResponse
self::RESPONSE_STATUS_MAX_ELEMENTS_EXCEEDED,
self::RESPONSE_STATUS_OVER_QUERY_LIMIT,
self::RESPONSE_STATUS_REQUEST_DENIED,
self::RESPONSE_STATUS_UNKNOWN_ERROR
self::RESPONSE_STATUS_UNKNOWN_ERROR,
];

private $status;
Expand All @@ -31,7 +30,7 @@ class GoogleDistanceMatrixResponse

private $rows;

public function __construct(\stdClass $responseObject)
public function __construct(\stdClass $responseObject)
{
$this->responseObject = $responseObject;
$this->originAddresses = array();
Expand Down Expand Up @@ -95,29 +94,26 @@ public function getRows()
return $this->rows;
}


private function construct()
{
$this->status = $this->responseObject->status;

foreach ($this->responseObject->origin_addresses as $originAddress){
foreach ($this->responseObject->origin_addresses as $originAddress) {
$this->addOriginAddress(new Address($originAddress));
}

foreach ($this->responseObject->destination_addresses as $destinationAddress){
foreach ($this->responseObject->destination_addresses as $destinationAddress) {
$this->addDestinationAddress(new Address($destinationAddress));
}

foreach ($this->responseObject->rows as $row){

foreach ($this->responseObject->rows as $row) {
$elements = array();
foreach($row->elements as $element){
$duration = new Duration($element->duration->text,$element->duration->value);
$distance = new Distance($element->distance->text,$element->distance->value);
$elements[] = new Element($element->status,$duration,$distance);
foreach ($row->elements as $element) {
$duration = new Duration($element->duration->text, $element->duration->value);
$distance = new Distance($element->distance->text, $element->distance->value);
$elements[] = new Element($element->status, $duration, $distance);
}
$this->addRow(new Row($elements));
}

}
}
}
6 changes: 2 additions & 4 deletions src/Response/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
* Created by PhpStorm.
* User: ciro
* Date: 10/07/17
* Time: 17:19
* Time: 17:19.
*/

namespace Valerian\GoogleDistanceMatrix\Response;


class Row
{
/**
Expand All @@ -28,5 +27,4 @@ public function getElements()
{
return $this->elements;
}

}
}

0 comments on commit 68a6932

Please sign in to comment.