91222c165b86ececf9069390e7021a0f59d43f11
[yaffs-website] / vendor / symfony / http-kernel / Tests / Profiler / Mock / RedisMock.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
13
14 /**
15  * RedisMock for simulating Redis extension in tests.
16  *
17  * @author Andrej Hudec <pulzarraider@gmail.com>
18  */
19 class RedisMock
20 {
21     private $connected = false;
22     private $storage = array();
23
24     /**
25      * Add a server to connection pool.
26      *
27      * @param string $host
28      * @param int    $port
29      * @param float  $timeout
30      *
31      * @return bool
32      */
33     public function connect($host, $port = 6379, $timeout = 0)
34     {
35         if ('127.0.0.1' == $host && 6379 == $port) {
36             $this->connected = true;
37
38             return true;
39         }
40
41         return false;
42     }
43
44     /**
45      * Set client option.
46      *
47      * @param int $name
48      * @param int $value
49      *
50      * @return bool
51      */
52     public function setOption($name, $value)
53     {
54         if (!$this->connected) {
55             return false;
56         }
57
58         return true;
59     }
60
61     /**
62      * Verify if the specified key exists.
63      *
64      * @param string $key
65      *
66      * @return bool
67      */
68     public function exists($key)
69     {
70         if (!$this->connected) {
71             return false;
72         }
73
74         return isset($this->storage[$key]);
75     }
76
77     /**
78      * Store data at the server with expiration time.
79      *
80      * @param string $key
81      * @param int    $ttl
82      * @param mixed  $value
83      *
84      * @return bool
85      */
86     public function setex($key, $ttl, $value)
87     {
88         if (!$this->connected) {
89             return false;
90         }
91
92         $this->storeData($key, $value);
93
94         return true;
95     }
96
97     /**
98      * Sets an expiration time on an item.
99      *
100      * @param string $key
101      * @param int    $ttl
102      *
103      * @return bool
104      */
105     public function setTimeout($key, $ttl)
106     {
107         if (!$this->connected) {
108             return false;
109         }
110
111         if (isset($this->storage[$key])) {
112             return true;
113         }
114
115         return false;
116     }
117
118     /**
119      * Retrieve item from the server.
120      *
121      * @param string $key
122      *
123      * @return bool
124      */
125     public function get($key)
126     {
127         if (!$this->connected) {
128             return false;
129         }
130
131         return $this->getData($key);
132     }
133
134     /**
135      * Append data to an existing item.
136      *
137      * @param string $key
138      * @param string $value
139      *
140      * @return int Size of the value after the append
141      */
142     public function append($key, $value)
143     {
144         if (!$this->connected) {
145             return false;
146         }
147
148         if (isset($this->storage[$key])) {
149             $this->storeData($key, $this->getData($key).$value);
150
151             return strlen($this->storage[$key]);
152         }
153
154         return false;
155     }
156
157     /**
158      * Remove specified keys.
159      *
160      * @param string|array $key
161      *
162      * @return int
163      */
164     public function delete($key)
165     {
166         if (!$this->connected) {
167             return false;
168         }
169
170         if (is_array($key)) {
171             $result = 0;
172             foreach ($key as $k) {
173                 if (isset($this->storage[$k])) {
174                     unset($this->storage[$k]);
175                     ++$result;
176                 }
177             }
178
179             return $result;
180         }
181
182         if (isset($this->storage[$key])) {
183             unset($this->storage[$key]);
184
185             return 1;
186         }
187
188         return 0;
189     }
190
191     /**
192      * Flush all existing items from all databases at the server.
193      *
194      * @return bool
195      */
196     public function flushAll()
197     {
198         if (!$this->connected) {
199             return false;
200         }
201
202         $this->storage = array();
203
204         return true;
205     }
206
207     /**
208      * Close Redis server connection.
209      *
210      * @return bool
211      */
212     public function close()
213     {
214         $this->connected = false;
215
216         return true;
217     }
218
219     private function getData($key)
220     {
221         if (isset($this->storage[$key])) {
222             return unserialize($this->storage[$key]);
223         }
224
225         return false;
226     }
227
228     private function storeData($key, $value)
229     {
230         $this->storage[$key] = serialize($value);
231
232         return true;
233     }
234
235     public function select($dbnum)
236     {
237         if (!$this->connected) {
238             return false;
239         }
240
241         if (0 > $dbnum) {
242             return false;
243         }
244
245         return true;
246     }
247 }