Skip to content

Commit

Permalink
Support extending formats
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Sep 12, 2018
1 parent d7d8204 commit 28282a1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 68 deletions.
14 changes: 9 additions & 5 deletions src/Abstracts/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function _buildEndpointUrl(EndpointInterface $endpointInterface)
*/
public function processRequest(ApiRequestInterface $request)
{
return $this->processPreparedRequest($this->prepareRequest($request));
return $this->processPreparedRequest($this->prepareRequest($request), $request);
}

/**
Expand All @@ -79,12 +79,13 @@ public function prepareRequest(ApiRequestInterface $request)
}

/**
* @param PromiseInterface $apiRequest
* @param PromiseInterface $apiRequest
*
* @param ApiRequestInterface|null $rawRequest
*
* @return \Packaged\Api\Interfaces\ApiResponseInterface
* @throws \Packaged\Api\Exceptions\InvalidApiResponseException
*/
public function processPreparedRequest(PromiseInterface $apiRequest)
public function processPreparedRequest(PromiseInterface $apiRequest, ApiRequestInterface $rawRequest = null)
{
$time = microtime(true);
try
Expand All @@ -97,9 +98,12 @@ public function processPreparedRequest(PromiseInterface $apiRequest)
}

$response = $this->_processResponse($response);

$totalTime = microtime(true) - $time;
return $this->_format($response, $totalTime, $rawRequest);
}

protected function _format($response, $totalTime = 0, ApiRequestInterface $rawRequest = null)
{
$format = new JsonFormat();
return $format->decode($response, number_format($totalTime * 1000, 3));
}
Expand Down
55 changes: 27 additions & 28 deletions src/Abstracts/AbstractApiFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ protected function _getDecodeContext()
return [];
}

public function encode(
$result, $statusCode = 200, $statusMessage = '', $type = null
)
public function encode($result, $statusCode = 200, $statusMessage = '', $type = null)
{
$output = new \stdClass();
$output->status = new \stdClass();
Expand All @@ -57,11 +55,7 @@ public function encode(

$output->result = $result;

return $this->_getEncoder()->encode(
$output,
self::FORMAT,
$this->_getEncodeContext()
);
return $this->_getEncoder()->encode($output, self::FORMAT, $this->_getEncodeContext());
}

public function decode(ResponseInterface $raw, $totalTime = 0)
Expand All @@ -82,11 +76,7 @@ public function decode(ResponseInterface $raw, $totalTime = 0)
try
{
$body = (string)$raw->getBody();
$result = $this->_getDecoder()->decode(
$body,
self::FORMAT,
$this->_getDecodeContext()
);
$result = $this->_getDecoder()->decode($body, self::FORMAT, $this->_getDecodeContext());
}
catch(\Exception $e)
{
Expand All @@ -95,22 +85,10 @@ public function decode(ResponseInterface $raw, $totalTime = 0)
$body = ' (' . $body . ')';
}
error_log("Invalid API Response: " . $body);
throw new InvalidApiResponseException(
"Unable to decode raw api response.", 500, $e
);
throw new InvalidApiResponseException("Unable to decode raw api response.", 500, $e);
}

if(
!property_exists($result, 'type')
|| !property_exists($result, 'status')
|| !property_exists($result, 'result')
|| !property_exists($result->status, 'message')
|| !property_exists($result->status, 'code')
)
{
error_log("Invalid API Result: " . json_encode($result));
throw new InvalidApiResponseException("Invalid api result", 500);
}
$this->_validateResult($result);

if($executionTime === 0)
{
Expand All @@ -122,7 +100,7 @@ public function decode(ResponseInterface $raw, $totalTime = 0)
$callTime = $executionTime;
}

return ResponseBuilder::create(
return $this->_buildFromCallData(
ApiCallData::create(
$result->type,
$result->result,
Expand All @@ -134,4 +112,25 @@ public function decode(ResponseInterface $raw, $totalTime = 0)
)
);
}

