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