      1: <?php
      2: /**
      3:  * Copyright 2012-2014 Rackspace US, Inc.
      4:  *
      5:  * Licensed under the Apache License, Version 2.0 (the "License");
      6:  * you may not use this file except in compliance with the License.
      7:  * You may obtain a copy of the License at
      8:  *
      9:  * http://www.apache.org/licenses/LICENSE-2.0
     10:  *
     11:  * Unless required by applicable law or agreed to in writing, software
     12:  * distributed under the License is distributed on an "AS IS" BASIS,
     13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14:  * See the License for the specific language governing permissions and
     15:  * limitations under the License.
     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:  * The AsyncResponse class encapsulates the data returned by a Cloud DNS
     27:  * asynchronous response.
     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:      * constructs a new AsyncResponse object from a JSON
     47:      * string
     48:      *
     49:      * @param \OpenCloud\Service $service the calling service
     50:      * @param string             $json    the json response from the initial request
     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:      * URL for status
     63:      *
     64:      * We always show details
     65:      *
     66:      * @return string
     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:      * returns the Name of the request (the job ID)
     76:      *
     77:      * @return string
     78:      */
     79:     public function name()
     80:     {
     81:         return $this->jobId;
     82:     }
     83:
     84:     /**
     85:      * overrides for methods
     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:
    122:             $body = $this->getClient()->get($jobUrl)->send()->json();
    123:
    124:             if ($callback) {
    125:                 call_user_func($callback, $body);
    126:             }
    127:
    128:             if (in_array($body['status'], $states) || (time() - $startTime) > $timeout) {
    129:                 $continue = false;
    130:             }
    131:
    132:             sleep($interval);
    133:         }
    134:
    135:         return isset($body['response']) ? $body['response'] : false;
    136:     }
    137: }
    138: