From 9176c562b9352c68cc679a9d53466cb526207a44 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 27 Jun 2024 19:12:48 -0700 Subject: [PATCH 1/2] feat: add resultFunction --- src/OperationResponse.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 436d97834..62a60e913 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -94,6 +94,7 @@ class OperationResponse private $operationStatusDoneValue; private ?string $operationErrorCodeMethod; private ?string $operationErrorMessageMethod; + private ?Closure $resultFunction; /** * OperationResponse constructor. @@ -139,6 +140,7 @@ public function __construct(string $operationName, $operationsClient, array $opt 'getOperationRequest' => GetOperationRequest::class, 'cancelOperationRequest' => CancelOperationRequest::class, 'deleteOperationRequest' => DeleteOperationRequest::class, + 'resultFunction' => null, ]; $this->operationReturnType = $options['operationReturnType']; $this->metadataReturnType = $options['metadataReturnType']; @@ -154,6 +156,7 @@ public function __construct(string $operationName, $operationsClient, array $opt $this->getOperationRequest = $options['getOperationRequest']; $this->cancelOperationRequest = $options['cancelOperationRequest']; $this->deleteOperationRequest = $options['deleteOperationRequest']; + $this->resultFunction = $options['resultFunction']; if (isset($options['initialPollDelayMillis'])) { $this->defaultPollSettings['initialPollDelayMillis'] = $options['initialPollDelayMillis']; @@ -310,6 +313,11 @@ public function getResult() /** @var Message $response */ $response = new $operationReturnType(); $response->mergeFromString($anyResponse->getValue()); + + if ($this->resultFunction) { + return ($this->resultFunction)($response); + } + return $response; } @@ -344,6 +352,29 @@ public function getError() return null; } + public function withResultFunction(Closure $resultFunction) + { + return new OperationResponse( + $this->operationName, + $this->operationsClient, + $this->getDescriptorOptions() + [ + 'lastProtoResponse' => $this->lastProtoResponse, + 'getOperationMethod' => $this->getOperationMethod, + 'cancelOperationMethod' => $this->cancelOperationMethod, + 'deleteOperationMethod' => $this->deleteOperationMethod, + 'operationStatusMethod' => $this->operationStatusMethod, + 'operationStatusDoneValue' => $this->operationStatusDoneValue, + 'additionalOperationArguments' => $this->additionalArgs, + 'operationErrorCodeMethod' => $this->operationErrorCodeMethod, + 'operationErrorMessageMethod' => $this->operationErrorMessageMethod, + 'getOperationRequest' => $this->getOperationRequest, + 'cancelOperationRequest' => $this->cancelOperationRequest, + 'deleteOperationRequest' => $this->deleteOperationRequest, + 'resultFunction' => $resultFunction, + ] + ); + } + /** * Get an array containing the values of 'operationReturnType', 'metadataReturnType', and * the polling options `initialPollDelayMillis`, `pollDelayMultiplier`, `maxPollDelayMillis`, From 3ad7fc3187f4429d9f3522148f08f80e00852cda Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 27 Jun 2024 19:55:25 -0700 Subject: [PATCH 2/2] add closure import --- src/OperationResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OperationResponse.php b/src/OperationResponse.php index 62a60e913..c45fff5e8 100644 --- a/src/OperationResponse.php +++ b/src/OperationResponse.php @@ -32,6 +32,7 @@ namespace Google\ApiCore; +use Closure; use Google\LongRunning\Client\OperationsClient; use Google\LongRunning\OperationsClient as LegacyOperationsClient; use Google\LongRunning\CancelOperationRequest;