protected function _validateResult($result)
{
if(
!property_exists($result, 'type')
|| !property_exists($result, 'status')
|| !property_exists($result, 'result')
|| !property_exists($result->status, 'message')
|| !property_exists($result->status, 'code')
)
{
error_log("Invalid API Result: " . json_encode($result));
throw new InvalidApiResponseException("Invalid api result", 500);
}
return true;
}

protected function _buildFromCallData(ApiCallData $callData)
{
return ResponseBuilder::create($callData);
}
}
3 changes: 1 addition & 2 deletions src/Abstracts/AbstractApiPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Packaged\Helpers\Arrays;
use Packaged\Helpers\Objects;

abstract class AbstractApiPayload extends AbstractDefinable
implements ApiPayloadInterface
abstract class AbstractApiPayload extends AbstractDefinable implements ApiPayloadInterface
{
/**
* Retrieve the request data as an array
Expand Down
11 changes: 2 additions & 9 deletions src/Abstracts/AbstractApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ public static function create(ApiRequestInterface $request)

protected function _getProperty($property, $default = null)
{
return Objects::property(
$this->_apiCallData->getRawResult(),
strtolower($property),
$default
);
return Objects::property($this->_apiCallData->getRawResult(), strtolower($property), $default);
}

/**
Expand Down Expand Up @@ -111,9 +107,6 @@ public function __call($method, $params)
{
return $this->_getProperty(substr($method, 3), Arrays::first($params));
}
else
{
throw new \Exception("Method $method is not supported");
}
throw new \Exception("Method $method is not supported");
}
}
8 changes: 2 additions & 6 deletions src/Response/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public static function create(ApiCallData $data)

if(!class_exists($type))
{
throw new \Exception(
"Type Class '" . $type . "', could not be loaded"
);
throw new \Exception("Type Class '" . $type . "', could not be loaded");
}

$interfaces = class_implements($type);
Expand Down Expand Up @@ -56,9 +54,7 @@ public static function create(ApiCallData $data)
}
else
{
throw new ApiException(
"An invalid message type was used '" . $type . "'"
);
throw new ApiException("An invalid message type was used '" . $type . "'");
}
}
}
4 changes: 1 addition & 3 deletions src/Traits/ApiAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public function getApi()
{
if($this->_api === null)
{
throw new \RuntimeException(
"No API has been bound to " . get_called_class()
);
throw new \RuntimeException("No API has been bound to " . get_called_class());
}
return $this->_api;
}
Expand Down
21 changes: 6 additions & 15 deletions src/Validation/PayloadValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public function validate(array $properties = null, $throw = true)
$optional = $block->hasTag('optional');
$val = $this->_payload->$property;

if(($val === null && ($nullable || $optional))
|| ($val === '' && $optional)
)
if(($val === null && ($nullable || $optional)) || ($val === '' && $optional))
{
continue;
}
Expand All @@ -67,15 +65,12 @@ public function validate(array $properties = null, $throw = true)
{
throw $e;
}
else
$allValid = false;
if(!isset($this->_errors[$property]))
{
$allValid = false;
if(!isset($this->_errors[$property]))
{
$this->_errors[$property] = [];
}
$this->_errors[$property][] = $e->getMessage();
$this->_errors[$property] = [];
}
$this->_errors[$property][] = $e->getMessage();
}
}
}
Expand Down Expand Up @@ -183,11 +178,7 @@ public function runValidator($tag, $property, $value, $options)
}
break;
case 'timestamp':
if(!((string)
(int)$value === (string)$value
&& ($value <= PHP_INT_MAX)
&& ($value >= ~PHP_INT_MAX))
)
if(!((string)(int)$value === (string)$value && ($value <= PHP_INT_MAX) && ($value >= ~PHP_INT_MAX)))
{
$msg = 'is not a valid timestamp';
}
Expand Down

0 comments on commit 28282a1

Please sign in to comment.