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\LoadBalancer\Resource;
19:
20: use Guzzle\Http\Exception\ClientErrorResponseException;
21: use OpenCloud\Common\Resource\PersistentResource;
22:
23: /**
24: * Class that represents abstract functionality for Load Balancer resources
25: *
26: * @package OpenCloud\LoadBalancer\Resource
27: */
28: abstract class AbstractResource extends PersistentResource
29: {
30: public function refresh($id = null, $url = null)
31: {
32: if (!isset($this->id)) {
33: return false;
34: }
35:
36: try {
37: return parent::refresh($id, $url);
38: } catch (ClientErrorResponseException $e) {
39: return false;
40: }
41: }
42:
43: protected function createJson()
44: {
45: $object = new \stdClass;
46:
47: foreach ($this->createKeys as $item) {
48: $object->$item = $this->$item;
49: }
50:
51: if ($top = $this->jsonName()) {
52: $object = array($top => $object);
53: }
54:
55: return $object;
56: }
57:
58: protected function updateJson($params = array())
59: {
60: return $this->createJson();
61: }
62:
63: public function name()
64: {
65: $classArray = explode('\\', get_class($this));
66:
67: return method_exists($this->getParent(), 'id')
68: ? sprintf('%s-%s', end($classArray), $this->getParent()->id())
69: : parent::name();
70: }
71: }
72: