1 : <?php
2 :
3 : /**
4 : * DataProxy implmentation that caches the output of the proxy it wraps.
5 : *
6 : * @category Services
7 : * @package Services_Twilio
8 : * @author Neuman Vong <neuman@twilio.com>
9 : * @license http://creativecommons.org/licenses/MIT/ MIT
10 : * @link http://pear.php.net/package/Services_Twilio
11 : */
12 : class Services_Twilio_CachingDataProxy
13 : implements Services_Twilio_DataProxy
14 : {
15 : /**
16 : * The proxy being wrapped.
17 : *
18 : * @var DataProxy $proxy
19 : */
20 : protected $proxy;
21 :
22 : /**
23 : * The principal data used to retrieve an object from the proxy.
24 : *
25 : * @var array $principal
26 : */
27 : protected $principal;
28 :
29 : /**
30 : * The object cache.
31 : *
32 : * @var object $cache
33 : */
34 : protected $cache;
35 :
36 : /**
37 : * Constructor.
38 : *
39 : * @param array $principal Usually the SID
40 : * @param Services_Twilio_DataProxy $proxy The proxy
41 : * @param object|null $cache The cache
42 : */
43 : public function __construct($principal, Services_Twilio_DataProxy $proxy,
44 : $cache = null
45 : ) {
46 27 : if (is_scalar($principal)) {
47 27 : $principal = array('sid' => $principal, 'params' => array());
48 27 : }
49 27 : $this->principal = $principal;
50 27 : $this->proxy = $proxy;
51 27 : $this->cache = $cache;
52 27 : }
53 :
54 : /**
55 : * Set the object cache.
56 : *
57 : * @param object $object The new object
58 : *
59 : * @return null
60 : */
61 : public function setCache($object)
62 : {
63 0 : $this->cache = $object;
64 0 : }
65 :
66 : /**
67 : * Implementation of magic method __get.
68 : *
69 : * @param string $prop The name of the property to get
70 : *
71 : * @return mixed The value of the property
72 : */
73 : public function __get($prop)
74 : {
75 19 : if ($prop == 'sid') {
76 6 : return $this->principal['sid'];
77 : }
78 14 : if (empty($this->cache)) {
79 8 : $this->_load();
80 7 : }
81 13 : return isset($this->cache->$prop)
82 13 : ? $this->cache->$prop
83 13 : : null;
84 : }
85 :
86 : /**
87 : * Implementation of retrieveData.
88 : *
89 : * @param string $path The path
90 : * @param array $params Optional parameters
91 : *
92 : * @return object Object representation
93 : */
94 : public function retrieveData($path, array $params = array())
95 : {
96 12 : return $this->proxy->retrieveData(
97 12 : $this->principal['sid'] . "/$path",
98 : $params
99 12 : );
100 : }
101 :
102 : /**
103 : * Implementation of createData.
104 : *
105 : * @param string $path The path
106 : * @param array $params Optional parameters
107 : *
108 : * @return object Object representation
109 : */
110 : public function createData($path, array $params = array())
111 : {
112 10 : return $this->proxy->createData(
113 10 : $this->principal['sid'] . "/$path",
114 : $params
115 10 : );
116 : }
117 :
118 : /**
119 : * Implementation of updateData.
120 : *
121 : * @param array $params Update parameters
122 : *
123 : * @return object Object representation
124 : */
125 : public function updateData($params)
126 : {
127 6 : $this->cache = $this->proxy->createData(
128 6 : $this->principal['sid'],
129 : $params
130 6 : );
131 6 : return $this;
132 : }
133 :
134 : /**
135 : * Implementation of deleteData.
136 : *
137 : * @param string $path The path
138 : * @param array $params Optional parameters
139 : *
140 : * @return null
141 : */
142 : public function deleteData($path, array $params = array())
143 : {
144 1 : $this->proxy->delete(
145 1 : $this->principal['sid'] . "/$path",
146 : $params
147 1 : );
148 1 : }
149 :
150 : /**
151 : * Retrieves object from proxy into cache, then initializes subresources.
152 : *
153 : * @param object|null $object The object
154 : *
155 : * @return null
156 : */
157 : private function _load($object = null)
158 : {
159 7 : $this->cache = $object !== null
160 8 : ? $object
161 8 : : $this->proxy->retrieveData($this->principal['sid']);
162 7 : }
163 : }
|