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 OpenCloud\Common\Http\Message\Formatter;
21
22 23 24 25 26 27
28 class PtrRecord extends Record
29 {
30
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 49 50 51 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 67 68 69
70 public function setDeviceParent(HasPtrRecordsInterface $parent)
71 {
72 $this->server = $parent;
73 }
74
75 76 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