1: <?php
2: /**
3: * PHP OpenCloud library.
4: *
5: * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
6: * @license https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
7: * @version 1.6.0
8: * @author Glen Campbell <glen.campbell@rackspace.com>
9: * @author Jamie Hannaford <jamie.hannaford@rackspace.com>
10: */
11:
12: namespace OpenCloud\LoadBalancer\Resource;
13:
14: use Guzzle\Http\Exception\ClientErrorResponseException;
15: use OpenCloud\Common\PersistentObject;
16: use OpenCloud\Common\Lang;
17:
18: /**
19: * SubResource is an abstract class that handles subresources of a
20: * LoadBalancer object; for example, the
21: * `/loadbalancers/{id}/errorpage`. Since most of the subresources are
22: * handled in a similar manner, this consolidates the functions.
23: *
24: * There are really four pieces of data that define a subresource:
25: * * `$url_resource` - the actual name of the url component
26: * * `$json_name` - the name of the JSON object holding the data
27: * * `$json_collection_name` - if the collection is not simply
28: * `$json_name . 's'`, this defines the collectio name.
29: * * `$json_collection_element` - if the object in a collection is not
30: * anonymous, this defines the name of the element holding the object.
31: * Of these, only the `$json_name` and `$url_resource` are required.
32: */
33: abstract class SubResource extends PersistentObject
34: {
35: /**
36: * This method needs attention.
37: *
38: * @codeCoverageIgnore
39: */
40: public function initialRefresh()
41: {
42: if (isset($this->id)) {
43: $this->refresh();
44: } else {
45: $entity = (method_exists($this->getParent(), 'url')) ? $this->getParent() : $this->getService();
46: $this->refresh(null, $entity->url($this->resourceName()));
47: }
48: }
49:
50: /**
51: * returns the JSON document's object for creating the subresource
52: *
53: * The value `$_create_keys` should be an array of names of data items
54: * that can be used in the creation of the object.
55: *
56: * @return \stdClass;
57: */
58: protected function createJson()
59: {
60: $object = new \stdClass;
61:
62: foreach ($this->createKeys as $item) {
63: $object->$item = $this->$item;
64: }
65:
66: if ($top = $this->jsonName()) {
67: $object = (object) array($top => $object);
68: }
69:
70: return $object;
71: }
72:
73: /**
74: * returns the JSON for the update (same as create)
75: *
76: * For these subresources, the update JSON is the same as the Create JSON
77: * @return \stdClass
78: */
79: protected function updateJson($params = array())
80: {
81: return $this->createJson();
82: }
83:
84: /**
85: * returns a (default) name of the object
86: *
87: * The name is constructed by the object class and the object's ID.
88: *
89: * @api
90: * @return string
91: */
92: public function name()
93: {
94: $classArray = explode('\\', get_class($this));
95: return method_exists($this->getParent(), 'id')
96: ? sprintf('%s-%s', end($classArray), $this->getParent()->id())
97: : parent::name();
98: }
99:
100: public function refresh($id = null, $url = null)
101: {
102: try {
103: return parent::refresh($id, $url);
104: } catch (ClientErrorResponseException $e) {
105: return false;
106: }
107: }
108:
109: }