diff --git a/src/Abstracts/AbstractApi.php b/src/Abstracts/AbstractApi.php index 13bf1a9..bcfed9a 100644 --- a/src/Abstracts/AbstractApi.php +++ b/src/Abstracts/AbstractApi.php @@ -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); } /** @@ -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 @@ -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)); } diff --git a/src/Abstracts/AbstractApiFormat.php b/src/Abstracts/AbstractApiFormat.php index f732697..ea83ded 100644 --- a/src/Abstracts/AbstractApiFormat.php +++ b/src/Abstracts/AbstractApiFormat.php @@ -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(); @@ -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) @@ -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) { @@ -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) { @@ -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, @@ -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); + } } diff --git a/src/Abstracts/AbstractApiPayload.php b/src/Abstracts/AbstractApiPayload.php index 76cd310..02cb3cb 100644 --- a/src/Abstracts/AbstractApiPayload.php +++ b/src/Abstracts/AbstractApiPayload.php @@ -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 diff --git a/src/Abstracts/AbstractApiResponse.php b/src/Abstracts/AbstractApiResponse.php index 37e3b0e..47fcf67 100644 --- a/src/Abstracts/AbstractApiResponse.php +++ b/src/Abstracts/AbstractApiResponse.php @@ -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); } /** @@ -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"); } } diff --git a/src/Response/ResponseBuilder.php b/src/Response/ResponseBuilder.php index a22c4d7..02d830d 100644 --- a/src/Response/ResponseBuilder.php +++ b/src/Response/ResponseBuilder.php @@ -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); @@ -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 . "'"); } } } diff --git a/src/Traits/ApiAwareTrait.php b/src/Traits/ApiAwareTrait.php index 124a296..db59ad7 100644 --- a/src/Traits/ApiAwareTrait.php +++ b/src/Traits/ApiAwareTrait.php @@ -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; } diff --git a/src/Validation/PayloadValidator.php b/src/Validation/PayloadValidator.php index cb6864f..31708e3 100644 --- a/src/Validation/PayloadValidator.php +++ b/src/Validation/PayloadValidator.php @@ -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; } @@ -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(); } } } @@ -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'; }