No Description

MnsPromise.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace AliyunMNS\Responses;
  3. use GuzzleHttp\Promise\PromiseInterface;
  4. use AliyunMNS\Responses\BaseResponse;
  5. use AliyunMNS\Exception\MnsException;
  6. use GuzzleHttp\Exception\TransferException;
  7. use Psr\Http\Message\ResponseInterface;
  8. class MnsPromise
  9. {
  10. private $response;
  11. private $promise;
  12. public function __construct(PromiseInterface &$promise, BaseResponse &$response)
  13. {
  14. $this->promise = $promise;
  15. $this->response = $response;
  16. }
  17. public function isCompleted()
  18. {
  19. return $this->promise->getState() != 'pending';
  20. }
  21. public function getResponse()
  22. {
  23. return $this->response;
  24. }
  25. public function wait()
  26. {
  27. try {
  28. $res = $this->promise->wait();
  29. if ($res instanceof ResponseInterface)
  30. {
  31. $this->response->parseResponse($res->getStatusCode(), $res->getBody());
  32. }
  33. } catch (TransferException $e) {
  34. $message = $e->getMessage();
  35. if ($e->hasResponse()) {
  36. $message = $e->getResponse()->getBody();
  37. }
  38. $this->response->parseErrorResponse($e->getCode(), $message);
  39. }
  40. return $this->response;
  41. }
  42. }
  43. ?>