PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / google / gax / src / BidiStream.php

BidiStream.php in Media Cloud Sync 1.2.11, at includes/sdk/google/google/gax/src/BidiStream.php

169 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Google\ApiCore;
34
35 use Dudlewebs\WPMCS\Google\Rpc\Code;
36 use Dudlewebs\WPMCS\Grpc\BidiStreamingCall;
37 /**
38 * BidiStream is the response object from a gRPC bidirectional streaming API call.
39 */
40 class BidiStream
41 {
42 private $call;
43 private $isComplete = \false;
44 private $writesClosed = \false;
45 private $resourcesGetMethod = null;
46 private $pendingResources = [];
47 /**
48 * BidiStream constructor.
49 *
50 * @param BidiStreamingCall $bidiStreamingCall The gRPC bidirectional streaming call object
51 * @param array $streamingDescriptor
52 */
53 public function __construct(BidiStreamingCall $bidiStreamingCall, array $streamingDescriptor = [])
54 {
55 $this->call = $bidiStreamingCall;
56 if (array_key_exists('resourcesGetMethod', $streamingDescriptor)) {
57 $this->resourcesGetMethod = $streamingDescriptor['resourcesGetMethod'];
58 }
59 }
60 /**
61 * Write request to the server.
62 *
63 * @param mixed $request The request to write
64 * @throws ValidationException
65 */
66 public function write($request)
67 {
68 if ($this->isComplete) {
69 throw new ValidationException("Cannot call write() after streaming call is complete.");
70 }
71 if ($this->writesClosed) {
72 throw new ValidationException("Cannot call write() after calling closeWrite().");
73 }
74 $this->call->write($request);
75 }
76 /**
77 * Write all requests in $requests.
78 *
79 * @param iterable $requests An Iterable of request objects to write to the server
80 *
81 * @throws ValidationException
82 */
83 public function writeAll($requests = [])
84 {
85 foreach ($requests as $request) {
86 $this->write($request);
87 }
88 }
89 /**
90 * Inform the server that no more requests will be written. The write() function cannot be
91 * called after closeWrite() is called.
92 * @throws ValidationException
93 */
94 public function closeWrite()
95 {
96 if ($this->isComplete) {
97 throw new ValidationException("Cannot call closeWrite() after streaming call is complete.");
98 }
99 if (!$this->writesClosed) {
100 $this->call->writesDone();
101 $this->writesClosed = \true;
102 }
103 }
104 /**
105 * Read the next response from the server. Returns null if the streaming call completed
106 * successfully. Throws an ApiException if the streaming call failed.
107 *
108 * @throws ValidationException
109 * @throws ApiException
110 * @return mixed
111 */
112 public function read()
113 {
114 if ($this->isComplete) {
115 throw new ValidationException("Cannot call read() after streaming call is complete.");
116 }
117 $resourcesGetMethod = $this->resourcesGetMethod;
118 if (!is_null($resourcesGetMethod)) {
119 if (count($this->pendingResources) === 0) {
120 $response = $this->call->read();
121 if (!is_null($response)) {
122 $pendingResources = [];
123 foreach ($response->{$resourcesGetMethod}() as $resource) {
124 $pendingResources[] = $resource;
125 }
126 $this->pendingResources = array_reverse($pendingResources);
127 }
128 }
129 $result = array_pop($this->pendingResources);
130 } else {
131 $result = $this->call->read();
132 }
133 if (is_null($result)) {
134 $status = $this->call->getStatus();
135 $this->isComplete = \true;
136 if (!($status->code == Code::OK)) {
137 throw ApiException::createFromStdClass($status);
138 }
139 }
140 return $result;
141 }
142 /**
143 * Call closeWrite(), and read all responses from the server, until the streaming call is
144 * completed. Throws an ApiException if the streaming call failed.
145 *
146 * @throws ValidationException
147 * @throws ApiException
148 * @return \Generator|mixed[]
149 */
150 public function closeWriteAndReadAll()
151 {
152 $this->closeWrite();
153 $response = $this->read();
154 while (!is_null($response)) {
155 yield $response;
156 $response = $this->read();
157 }
158 }
159 /**
160 * Return the underlying gRPC call object
161 *
162 * @return \Grpc\BidiStreamingCall|mixed
163 */
164 public function getBidiStreamingCall()
165 {
166 return $this->call;
167 }
168 }
169