1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17
18 namespace OpenCloud\DNS\Resource;
19
20 use Guzzle\Http\Url;
21 use OpenCloud\Common\Constants\State;
22 use OpenCloud\Common\PersistentObject;
23 use OpenCloud\Common\Service\ServiceInterface;
24
25 26 27 28
29 class AsyncResponse extends PersistentObject
30 {
31 const DEFAULT_INTERVAL = 2;
32
33 public $jobId;
34 public $callbackUrl;
35 public $status;
36 public $requestUrl;
37 public $verb;
38 public $request;
39 public $response;
40 public $error;
41 public $domains;
42
43 protected static $json_name = false;
44
45 46 47 48 49 50 51
52 public function __construct(ServiceInterface $service, $object = null)
53 {
54 if (!$object) {
55 return;
56 }
57
58 parent::__construct($service, $object);
59 }
60
61 62 63 64 65 66 67
68 public function getUrl($path = null, array $query = array())
69 {
70 return Url::factory($this->callbackUrl)
71 ->setQuery(array('showDetails' => 'True'));
72 }
73
74 75 76 77 78
79 public function name()
80 {
81 return $this->jobId;
82 }
83
84 85 86
87 public function create($params = array())
88 {
89 return $this->noCreate();
90 }
91
92 public function update($params = array())
93 {
94 return $this->noUpdate();
95 }
96
97 public function delete()
98 {
99 return $this->noDelete();
100 }
101
102 public function primaryKeyField()
103 {
104 return 'jobId';
105 }
106
107 public function waitFor($state = null, $timeout = null, $callback = null, $interval = null)
108 {
109 $state = $state ?: 'COMPLETED';
110 $timeout = $timeout ?: State::DEFAULT_TIMEOUT;
111 $interval = $interval ?: self::DEFAULT_INTERVAL;
112
113 $jobUrl = Url::factory($this->callbackUrl);
114 $jobUrl->setQuery(array('showDetails' => 'true'));
115
116 $continue = true;
117 $startTime = time();
118 $states = array('ERROR', $state);
119
120 while ($continue) {
121 $body = $this->getClient()->get($jobUrl)->send()->json();
122
123 if ($callback) {
124 call_user_func($callback, $body);
125 }
126
127 if (in_array($body['status'], $states) || (time() - $startTime) > $timeout) {
128 $continue = false;
129 }
130
131 sleep($interval);
132 }
133
134 return isset($body['response']) ? $body['response'] : false;
135 }
136 }
137