Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.10% |
27 / 29 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
Redis | |
93.10% |
27 / 29 |
|
90.00% |
9 / 10 |
18.11 | |
0.00% |
0 / 1 |
__construct | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
redis | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
delete | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
encodeValue | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
decodeValue | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Debug\Storage; |
15 | |
16 | /** |
17 | * Debug redis storage class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Debug |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 1.3.0 |
25 | */ |
26 | class Redis extends AbstractStorage |
27 | { |
28 | |
29 | /** |
30 | * Redis object |
31 | * @var \Redis |
32 | */ |
33 | protected $redis = null; |
34 | |
35 | /** |
36 | * Constructor |
37 | * |
38 | * Instantiate the Redis storage object |
39 | * |
40 | * @param string $format |
41 | * @param string $host |
42 | * @param int $port |
43 | * @throws Exception |
44 | */ |
45 | public function __construct($format = null, $host = 'localhost', $port = 6379) |
46 | { |
47 | if (!class_exists('Redis', false)) { |
48 | throw new Exception('Error: Redis is not available.'); |
49 | } |
50 | |
51 | parent::__construct($format); |
52 | |
53 | $this->redis = new \Redis(); |
54 | if (!$this->redis->connect($host, (int)$port)) { |
55 | throw new Exception('Error: Unable to connect to the redis server.'); |
56 | } |
57 | } |
58 | |
59 | /** |
60 | * Get the redis object. |
61 | * |
62 | * @return \Redis |
63 | */ |
64 | public function redis() |
65 | { |
66 | return $this->redis; |
67 | } |
68 | |
69 | /** |
70 | * Get the current version of redis. |
71 | * |
72 | * @return string |
73 | */ |
74 | public function getVersion() |
75 | { |
76 | return $this->redis->info()['redis_version']; |
77 | } |
78 | |
79 | /** |
80 | * Save debug data |
81 | * |
82 | * @param string $id |
83 | * @param mixed $value |
84 | * @return Redis |
85 | */ |
86 | public function save($id, $value) |
87 | { |
88 | $this->redis->set($id, $this->encodeValue($value)); |
89 | return $this; |
90 | } |
91 | |
92 | /** |
93 | * Get debug data |
94 | * |
95 | * @param string $id |
96 | * @return mixed |
97 | */ |
98 | public function get($id) |
99 | { |
100 | return $this->decodeValue($this->redis->get($id)); |
101 | } |
102 | |
103 | /** |
104 | * Determine if debug data exists |
105 | * |
106 | * @param string $id |
107 | * @return boolean |
108 | */ |
109 | public function has($id) |
110 | { |
111 | return ($this->redis->get($id) !== false); |
112 | } |
113 | |
114 | /** |
115 | * Delete debug data |
116 | * |
117 | * @param string $id |
118 | * @return Redis |
119 | */ |
120 | public function delete($id) |
121 | { |
122 | $this->redis->del($id); |
123 | return $this; |
124 | } |
125 | |
126 | /** |
127 | * Clear all debug data |
128 | * |
129 | * @return Redis |
130 | */ |
131 | public function clear() |
132 | { |
133 | $this->redis->flushDb(); |
134 | return $this; |
135 | } |
136 | |
137 | /** |
138 | * Encode the value based on the format |
139 | * |
140 | * @param mixed $value |
141 | * @throws Exception |
142 | * @return string |
143 | */ |
144 | public function encodeValue($value) |
145 | { |
146 | if ($this->format == self::JSON) { |
147 | $value = json_encode($value, JSON_PRETTY_PRINT); |
148 | } else if ($this->format == self::PHP) { |
149 | $value = serialize($value); |
150 | } else if (!is_string($value)) { |
151 | throw new Exception('Error: The value must be a string if storing as a text file.'); |
152 | } |
153 | |
154 | return $value; |
155 | } |
156 | |
157 | /** |
158 | * Decode the value based on the format |
159 | * |
160 | * @param mixed $value |
161 | * @return mixed |
162 | */ |
163 | public function decodeValue($value) |
164 | { |
165 | if ($value !== false) { |
166 | if ($this->format == self::JSON) { |
167 | $value = json_decode($value, true); |
168 | } else if ($this->format == self::PHP) { |
169 | $value = unserialize($value); |
170 | } |
171 | } |
172 | |
173 | return $value; |
174 | } |
175 | |
176 | } |