      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 OpenCloud\Common\Http\Message\Formatter;
     21:
     22: /**
     23:  * The Domain class represents a single domain
     24:  *
     25:  * Note that the `Subdomain` class is defined in this same file because of
     26:  * mutual dependencies.
     27:  */
     28: class Domain extends AbstractResource
     29: {
     30:     public $id;
     31:     public $accountId;
     32:     public $ttl;
     33:     public $updated;
     34:     public $emailAddress;
     35:     public $created;
     36:     public $name;
     37:     public $comment;
     38:
     39:     protected static $json_name = false;
     40:     protected static $json_collection_name = 'domains';
     41:     protected static $url_resource = 'domains';
     42:
     43:     protected $createKeys = array(
     44:         'name',
     45:         'emailAddress',
     46:         'ttl',
     47:         'comment'
     48:     );
     49:
     50:     protected $updateKeys = array(
     51:         'emailAddress',
     52:         'ttl',
     53:         'comment'
     54:     );
     55:
     56:     private $records = array();
     57:     private $subdomains = array();
     58:
     59:     /**
     60:      * returns a Record object
     61:      *
     62:      * Note that this method is available at the DNS level, but only for
     63:      * PTR records.
     64:      *
     65:      * @return Record
     66:      */
     67:     public function record($info = null)
     68:     {
     69:         $resource = new Record($this->getService());
     70:         $resource->setParent($this)->populate($info);
     71:
     72:         return $resource;
     73:     }
     74:
     75:     /**
     76:      * returns a Collection of Record objects
     77:      *
     78:      * @param array $filter query-string parameters
     79:      * @return \OpenCloud\Collection
     80:      */
     81:     public function recordList($filter = array())
     82:     {
     83:         $url = $this->getUrl(Record::resourceName(), $filter);
     84:
     85:         return $this->getParent()->collection(
     86:             'OpenCloud\DNS\Resource\Record', $url, $this
     87:         );
     88:     }
     89:
     90:     /**
     91:      * returns a Subdomain object (child of current domain)
     92:      *
     93:      */
     94:     public function subdomain($info = array())
     95:     {
     96:         $resource = new Subdomain($this->getService());
     97:         $resource->setParent($this)->populate($info);
     98:
     99:         return $resource;
    100:     }
    101:
    102:     /**
    103:      * returns a Collection of subdomains
    104:      *
    105:      * The subdomains are all `DNS:Domain` objects that are children of
    106:      * the current domain.
    107:      *
    108:      * @param array $filter key/value pairs for query string parameters
    109:      *                      return \OpenCloud\Collection
    110:      */
    111:     public function subdomainList($filter = array())
    112:     {
    113:         $url = $this->getUrl(Subdomain::resourceName(), $filter);
    114:
    115:         return $this->getParent()->collection(
    116:             'OpenCloud\DNS\Resource\Subdomain', $url, $this
    117:         );
    118:     }
    119:
    120:     /**
    121:      * Adds a new record to the list (for multiple record creation)
    122:      *
    123:      * @api
    124:      * @param Record $rec the record to add
    125:      * @return integer the number of records
    126:      */
    127:     public function addRecord(Record $record)
    128:     {
    129:         $this->records[] = $record;
    130:
    131:         return count($this->records);
    132:     }
    133:
    134:     /**
    135:      * adds a new subdomain (for multiple subdomain creation)
    136:      *
    137:      * @api
    138:      * @param Subdomain $subd the subdomain to add
    139:      * @return integer the number of subdomains
    140:      */
    141:     public function addSubdomain(Subdomain $subdomain)
    142:     {
    143:         $this->subdomains[] = $subdomain;
    144:
    145:         return count($this->subdomains);
    146:     }
    147:
    148:     /**
    149:      * returns changes since a specified date/time
    150:      *
    151:      * @param string $since the date or time
    152:      * @return DNS\Changes
    153:      */
    154:     public function changes($since = null)
    155:     {
    156:         $url = clone $this->getUrl();
    157:         $url->addPath('changes');
    158:
    159:         if ($since) {
    160:             $url->setQuery(array('since' => $since));
    161:         }
    162:
    163:         $response = $this->getService()
    164:             ->getClient()
    165:             ->get($url)
    166:             ->send();
    167:
    168:         return Formatter::decode($response);
    169:     }
    170:
    171:     /**
    172:      * exports the domain
    173:      *
    174:      * @return AsyncResponse
    175:      */
    176:     public function export()
    177:     {
    178:         return $this->getService()->asyncRequest($this->getUrl('export'));
    179:     }
    180:
    181:     /**
    182:      * clones the domain to the specified target domain
    183:      *
    184:      * @param string  $newdomain the new domain to create from this domain
    185:      * @param boolean $sub       to clone subdomains as well
    186:      * @param boolean $comments  Replace occurrences of the reference domain
    187:      *                           name with the new domain name in comments
    188:      * @param boolean $email     Replace occurrences of the reference domain
    189:      *                           name with the new domain name in email addresses on the cloned
    190:      *                           (new) domain.
    191:      * @param boolean $records   Replace occurrences of the reference domain
    192:      *                           name with the new domain name in data fields (of records) on the
    193:      *                           cloned (new) domain. Does not affect NS records.
    194:      * @return AsyncResponse
    195:      */
    196:     public function cloneDomain(
    197:         $newDomainName,
    198:         $subdomains = true,
    199:         $comments = true,
    200:         $email = true,
    201:         $records = true
    202:     ) {
    203:         $url = $this->getUrl();
    204:         $url->addPath('clone');
    205:         $url->setQuery(array(
    206:             'cloneName'          => $newDomainName,
    207:             'cloneSubdomains'    => $subdomains,
    208:             'modifyComment'      => $comments,
    209:             'modifyEmailAddress' => $email,
    210:             'modifyRecordData'   => $records
    211:         ));
    212:
    213:         return $this->getService()->asyncRequest($url, 'POST');
    214:     }
    215:
    216:     /**
    217:      * handles creation of multiple records at Create()
    218:      *
    219:      * @return \stdClass
    220:      */
    221:     protected function createJson()
    222:     {
    223:         $object = parent::createJson();
    224:
    225:         // add records, if any
    226:         if (count($this->records)) {
    227:
    228:             $recordsObject = (object) array('records' => array());
    229:
    230:             foreach ($this->records as $record) {
    231:                 $recordObject = new \stdClass;
    232:                 foreach ($record->getCreateKeys() as $key) {
    233:                     if (isset($record->$key)) {
    234:                         $recordObject->$key = $record->$key;
    235:                     }
    236:                 }
    237:                 $recordsObject->records[] = $recordObject;
    238:             }
    239:             $object->domains[0]->recordsList = $recordsObject;
    240:         }
    241:
    242:         // add subdomains, if any
    243:         if (count($this->subdomains)) {
    244:
    245:             $subdomainsObject = (object) array('domains' => array());
    246:
    247:             foreach ($this->subdomains as $subdomain) {
    248:                 $subdomainObject = new \stdClass;
    249:                 foreach ($subdomain->getCreateKeys() as $key) {
    250:                     if (isset($subdomain->$key)) {
    251:                         $subdomainObject->$key = $subdomain->$key;
    252:                     }
    253:                 }
    254:                 $subdomainsObject->domains[] = $subdomainObject;
    255:             }
    256:             $object->domains[0]->subdomains = $subdomainsObject;
    257:         }
    258:
    259:         return $object;
    260:     }
    261: }
    262: