From 49167ce662389ea2e231106aa66e2e18b5bd8573 Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Tue, 17 Nov 2020 20:44:57 -0600 Subject: [PATCH] beginning work on using object responses --- src/ACL/ACLClient.php | 28 ++--- src/AbstractClient.php | 136 ++++++++++++---------- src/Agent/AgentClient.php | 48 ++++---- src/Catalog/CatalogClient.php | 22 ++-- src/Coordinate/CoordinateClient.php | 6 +- src/DecodedBody.php | 99 ++++++++++++++++ src/Event/EventClient.php | 8 +- src/Health/HealthClient.php | 16 +-- src/KV/KVClient.php | 30 ++--- src/Operator/OperatorClient.php | 46 ++++---- src/PreparedQuery/PreparedQueryClient.php | 20 ++-- src/QueryMeta.php | 59 ++++++++-- src/RequestResponse.php | 116 ++++++++++++++++++ src/Session/SessionClient.php | 24 ++-- src/Status/StatusClient.php | 4 +- src/WriteMeta.php | 14 ++- 16 files changed, 476 insertions(+), 200 deletions(-) create mode 100644 src/DecodedBody.php create mode 100644 src/RequestResponse.php diff --git a/src/ACL/ACLClient.php b/src/ACL/ACLClient.php index 14f78a2..0b0c142 100644 --- a/src/ACL/ACLClient.php +++ b/src/ACL/ACLClient.php @@ -41,12 +41,12 @@ public function bootstrap(): array $r = new Request('PUT', 'v1/acl/bootstrap', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', null, $err]; } @@ -71,14 +71,14 @@ public function create(ACLEntry $acl, WriteOptions $options = null): array $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } $wm = $this->buildWriteMeta($duration); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', $wm, $err]; } @@ -99,7 +99,7 @@ public function update(ACLEntry $acl, WriteOptions $options = null): array $r = new Request('PUT', 'v1/acl/update', $this->config, $acl); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; @@ -121,7 +121,7 @@ public function destroy(string $id, WriteOptions $options = null): array $r = new Request('PUT', sprintf('v1/acl/destroy/%s', $id), $this->config); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; @@ -145,14 +145,14 @@ public function clone(string $id, WriteOptions $options = null): array $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } $wm = $this->buildWriteMeta($duration); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', $wm, $err]; } @@ -175,14 +175,14 @@ public function info(string $id, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } @@ -209,14 +209,14 @@ public function list(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } @@ -243,14 +243,14 @@ public function replication(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } diff --git a/src/AbstractClient.php b/src/AbstractClient.php index 9d8808c..889143f 100644 --- a/src/AbstractClient.php +++ b/src/AbstractClient.php @@ -18,6 +18,7 @@ limitations under the License. */ +use DCarbone\Go\Time; use GuzzleHttp\ClientInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -27,7 +28,8 @@ * Class AbstractClient * @package DCarbone\PHPConsulAPI */ -abstract class AbstractClient { +abstract class AbstractClient +{ /** @var Config */ protected $config; @@ -35,7 +37,8 @@ abstract class AbstractClient { * AbstractConsulClient constructor. * @param Config $config */ - public function __construct(Config $config) { + public function __construct(Config $config) + { // TODO: Clone config? $this->config = clone $config; } @@ -43,30 +46,28 @@ public function __construct(Config $config) { /** * @return \DCarbone\PHPConsulAPI\Config */ - public function getConfig(): Config { + public function getConfig(): Config + { return $this->config; } /** - * @param array $r - * @return array( - * @type int query duration in microseconds - * @type ResponseInterface|null response object - * @type \DCarbone\PHPConsulAPI\Error|null error, if any - * ) + * @param \DCarbone\PHPConsulAPI\RequestResponse $r + * @return \DCarbone\PHPConsulAPI\RequestResponse */ - protected function requireOK(array $r): array { + protected function requireOK(RequestResponse $r): RequestResponse + { // If a previous error occurred, just return as-is. - if (null !== $r[2]) { + if (null !== $r->Err) { return $r; } // If we have any kind of response... - if (null !== $r[1]) { + if (null !== $r->Response) { // If this is a response... - if ($r[1] instanceof ResponseInterface) { + if ($r->Response instanceof ResponseInterface) { // Get the response code... - $code = $r[1]->getStatusCode(); + $code = $r->Response->getStatusCode(); // If 200, move right along if (200 === $code) { @@ -74,22 +75,22 @@ protected function requireOK(array $r): array { } // Otherwise, return error - return [$r[0], - $r[1], - new Error(sprintf( + $r->Err = new Error( + sprintf( '%s - Non-200 response seen. Response code: %d. Message: %s', get_class($this), $code, - $r[1]->getReasonPhrase() - ))]; - + $r->Response->getReasonPhrase() + ) + ); } else { - return [$r[0], - $r[1], - new Error(sprintf( + $r->Err = new Error( + sprintf( '%s - Expected response to be instance of \\Psr\\Message\\ResponseInterface, %s seen.', - is_object($r[1]) ? get_class($r[1]) : gettype($r[1]) - ))]; + get_class($this), + is_object($r->Response) ? get_class($r->Response) : gettype($r->Response) + ) + ); } } @@ -97,47 +98,53 @@ protected function requireOK(array $r): array { } /** - * @param Request $r - * @return array( - * @type int duration in microseconds - * @type \Psr\Http\Message\ResponseInterface|null http response - * @type \DCarbone\PHPConsulAPI\Error|null any seen errors - * ) + * @param \DCarbone\PHPConsulAPI\Request $r + * @return \DCarbone\PHPConsulAPI\RequestResponse + * @throws \GuzzleHttp\Exception\GuzzleException */ - protected function doRequest(Request $r): array { + protected function doRequest(Request $r): RequestResponse + { $rt = microtime(true); $response = null; $err = null; try { // If we actually have a client defined... if (isset($this->config->HttpClient) && $this->config->HttpClient instanceof ClientInterface) { - $response = - $this->config->HttpClient->send($r->toPsrRequest(), $this->config->getGuzzleRequestOptions($r)); + $response = $this->config->HttpClient->send( + $r->toPsrRequest(), + $this->config->getGuzzleRequestOptions($r) + ); } // Otherwise, throw error to be caught below else { throw new \RuntimeException('Unable to execute query as no HttpClient has been defined.'); } } catch (\Exception $e) { // If there has been an exception of any kind, catch it and create Error object - $err = new Error(sprintf( - '%s - Error seen while executing "%s". Message: "%s"', - get_class($this), - $r->getUri(), - $e->getMessage() - )); + $err = new Error( + sprintf( + '%s - Error seen while executing "%s". Message: "%s"', + get_class($this), + $r->getUri(), + $e->getMessage() + ) + ); } // Calculate duration and move along whatever response and error we see (if any) - return [(int)((microtime(true) - $rt) * 1000000), $response, $err]; + return new RequestResponse(intval((microtime(true) - $rt)), $response, $err); } /** - * @param int $duration + * @param \DCarbone\Go\Time\Duration $duration * @param \Psr\Http\Message\ResponseInterface $response * @param \Psr\Http\Message\UriInterface $uri * @return \DCarbone\PHPConsulAPI\QueryMeta */ - protected function buildQueryMeta(int $duration, ResponseInterface $response, UriInterface $uri): QueryMeta { + protected function buildQueryMeta( + Time\Duration $duration, + ResponseInterface $response, + UriInterface $uri + ): QueryMeta { $qm = new QueryMeta(); $qm->RequestTime = $duration; @@ -147,15 +154,17 @@ protected function buildQueryMeta(int $duration, ResponseInterface $response, Ur $qm->LastIndex = (int)$h; } + $qm->LastContentHash = $response->getHeaderLine('X-Consul-ContentHash'); + if ('' !== ($h = $response->getHeaderLine('X-Consul-KnownLeader'))) { $qm->KnownLeader = (bool)$h; - } else if ('' !== ($h = $response->getHeaderLine('X-Consul-Knownleader'))) { + } elseif ('' !== ($h = $response->getHeaderLine('X-Consul-Knownleader'))) { $qm->KnownLeader = (bool)$h; } if ('' !== ($h = $response->getHeaderLine('X-Consul-LastContact'))) { $qm->LastContact = (int)$h; - } else if ('' !== ($h = $response->getHeaderLine('X-Consul-Lastcontact'))) { + } elseif ('' !== ($h = $response->getHeaderLine('X-Consul-Lastcontact'))) { $qm->LastContact = (int)$h; } @@ -163,14 +172,19 @@ protected function buildQueryMeta(int $duration, ResponseInterface $response, Ur $qm->AddressTranslationEnabled = (bool)$h; } + if ('' !== ($h = $response->getHeaderLine('X-Cache'))) { + $qm->CacheAge = Time::Duration(intval($h, 10) * Time::Second); + } + return $qm; } /** - * @param int $duration + * @param \DCarbone\Go\Time\Duration $duration * @return \DCarbone\PHPConsulAPI\WriteMeta */ - protected function buildWriteMeta(int $duration): WriteMeta { + protected function buildWriteMeta(Time\Duration $duration): WriteMeta + { $wm = new WriteMeta(); $wm->RequestTime = $duration; @@ -178,24 +192,26 @@ protected function buildWriteMeta(int $duration): WriteMeta { } /** - * @param StreamInterface $body - * @return array( - * @type array|string|bool|int|float decoded response - * @type \DCarbone\PHPConsulAPI\Error|null error, if any - * ) + * @param \Psr\Http\Message\StreamInterface $body + * @return \DCarbone\PHPConsulAPI\DecodedBody */ - protected function decodeBody(StreamInterface $body): array { + protected function decodeBody(StreamInterface $body): DecodedBody + { $data = @json_decode((string)$body, true); if (JSON_ERROR_NONE === json_last_error()) { - return [$data, null]; + return new DecodedBody($data, null); } - return [null, - new Error(sprintf( - '%s - Unable to parse response as JSON. Message: %s', - get_class($this), - json_last_error_msg() - ))]; + return new DecodedBody( + null, + new Error( + sprintf( + '%s - Unable to parse response as JSON. Message: %s', + get_class($this), + json_last_error_msg() + ) + ) + ); } } \ No newline at end of file diff --git a/src/Agent/AgentClient.php b/src/Agent/AgentClient.php index d686398..515c1cb 100644 --- a/src/Agent/AgentClient.php +++ b/src/Agent/AgentClient.php @@ -44,14 +44,14 @@ public function Self(): array $r = new Request('GET', 'v1/agent/self', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; @@ -73,14 +73,14 @@ public function Metrics(): array $r = new Request('GET', 'v1/agent/metrics', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; @@ -108,7 +108,7 @@ public function Reload() public function NodeName(): array { if (null === $this->_self) { - list($_, $_, $err) = $this->Self(); + [$_, $_, $err] = $this->Self(); if (null !== $err) { return ['', $err]; } @@ -132,13 +132,13 @@ public function Checks(): array $r = new Request('GET', 'v1/agent/checks', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; @@ -163,13 +163,13 @@ public function Services(): array $r = new Request('GET', 'v1/agent/services', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; @@ -194,12 +194,12 @@ public function Members(): array $r = new Request('GET', 'v1/agent/members', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; } @@ -224,16 +224,16 @@ public function MemberOpts(MemberOpts $opts): array $r = new Request('GET', 'v1/agent/members', $this->config); $r->Params->set('segment', $opts->Segment); if ($opts->WAN) { - $r->Params->set('wan', 1); + $r->Params->set('wan', '1'); } /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; } @@ -334,10 +334,12 @@ public function UpdateTTL(string $checkID, string $output, string $status) return new Error("\"{$status}\" is not a valid status. Allowed: [\"pass\", \"warn\", \"fail\"]"); } - $r = new Request('PUT', + $r = new Request( + 'PUT', sprintf('v1/agent/check/update/%s', $checkID), $this->config, - new AgentCheckUpdate(['Output' => $output, 'Status' => $status])); + new AgentCheckUpdate(['Output' => $output, 'Status' => $status]) + ); return $this->requireOK($this->doRequest($r))[2]; } @@ -376,7 +378,7 @@ public function Join(string $addr, bool $wan = false) $r->Params->set('wan', '1'); } - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -389,7 +391,7 @@ public function ForceLeave(string $node) { $r = new Request('PUT', sprintf('v1/agent/force-leave/%s', $node), $this->config); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -405,7 +407,7 @@ public function EnableServiceMaintenance(string $serviceID, string $reason = '') $r->Params->set('enable', 'true'); $r->Params->set('reason', $reason); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -419,7 +421,7 @@ public function DisableServiceMaintenance(string $serviceID) $r = new Request('PUT', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->config); $r->Params->set('enable', 'false'); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -434,7 +436,7 @@ public function EnableNodeMaintenance(string $reason = '') $r->Params->set('enable', 'true'); $r->Params->set('reason', $reason); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -447,7 +449,7 @@ public function DisableNodeMaintenance() $r = new Request('PUT', 'v1/agent/maintenance', $this->config); $r->Params->set('enable', 'false'); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -459,7 +461,7 @@ public function Leave() { $r = new Request('PUT', 'v1/agent/leave', $this->config); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } diff --git a/src/Catalog/CatalogClient.php b/src/Catalog/CatalogClient.php index 2aa3312..e371dc7 100644 --- a/src/Catalog/CatalogClient.php +++ b/src/Catalog/CatalogClient.php @@ -42,7 +42,7 @@ public function Register(CatalogRegistration $catalogRegistration, WriteOptions $r = new Request('PUT', 'v1/catalog/register', $this->config, $catalogRegistration); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -63,7 +63,7 @@ public function Deregister(CatalogDeregistration $catalogDeregistration, WriteOp $r = new Request('PUT', 'v1/catalog/deregister', $this->config, $catalogDeregistration); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -82,7 +82,7 @@ public function Datacenters(): array $r = new Request('GET', 'v1/catalog/datacenters', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -104,12 +104,12 @@ public function Nodes(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -136,12 +136,12 @@ public function Services(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -168,12 +168,12 @@ public function Service(string $service, string $tag = '', QueryOptions $options } /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -201,12 +201,12 @@ public function Node(string $node, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } diff --git a/src/Coordinate/CoordinateClient.php b/src/Coordinate/CoordinateClient.php index c98087b..acbe878 100644 --- a/src/Coordinate/CoordinateClient.php +++ b/src/Coordinate/CoordinateClient.php @@ -39,13 +39,13 @@ public function Datacenters(): array $r = new Request('GET', 'v1/coordinate/datacenters', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; @@ -80,7 +80,7 @@ public function Nodes(QueryOptions $options = null): array $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } diff --git a/src/DecodedBody.php b/src/DecodedBody.php new file mode 100644 index 0000000..7e54a7c --- /dev/null +++ b/src/DecodedBody.php @@ -0,0 +1,99 @@ +Decoded = $decoded; + $this->Err = $err; + } + + /** + * @return mixed + */ + public function getDecoded() + { + return $this->Decoded; + } + + /** + * @return \DCarbone\PHPConsulAPI\Error + */ + public function getErr(): \DCarbone\PHPConsulAPI\Error + { + return $this->Err; + } + + /** + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) + { + return is_int($offset) && 0 <= $offset && $offset < 2; + } + + /** + * @param mixed $offset + * @return \DCarbone\PHPConsulAPI\Error|mixed|null + */ + public function offsetGet($offset) + { + if (0 === $offset) { + return $this->Decoded; + } elseif (1 === $offset) { + return $this->Err; + } else { + throw new \OutOfBoundsException(sprintf('Offset %s does not exist', var_export($offset, true))); + } + } + + /** + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + throw new \BadMethodCallException(sprintf('Cannot call method %s on class %s', __METHOD__, __CLASS__)); + } + + /** + * @param mixed $offset + */ + public function offsetUnset($offset) + { + throw new \BadMethodCallException(sprintf('Cannot call method %s on class %s', __METHOD__, __CLASS__)); + } +} \ No newline at end of file diff --git a/src/Event/EventClient.php b/src/Event/EventClient.php index f50339e..2ca06cf 100644 --- a/src/Event/EventClient.php +++ b/src/Event/EventClient.php @@ -59,14 +59,14 @@ public function Fire(UserEvent $event, WriteOptions $options = null): array } /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $wm = $this->buildWriteMeta($duration); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if ($err !== null) { return [null, $wm, $err]; } @@ -92,14 +92,14 @@ public function List(string $name = '', QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; diff --git a/src/Health/HealthClient.php b/src/Health/HealthClient.php index 85f506c..58245fd 100644 --- a/src/Health/HealthClient.php +++ b/src/Health/HealthClient.php @@ -44,12 +44,12 @@ public function Node(string $node, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -74,12 +74,12 @@ public function Checks(string $service, QueryOptions $options = null): array $r = new Request('GET', sprintf('v1/health/checks/%s', $service), $this->config); $r->setQueryOptions($options); - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -115,14 +115,14 @@ public function Service(string $service, } /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; @@ -164,12 +164,12 @@ public function State(string $state, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } diff --git a/src/KV/KVClient.php b/src/KV/KVClient.php index 44d662d..9a27f19 100644 --- a/src/KV/KVClient.php +++ b/src/KV/KVClient.php @@ -45,7 +45,7 @@ public function Get(string $key, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->doRequest($r); + [$duration, $response, $err] = $this->doRequest($r); if (null !== $err) { return [null, null, $err]; } @@ -53,7 +53,7 @@ public function Get(string $key, QueryOptions $options = null): array $code = $response->getStatusCode(); if (200 === $code) { - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; @@ -88,7 +88,7 @@ public function Put(KVPair $p, WriteOptions $options = null): array $r->Params->set('flags', (string)$p->Flags); } - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -133,12 +133,12 @@ public function List(string $prefix = '', QueryOptions $options = null): array $r->Params->set('recurse', ''); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -164,12 +164,12 @@ public function Keys(string $prefix = '', QueryOptions $options = null): array $r->Params->set('keys', ''); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -196,7 +196,7 @@ public function CAS(KVPair $p, WriteOptions $options = null): array } /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -221,7 +221,7 @@ public function Acquire(KVPair $p, WriteOptions $options = null): array $r->Params->set('flags', (string)$p->Flags); } - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -245,7 +245,7 @@ public function DeleteCAS(KVPair $p, WriteOptions $options = null): array $r->Params['cas'] = (string)$p->ModifyIndex; /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } @@ -270,7 +270,7 @@ public function Release(KVPair $p, WriteOptions $options = null): array $r->Params->set('flags', (string)$p->Flags); } - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -292,7 +292,7 @@ public function DeleteTree(string $prefix, QueryOptions $options = null): array $r->Params['recurse'] = ''; $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -321,7 +321,7 @@ public function Txn(KVTxnOps $txn, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->doRequest($r); + [$duration, $response, $err] = $this->doRequest($r); if (null !== $err) { return [false, null, null, $err]; } @@ -330,7 +330,7 @@ public function Txn(KVTxnOps $txn, QueryOptions $options = null): array $code = $response->getStatusCode(); if (200 === $code || 409 === $code) { - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [false, null, null, $err]; } @@ -359,7 +359,7 @@ public function Txn(KVTxnOps $txn, QueryOptions $options = null): array */ public function Tree(string $prefix = '', QueryOptions $options = null): array { - list($valueList, $_, $err) = $this->List($prefix, $options); + [$valueList, $_, $err] = $this->List($prefix, $options); if (null !== $err) { return [null, $err]; diff --git a/src/Operator/OperatorClient.php b/src/Operator/OperatorClient.php index 06d3d7e..5f24d1e 100644 --- a/src/Operator/OperatorClient.php +++ b/src/Operator/OperatorClient.php @@ -44,12 +44,12 @@ public function AreaCreate(Area $area, WriteOptions $options = null): array $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', null, $err]; } @@ -73,12 +73,12 @@ public function AreaUpdate(string $areaID, Area $area, WriteOptions $options = n $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', null, $err]; } @@ -101,12 +101,12 @@ public function AreaGet(string $areaID, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -133,12 +133,12 @@ public function AreaList(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -164,7 +164,7 @@ public function AreaDelete(string $areaID, WriteOptions $options = null): array $r = new Request('DELETE', 'v1/operator/area/' . $areaID, $this->config); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -188,12 +188,12 @@ public function AreaJoin(string $areaID, array $addresses, WriteOptions $options $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -221,12 +221,12 @@ public function AreaMembers(string $areaID, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; } @@ -252,12 +252,12 @@ public function AutopilotGetConfiguration(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; } @@ -275,7 +275,7 @@ public function AutopilotSetConfiguration(AutopilotConfiguration $conf, WriteOpt $r = new Request('PUT', 'v1/operator/autopilot/configuration', $this->config, $conf); $r->setWriteOptions($options); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } @@ -291,10 +291,10 @@ public function AutopilotCASConfiguration(AutopilotConfiguration $conf, WriteOpt { $r = new Request('PUT', 'v1/operator/autopilot/configuration', $this->config, $conf); $r->setWriteOptions($options); - $r->Params->set('cas', $conf->ModifyIndex); + $r->Params->set('cas', strval($conf->ModifyIndex)); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [false, $err]; } @@ -315,12 +315,12 @@ public function AutopilotServerHealth(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $err]; } @@ -342,12 +342,12 @@ public function RaftGetConfiguration(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, null, $err]; @@ -369,7 +369,7 @@ public function RaftRemovePeerByAddress(string $address, WriteOptions $options = $r->setWriteOptions($options); $r->Params->set('address', (string)$address); - list($_, $_, $err) = $this->requireOK($this->doRequest($r)); + [$_, $_, $err] = $this->requireOK($this->doRequest($r)); return $err; } diff --git a/src/PreparedQuery/PreparedQueryClient.php b/src/PreparedQuery/PreparedQueryClient.php index c01c2c4..5f4183f 100644 --- a/src/PreparedQuery/PreparedQueryClient.php +++ b/src/PreparedQuery/PreparedQueryClient.php @@ -42,7 +42,7 @@ public function Create(PreparedQueryDefinition $query, WriteOptions $options = n $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } @@ -63,7 +63,7 @@ public function Update(PreparedQueryDefinition $query, WriteOptions $options = n $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -84,14 +84,14 @@ public function List(QueryOptions $options = null): array { $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($body, $err) = $this->decodeBody($response->getBody()); + [$body, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } @@ -117,14 +117,14 @@ public function Get(string $queryID, QueryOptions $options = null): array { $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($body, $err) = $this->decodeBody($response->getBody()); + [$body, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } @@ -149,12 +149,12 @@ public function Delete(string $queryID, WriteOptions $options = null): array { $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } - list($_, $err) = $this->decodeBody($response->getBody()); + [$_, $err] = $this->decodeBody($response->getBody()); return [$this->buildWriteMeta($duration), null]; } @@ -173,14 +173,14 @@ public function Execute(string $queryIDOrName, QueryOptions $options = null): ar $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($body, $err) = $this->decodeBody($response->getBody()); + [$body, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; } diff --git a/src/QueryMeta.php b/src/QueryMeta.php index 6b463de..d060960 100644 --- a/src/QueryMeta.php +++ b/src/QueryMeta.php @@ -18,63 +18,102 @@ limitations under the License. */ +use DCarbone\Go\Time; + /** * Class QueryMeta * @package DCarbone\PHPConsulAPI */ -class QueryMeta { +class QueryMeta +{ /** @var string */ public $RequestUrl = ''; /** @var int */ public $LastIndex = 0; + /** @var string */ + public $LastContentHash = ''; /** @var int */ public $LastContact = 0; /** @var bool */ public $KnownLeader = false; - /** @var int */ - public $RequestTime = 0; + /** @var \DCarbone\Go\Time\Duration */ + public $RequestTime = null; /** @var bool */ public $AddressTranslationEnabled = false; + /** @var bool */ + public $CacheHit = false; + /** @var \DCarbone\Go\Time\Duration|null */ + public $CacheAge = null; /** * @return string */ - public function getRequestUrl(): string { + public function getRequestUrl(): string + { return $this->RequestUrl; } /** * @return int */ - public function getLastIndex(): int { + public function getLastIndex(): int + { return $this->LastIndex; } + /** + * @return string + */ + public function getLastContentHash(): string + { + return $this->LastContentHash; + } + /** * @return int */ - public function getLastContact(): int { + public function getLastContact(): int + { return $this->LastContact; } /** * @return bool */ - public function isKnownLeader(): bool { + public function isKnownLeader(): bool + { return $this->KnownLeader; } /** - * @return int + * @return \DCarbone\Go\Time\Duration|null */ - public function getRequestTime(): int { + public function getRequestTime(): ?Time\Duration + { return $this->RequestTime; } /** * @return bool */ - public function isAddressTranslationEnabled(): bool { + public function isAddressTranslationEnabled(): bool + { return $this->AddressTranslationEnabled; } + + /** + * @return bool + */ + public function isCacheHit(): bool + { + return $this->CacheHit; + } + + /** + * @return \DCarbone\Go\Time\Duration|null + */ + public function getCacheAge(): ?Time\Duration + { + return $this->CacheAge; + } } \ No newline at end of file diff --git a/src/RequestResponse.php b/src/RequestResponse.php new file mode 100644 index 0000000..d69c9cf --- /dev/null +++ b/src/RequestResponse.php @@ -0,0 +1,116 @@ +Duration = Time::Duration($durf * Time::Second); + $this->Response = $resp; + $this->Err = $err; + } + + /** + * @return \DCarbone\Go\Time\Duration|null + */ + public function getDuration(): ?Time\Duration + { + return $this->Duration; + } + + /** + * @return \Psr\Http\Message\ResponseInterface|null + */ + public function getResponse(): ?ResponseInterface + { + return $this->Response; + } + + /** + * @return \DCarbone\PHPConsulAPI\Error|null + */ + public function getErr(): ?Error + { + return $this->Err; + } + + /** + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) + { + return is_int($offset) && $offset >= 0 && $offset < 3; + } + + /** + * @param mixed $offset + * @return \DCarbone\Go\Time\Duration|\DCarbone\PHPConsulAPI\Error|mixed|\Psr\Http\Message\ResponseInterface|null + */ + public function offsetGet($offset) + { + if (0 === $offset) { + return $this->Duration; + } elseif (1 === $offset) { + return $this->Response; + } elseif (2 === $offset) { + return $this->Err; + } else { + throw new \OutOfBoundsException(sprintf('Offset %s does not exist', var_export($offset, true))); + } + } + + /** + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + throw new \BadMethodCallException(sprintf('Cannot call method %s on class %s', __METHOD__, __CLASS__)); + } + + /** + * @param mixed $offset + */ + public function offsetUnset($offset) + { + throw new \BadMethodCallException(sprintf('Cannot call method %s on class %s', __METHOD__, __CLASS__)); + } +} \ No newline at end of file diff --git a/src/Session/SessionClient.php b/src/Session/SessionClient.php index cb6d9d6..c8a39f5 100644 --- a/src/Session/SessionClient.php +++ b/src/Session/SessionClient.php @@ -54,14 +54,14 @@ public function CreateNoChecks(SessionEntry $sessionEntry = null, WriteOptions $ $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } $wm = $this->buildWriteMeta($duration); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', $wm, $err]; } @@ -84,14 +84,14 @@ public function Create(SessionEntry $sessionEntry = null, WriteOptions $options $r->setWriteOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', null, $err]; } $wm = $this->buildWriteMeta($duration); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return ['', $wm, $err]; @@ -113,7 +113,7 @@ public function Destroy(string $id, WriteOptions $options = null): array $r = new Request('PUT', sprintf('v1/session/destroy/%s', $id), $this->config); $r->setWriteOptions($options); - list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $_, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; } @@ -160,7 +160,7 @@ public function Renew(string $id, WriteOptions $options = null): array ))]; } - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $wm, $err]; @@ -184,14 +184,14 @@ public function Info(string $id, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; @@ -219,14 +219,14 @@ public function Node(string $node, QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; @@ -249,14 +249,14 @@ public function List(QueryOptions $options = null): array $r->setQueryOptions($options); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); + [$duration, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, null, $err]; } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); - list($data, $err) = $this->decodeBody($response->getBody()); + [$data, $err] = $this->decodeBody($response->getBody()); if (null !== $err) { return [null, $qm, $err]; diff --git a/src/Status/StatusClient.php b/src/Status/StatusClient.php index 7e6dfcd..4a0d863 100644 --- a/src/Status/StatusClient.php +++ b/src/Status/StatusClient.php @@ -38,7 +38,7 @@ public function Leader(): array $r = new Request('GET', 'v1/status/leader', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return ['', $err]; @@ -55,7 +55,7 @@ public function Peers() $r = new Request('GET', 'v1/status/peers', $this->config); /** @var \Psr\Http\Message\ResponseInterface $response */ - list($_, $response, $err) = $this->requireOK($this->doRequest($r)); + [$_, $response, $err] = $this->requireOK($this->doRequest($r)); if (null !== $err) { return [null, $err]; diff --git a/src/WriteMeta.php b/src/WriteMeta.php index a3cab7e..5793c23 100644 --- a/src/WriteMeta.php +++ b/src/WriteMeta.php @@ -18,18 +18,22 @@ limitations under the License. */ +use DCarbone\Go\Time; + /** * Class WriteMeta * @package DCarbone\PHPConsulAPI */ -class WriteMeta { - /** @var int */ - public $RequestTime = 0; +class WriteMeta +{ + /** @var \DCarbone\Go\Time\Duration */ + public $RequestTime = null; /** - * @return int + * @return \DCarbone\Go\Time\Duration|null */ - public function getRequestTime(): int { + public function getRequestTime(): ?Time\Duration + { return $this->RequestTime; } } \ No newline at end of file