| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2016 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\GCP\Google\ApiCore; |
| 34 |
|
| 35 |
use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\LoggingTrait; |
| 36 |
use Dudlewebs\WPMCS\GCP\Google\Auth\Logging\RpcLogEvent; |
| 37 |
use Dudlewebs\WPMCS\GCP\Google\Protobuf\Internal\Message; |
| 38 |
use Dudlewebs\WPMCS\GCP\Google\Rpc\Code; |
| 39 |
use Dudlewebs\WPMCS\GCP\Grpc\ClientStreamingCall; |
| 40 |
use Dudlewebs\WPMCS\GCP\Psr\Log\LoggerInterface; |
| 41 |
/** |
| 42 |
* ClientStream is the response object from a gRPC client streaming API call. |
| 43 |
*/ |
| 44 |
class ClientStream |
| 45 |
{ |
| 46 |
use LoggingTrait; |
| 47 |
private $call; |
| 48 |
private null|LoggerInterface $logger; |
| 49 |
/** |
| 50 |
* ClientStream constructor. |
| 51 |
* |
| 52 |
* @param ClientStreamingCall $clientStreamingCall The gRPC client streaming call object |
| 53 |
* @param array $streamingDescriptor |
| 54 |
* @param null|LoggerInterface $logger A PSR-3 compliant logger. |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
// @phpstan-ignore-line |
| 58 |
ClientStreamingCall $clientStreamingCall, |
| 59 |
array $streamingDescriptor = [], |
| 60 |
null|LoggerInterface $logger = null |
| 61 |
) |
| 62 |
{ |
| 63 |
$this->call = $clientStreamingCall; |
| 64 |
$this->logger = $logger; |
| 65 |
} |
| 66 |
/** |
| 67 |
* Write request to the server. |
| 68 |
* |
| 69 |
* @param mixed $request The request to write |
| 70 |
*/ |
| 71 |
public function write($request) |
| 72 |
{ |
| 73 |
// In some cases, $request can be a string |
| 74 |
if ($this->logger && $request instanceof Message) { |
| 75 |
$requestEvent = new RpcLogEvent(); |
| 76 |
$requestEvent->payload = $request->serializeToJsonString(); |
| 77 |
$requestEvent->processId = (int) \getmypid(); |
| 78 |
$requestEvent->requestId = \crc32((string) \spl_object_id($this) . \getmypid()); |
| 79 |
$this->logRequest($requestEvent); |
| 80 |
} |
| 81 |
$this->call->write($request); |
| 82 |
} |
| 83 |
/** |
| 84 |
* Read the response from the server, completing the streaming call. |
| 85 |
* |
| 86 |
* @throws ApiException |
| 87 |
* @return mixed The response object from the server |
| 88 |
*/ |
| 89 |
public function readResponse() |
| 90 |
{ |
| 91 |
list($response, $status) = $this->call->wait(); |
| 92 |
if ($status->code == Code::OK) { |
| 93 |
if ($this->logger) { |
| 94 |
$responseEvent = new RpcLogEvent(); |
| 95 |
$responseEvent->headers = $status->metadata; |
| 96 |
$responseEvent->status = $status->code; |
| 97 |
$responseEvent->processId = (int) \getmypid(); |
| 98 |
$responseEvent->requestId = \crc32((string) \spl_object_id($this) . \getmypid()); |
| 99 |
if ($response instanceof Message) { |
| 100 |
$response->serializeToJsonString(); |
| 101 |
} |
| 102 |
$this->logResponse($responseEvent); |
| 103 |
} |
| 104 |
return $response; |
| 105 |
} else { |
| 106 |
throw ApiException::createFromStdClass($status); |
| 107 |
} |
| 108 |
} |
| 109 |
/** |
| 110 |
* Write all data in $dataArray and read the response from the server, completing the streaming |
| 111 |
* call. |
| 112 |
* |
| 113 |
* @param mixed[] $requests An iterator of request objects to write to the server |
| 114 |
* @return mixed The response object from the server |
| 115 |
*/ |
| 116 |
public function writeAllAndReadResponse(array $requests) |
| 117 |
{ |
| 118 |
foreach ($requests as $request) { |
| 119 |
$this->write($request); |
| 120 |
} |
| 121 |
return $this->readResponse(); |
| 122 |
} |
| 123 |
/** |
| 124 |
* Return the underlying gRPC call object |
| 125 |
* |
| 126 |
* @return \Grpc\ClientStreamingCall|mixed |
| 127 |
*/ |
| 128 |
public function getClientStreamingCall() |
| 129 |
{ |
| 130 |
return $this->call; |
| 131 |
} |
| 132 |
} |
| 133 |
|