ServerStream.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * Copyright 2016 Google LLC
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. namespace Google\ApiCore;
  33. use Google\Rpc\Code;
  34. /**
  35. * ServerStream is the response object from a server streaming API call.
  36. */
  37. class ServerStream
  38. {
  39. private $call;
  40. private $resourcesGetMethod;
  41. /**
  42. * ServerStream constructor.
  43. *
  44. * @param ServerStreamingCallInterface $serverStreamingCall The server streaming call object
  45. * @param array $streamingDescriptor
  46. */
  47. public function __construct($serverStreamingCall, array $streamingDescriptor = [])
  48. {
  49. $this->call = $serverStreamingCall;
  50. if (array_key_exists('resourcesGetMethod', $streamingDescriptor)) {
  51. $this->resourcesGetMethod = $streamingDescriptor['resourcesGetMethod'];
  52. }
  53. }
  54. /**
  55. * A generator which yields results from the server until the streaming call
  56. * completes. Throws an ApiException if the streaming call failed.
  57. *
  58. * @throws ApiException
  59. * @return \Generator|mixed
  60. */
  61. public function readAll()
  62. {
  63. $resourcesGetMethod = $this->resourcesGetMethod;
  64. if (!is_null($resourcesGetMethod)) {
  65. foreach ($this->call->responses() as $response) {
  66. foreach ($response->$resourcesGetMethod() as $resource) {
  67. yield $resource;
  68. }
  69. }
  70. } else {
  71. foreach ($this->call->responses() as $response) {
  72. yield $response;
  73. }
  74. }
  75. // Errors in the REST transport will be thrown from there and not reach
  76. // this handling. Successful REST server-streams will have an OK status.
  77. $status = $this->call->getStatus();
  78. if ($status->code !== Code::OK) {
  79. throw ApiException::createFromStdClass($status);
  80. }
  81. }
  82. /**
  83. * Return the underlying call object.
  84. *
  85. * @return ServerStreamingCallInterface
  86. */
  87. public function getServerStreamingCall()
  88. {
  89. return $this->call;
  90. }
  91. }