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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
<?php
namespace OpenCloud\DNS\Resource;
use Guzzle\Http\Url;
use OpenCloud\Common\Constants\State;
use OpenCloud\Common\PersistentObject;
use OpenCloud\Common\Service\ServiceInterface;
class AsyncResponse extends PersistentObject
{
const DEFAULT_INTERVAL = 2;
public $jobId;
public $callbackUrl;
public $status;
public $requestUrl;
public $verb;
public $request;
public $response;
public $error;
public $domains;
protected static $json_name = false;
public function __construct(ServiceInterface $service, $object = null)
{
if (!$object) {
return;
}
parent::__construct($service, $object);
}
public function getUrl($path = null, array $query = array())
{
return Url::factory($this->callbackUrl)
->setQuery(array('showDetails' => 'True'));
}
public function name()
{
return $this->jobId;
}
public function create($params = array())
{
return $this->noCreate();
}
public function update($params = array())
{
return $this->noUpdate();
}
public function delete()
{
return $this->noDelete();
}
public function primaryKeyField()
{
return 'jobId';
}
public function waitFor($state = null, $timeout = null, $callback = null, $interval = null)
{
$state = $state ?: 'COMPLETED';
$timeout = $timeout ?: State::DEFAULT_TIMEOUT;
$interval = $interval ?: self::DEFAULT_INTERVAL;
$jobUrl = Url::factory($this->callbackUrl);
$jobUrl->setQuery(array('showDetails' => 'true'));
$continue = true;
$startTime = time();
$states = array('ERROR', $state);
while ($continue) {
$body = $this->getClient()->get($jobUrl)->send()->json();
if ($callback) {
call_user_func($callback, $body);
}
if (in_array($body['status'], $states) || (time() - $startTime) > $timeout) {
$continue = false;
}
sleep($interval);
}
return isset($body['response']) ? $body['response'] : false;
}
}