1: <?php
2:
3: namespace OpenCloud\CloudMonitoring\Resource;
4:
5: use OpenCloud\Common\Exceptions;
6: use OpenCloud\Common\Lang;
7: use OpenCloud\Common\PersistentObject;
8: use OpenCloud\CloudMonitoring\Exception;
9:
10: 11: 12: 13: 14: 15: 16: 17: 18:
19: abstract class AbstractResource extends PersistentObject
20: {
21:
22: 23: 24: 25: 26: 27:
28: public $id;
29:
30: 31: 32: 33: 34: 35:
36: public $name;
37:
38: 39: 40: 41: 42: 43: 44: 45:
46: public function __construct($service, $info)
47: {
48: $this->setService($service);
49: parent::__construct($service, $info);
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59:
60: public function getProperty($haystack, $needle)
61: {
62: if (is_object($haystack) && isset($haystack->$needle)) {
63: return $haystack->$needle;
64: }
65:
66: if (is_array($haystack) && isset($haystack[$needle])) {
67: return $haystack[$needle];
68: }
69:
70: return false;
71: }
72:
73: 74: 75: 76: 77: 78: 79:
80: public function url($subresource = '', $query = array())
81: {
82: $url = $this->baseUrl();
83:
84: if ($subresource) {
85: $url .= "/$subresource";
86: }
87:
88: return $url . $this->MakeQueryString($query);
89: }
90:
91: 92: 93: 94: 95: 96:
97: protected function createJson()
98: {
99: $object = new \stdClass;
100:
101: foreach (static::$emptyObject as $key) {
102: if (isset($this->$key)) {
103: $object->$key = $this->$key;
104: }
105: }
106:
107: foreach (static::$requiredKeys as $requiredKey) {
108: if (!isset($object->$requiredKey)) {
109: throw new Exceptions\CreateError(sprintf(
110: "%s is required to create a new %s", $requiredKey, get_class()
111: ));
112: }
113: }
114:
115: return $object;
116: }
117:
118: 119: 120: 121: 122: 123:
124: protected function updateJson($params = array())
125: {
126: foreach (static::$requiredKeys as $requiredKey) {
127: if (!isset($this->$requiredKey)) {
128: throw new Exceptions\UpdateError(sprintf(
129: "%s is required to create a new %s", $requiredKey, get_class()
130: ));
131: }
132: }
133:
134: return $this;
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function listAll()
144: {
145: return $this->getService()->collection(get_class($this), $this->Url());
146: }
147:
148: public function updateUrl()
149: {
150: return $this->url($this->id);
151: }
152:
153: 154: 155: 156: 157: 158: 159:
160: public function update($params = array())
161: {
162:
163: foreach ($params as $key => $value) {
164: $this->$key = $value;
165: }
166:
167:
168: $this->getLogger()->info('{class}::update({name})', array(
169: 'class' => get_class($this),
170: 'name' => $this->name()
171: ));
172:
173:
174: $obj = $this->updateJson($params);
175: $json = json_encode($obj);
176:
177: $this->checkJsonError();
178:
179: $this->getLogger()->info('{class}::Update JSON [{json}]', array(
180: 'class' => get_class($this),
181: 'json' => $json
182: ));
183:
184:
185: $response = $this->Service()->Request(
186: $this->updateUrl(), 'PUT', array(), $json
187: );
188:
189:
190: if ($response->HttpStatus() > 204) {
191: throw new Exceptions\UpdateError(sprintf(
192: Lang::translate('Error updating [%s] with [%s], status [%d] response [%s]'),
193: get_class($this),
194: $json,
195: $response->HttpStatus(),
196: $response->HttpBody()
197: ));
198: }
199:
200:
201: return $response;
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: public function delete()
212: {
213: $this->getLogger()->info('{class}::delete()', array('class' => get_class($this)));
214:
215:
216: $response = $this->getService()->request($this->url($this->id), 'DELETE');
217:
218:
219: if ($response->HttpStatus() > 204) {
220: throw new Exceptions\DeleteError(sprintf(
221: Lang::translate('Error deleting [%s] [%s], status [%d] response [%s]'),
222: get_class(),
223: $this->Name(),
224: $response->HttpStatus(),
225: $response->HttpBody()
226: ));
227: }
228:
229:
230: return $response;
231: }
232:
233: 234: 235: 236: 237: 238: 239: 240: 241: 242:
243: protected function request($url, $method = 'GET', array $headers = array(), $body = null)
244: {
245: $response = $this->getService()->request($url, $method, $headers, $body);
246: return ($body = $response->HttpBody()) ? json_decode($body) : false;
247: }
248:
249: 250: 251: 252: 253: 254: 255: 256:
257: public function test($params = array(), $debug = false)
258: {
259: if (!empty($params)) {
260: $this->populate($params);
261: }
262:
263: $json = json_encode($this->createJson());
264: $this->checkJsonError();
265:
266:
267: return $this->customAction($this->testUrl($debug), 'POST', $json);
268: }
269:
270: 271: 272: 273: 274: 275: 276:
277: public function testExisting($debug = false)
278: {
279: $json = json_encode($this->updateJson());
280: $this->checkJsonError();
281:
282: $url = $this->url($this->id . '/test' . ($debug ? '?debug=true' : ''));
283: return $this->customAction($url, 'POST', $json);
284: }
285:
286: public function refresh($id = null, $url = null)
287: {
288: if (!$url) {
289: $url = $this->url($id);
290: }
291:
292: parent::refresh($id, $url);
293: }
294:
295: }