      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:  * PTR records are used for reverse DNS
     24:  *
     25:  * The PtrRecord object is nearly identical with the Record object. However,
     26:  * the PtrRecord is a child of the service, and not a child of a Domain.
     27:  */
     28: class PtrRecord extends Record
     29: {
     30:     /** @var HasPtrRecordsInterface The device which this record refers to */
     31:     public $server;
     32:
     33:     protected static $json_name = false;
     34:     protected static $json_collection_name = 'records';
     35:     protected static $url_resource = 'rdns';
     36:
     37:     private $link_rel;
     38:     private $link_href;
     39:
     40:     public function __construct($service, $info = null)
     41:     {
     42:         parent::__construct($service, $info);
     43:
     44:         $this->type = 'PTR';
     45:     }
     46:
     47:     /**
     48:      * Used to internally populate this object with the appropriate type checks
     49:      *
     50:      * @param array $params
     51:      * @throws \InvalidArgumentException If no parent device set
     52:      */
     53:     protected function populateRecord(array $params = array())
     54:     {
     55:         if (!isset($params['parent'])) {
     56:             throw new \InvalidArgumentException('You must set a `parent` device');
     57:         }
     58:
     59:         $this->setDeviceParent($params['parent']);
     60:         unset($params['parent']);
     61:
     62:         parent::populate($params);
     63:     }
     64:
     65:     /**
     66:      * Set the parent device
     67:      *
     68:      * @param HasPtrRecordsInterface $parent
     69:      */
     70:     public function setDeviceParent(HasPtrRecordsInterface $parent)
     71:     {
     72:         $this->server = $parent;
     73:     }
     74:
     75:     /**
     76:      * @return HasPtrRecordsInterface
     77:      */
     78:     public function getDeviceParent()
     79:     {
     80:         return $this->server;
     81:     }
     82:
     83:     public function create($params = array())
     84:     {
     85:         $this->populateRecord($params);
     86:
     87:         $this->link_rel = $this->getDeviceParent()->getService()->getName();
     88:         $this->link_href = (string) $this->getDeviceParent()->getUrl();
     89:
     90:         return parent::create();
     91:     }
     92:
     93:     public function update($params = array())
     94:     {
     95:         $this->populateRecord($params);
     96:
     97:         $this->link_rel = $this->getDeviceParent()->getService()->getName();
     98:         $this->link_href = (string) $this->getDeviceParent()->getUrl();
     99:
    100:         return parent::update();
    101:     }
    102:
    103:     public function delete()
    104:     {
    105:         $this->link_rel = $this->getDeviceParent()->getService()->Name();
    106:         $this->link_href = (string) $this->getDeviceParent()->getUrl();
    107:
    108:         $params = array('href' => $this->link_href);
    109:         if (!empty($this->data)) {
    110:             $params['ip'] = $this->data;
    111:         }
    112:
    113:         $url = clone $this->getUrl();
    114:         $url->addPath('..')
    115:             ->normalizePath()
    116:             ->addPath($this->link_rel)
    117:             ->setQuery($params);
    118:
    119:         $response = $this->getClient()->delete($url)->send();
    120:
    121:         return new AsyncResponse($this->getService(), Formatter::decode($response));
    122:     }
    123:
    124:     protected function createJson()
    125:     {
    126:         return (object) array(
    127:             'recordsList' => parent::createJson(),
    128:             'link' => array(
    129:                 'href' => $this->link_href,
    130:                 'rel'  => $this->link_rel
    131:             )
    132:         );
    133:     }
    134:
    135:     protected function updateJson($params = array())
    136:     {
    137:         $this->populate($params);
    138:
    139:         $object = $this->createJson();
    140:         $object->recordsList->records[0]->id = $this->id;
    141:
    142:         return $object;
    143:     }
    144: }
    145: