Total Complexity | 57 |
Total Lines | 375 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RedisProfilerStorage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RedisProfilerStorage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class RedisProfilerStorage implements ProfilerStorageInterface |
||
25 | { |
||
26 | const TOKEN_PREFIX = 'sf_profiler_'; |
||
27 | |||
28 | const REDIS_OPT_SERIALIZER = 1; |
||
29 | const REDIS_OPT_PREFIX = 2; |
||
30 | const REDIS_SERIALIZER_NONE = 0; |
||
31 | const REDIS_SERIALIZER_PHP = 1; |
||
32 | |||
33 | protected $dsn; |
||
34 | protected $lifetime; |
||
35 | |||
36 | /** |
||
37 | * @var \Redis |
||
38 | */ |
||
39 | private $redis; |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * @param string $dsn A data source name |
||
45 | * @param string $username Not used |
||
46 | * @param string $password Not used |
||
47 | * @param int $lifetime The lifetime to use for the purge |
||
48 | */ |
||
49 | public function __construct($dsn, $username = '', $password = '', $lifetime = 86400) |
||
50 | { |
||
51 | $this->dsn = $dsn; |
||
52 | $this->lifetime = (int) $lifetime; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function find($ip, $url, $limit, $method, $start = null, $end = null) |
||
59 | { |
||
60 | $indexName = $this->getIndexName(); |
||
61 | |||
62 | if (!$indexContent = $this->getValue($indexName, self::REDIS_SERIALIZER_NONE)) { |
||
63 | return array(); |
||
64 | } |
||
65 | |||
66 | $profileList = array_reverse(explode("\n", $indexContent)); |
||
67 | $result = array(); |
||
68 | |||
69 | foreach ($profileList as $item) { |
||
70 | if (0 === $limit) { |
||
71 | break; |
||
72 | } |
||
73 | |||
74 | if ('' == $item) { |
||
75 | continue; |
||
76 | } |
||
77 | |||
78 | $values = explode("\t", $item, 7); |
||
79 | list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values; |
||
80 | $statusCode = isset($values[6]) ? $values[6] : null; |
||
81 | |||
82 | $itemTime = (int) $itemTime; |
||
83 | |||
84 | if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | if (!empty($start) && $itemTime < $start) { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | if (!empty($end) && $itemTime > $end) { |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | $result[] = array( |
||
97 | 'token' => $itemToken, |
||
98 | 'ip' => $itemIp, |
||
99 | 'method' => $itemMethod, |
||
100 | 'url' => $itemUrl, |
||
101 | 'time' => $itemTime, |
||
102 | 'parent' => $itemParent, |
||
103 | 'status_code' => $statusCode, |
||
104 | ); |
||
105 | --$limit; |
||
106 | } |
||
107 | |||
108 | return $result; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function purge() |
||
115 | { |
||
116 | // delete only items from index |
||
117 | $indexName = $this->getIndexName(); |
||
118 | |||
119 | $indexContent = $this->getValue($indexName, self::REDIS_SERIALIZER_NONE); |
||
120 | |||
121 | if (!$indexContent) { |
||
122 | return false; |
||
123 | } |
||
124 | |||
125 | $profileList = explode("\n", $indexContent); |
||
126 | |||
127 | $result = array(); |
||
128 | |||
129 | foreach ($profileList as $item) { |
||
130 | if ('' == $item) { |
||
131 | continue; |
||
132 | } |
||
133 | |||
134 | if (false !== $pos = strpos($item, "\t")) { |
||
135 | $result[] = $this->getItemName(substr($item, 0, $pos)); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $result[] = $indexName; |
||
140 | |||
141 | return $this->delete($result); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function read($token) |
||
148 | { |
||
149 | if (empty($token)) { |
||
150 | return false; |
||
|
|||
151 | } |
||
152 | |||
153 | $profile = $this->getValue($this->getItemName($token), self::REDIS_SERIALIZER_PHP); |
||
154 | |||
155 | if (false !== $profile) { |
||
156 | $profile = $this->createProfileFromData($token, $profile); |
||
157 | } |
||
158 | |||
159 | return $profile; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function write(Profile $profile) |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Internal convenience method that returns the instance of Redis. |
||
208 | * |
||
209 | * @throws \RuntimeException |
||
210 | * |
||
211 | * @return \Redis |
||
212 | */ |
||
213 | protected function getRedis() |
||
214 | { |
||
215 | if (null === $this->redis) { |
||
216 | $data = parse_url($this->dsn); |
||
217 | |||
218 | if (false === $data || !isset($data['scheme']) || 'redis' !== $data['scheme'] || !isset($data['host']) || !isset($data['port'])) { |
||
219 | throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Redis with an invalid dsn "%s". The minimal expected format is "redis://[host]:port".', $this->dsn)); |
||
220 | } |
||
221 | |||
222 | if (!extension_loaded('redis')) { |
||
223 | throw new \RuntimeException('RedisProfilerStorage requires that the redis extension is loaded.'); |
||
224 | } |
||
225 | |||
226 | $redis = new \Redis(); |
||
227 | $redis->connect($data['host'], $data['port']); |
||
228 | |||
229 | if (isset($data['path'])) { |
||
230 | $redis->select(substr($data['path'], 1)); |
||
231 | } |
||
232 | |||
233 | if (isset($data['pass'])) { |
||
234 | $redis->auth($data['pass']); |
||
235 | } |
||
236 | |||
237 | $redis->setOption(self::REDIS_OPT_PREFIX, self::TOKEN_PREFIX); |
||
238 | |||
239 | $this->redis = $redis; |
||
240 | } |
||
241 | |||
242 | return $this->redis; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Set instance of the Redis. |
||
247 | * |
||
248 | * @param \Redis $redis |
||
249 | */ |
||
250 | public function setRedis($redis) |
||
251 | { |
||
252 | $this->redis = $redis; |
||
253 | } |
||
254 | |||
255 | private function createProfileFromData($token, $data, $parent = null) |
||
256 | { |
||
257 | $profile = new Profile($token); |
||
258 | $profile->setIp($data['ip']); |
||
259 | $profile->setMethod($data['method']); |
||
260 | $profile->setUrl($data['url']); |
||
261 | $profile->setTime($data['time']); |
||
262 | $profile->setCollectors($data['data']); |
||
263 | |||
264 | if (!$parent && $data['parent']) { |
||
265 | $parent = $this->read($data['parent']); |
||
266 | } |
||
267 | |||
268 | if ($parent) { |
||
269 | $profile->setParent($parent); |
||
270 | } |
||
271 | |||
272 | foreach ($data['children'] as $token) { |
||
273 | if (!$token) { |
||
274 | continue; |
||
275 | } |
||
276 | |||
277 | if (!$childProfileData = $this->getValue($this->getItemName($token), self::REDIS_SERIALIZER_PHP)) { |
||
278 | continue; |
||
279 | } |
||
280 | |||
281 | $profile->addChild($this->createProfileFromData($token, $childProfileData, $profile)); |
||
282 | } |
||
283 | |||
284 | return $profile; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Gets the item name. |
||
289 | * |
||
290 | * @param string $token |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | private function getItemName($token) |
||
295 | { |
||
296 | $name = $token; |
||
297 | |||
298 | if ($this->isItemNameValid($name)) { |
||
299 | return $name; |
||
300 | } |
||
301 | |||
302 | return false; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Gets the name of the index. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | private function getIndexName() |
||
311 | { |
||
312 | $name = 'index'; |
||
313 | |||
314 | if ($this->isItemNameValid($name)) { |
||
315 | return $name; |
||
316 | } |
||
317 | |||
318 | return false; |
||
319 | } |
||
320 | |||
321 | private function isItemNameValid($name) |
||
322 | { |
||
323 | $length = strlen($name); |
||
324 | |||
325 | if ($length > 2147483648) { |
||
326 | throw new \RuntimeException(sprintf('The Redis item key "%s" is too long (%s bytes). Allowed maximum size is 2^31 bytes.', $name, $length)); |
||
327 | } |
||
328 | |||
329 | return true; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Retrieves an item from the Redis server. |
||
334 | * |
||
335 | * @param string $key |
||
336 | * @param int $serializer |
||
337 | * |
||
338 | * @return mixed |
||
339 | */ |
||
340 | private function getValue($key, $serializer = self::REDIS_SERIALIZER_NONE) |
||
341 | { |
||
342 | $redis = $this->getRedis(); |
||
343 | $redis->setOption(self::REDIS_OPT_SERIALIZER, $serializer); |
||
344 | |||
345 | return $redis->get($key); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Stores an item on the Redis server under the specified key. |
||
350 | * |
||
351 | * @param string $key |
||
352 | * @param mixed $value |
||
353 | * @param int $expiration |
||
354 | * @param int $serializer |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | private function setValue($key, $value, $expiration = 0, $serializer = self::REDIS_SERIALIZER_NONE) |
||
359 | { |
||
360 | $redis = $this->getRedis(); |
||
361 | $redis->setOption(self::REDIS_OPT_SERIALIZER, $serializer); |
||
362 | |||
363 | return $redis->setex($key, $expiration, $value); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Appends data to an existing item on the Redis server. |
||
368 | * |
||
369 | * @param string $key |
||
370 | * @param string $value |
||
371 | * @param int $expiration |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | private function appendValue($key, $value, $expiration = 0) |
||
376 | { |
||
377 | $redis = $this->getRedis(); |
||
378 | $redis->setOption(self::REDIS_OPT_SERIALIZER, self::REDIS_SERIALIZER_NONE); |
||
379 | |||
380 | if ($redis->exists($key)) { |
||
381 | $redis->append($key, $value); |
||
382 | |||
383 | return $redis->setTimeout($key, $expiration); |
||
384 | } |
||
385 | |||
386 | return $redis->setex($key, $expiration, $value); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Removes the specified keys. |
||
391 | * |
||
392 | * @param array $keys |
||
393 | * |
||
394 | * @return bool |
||
395 | */ |
||
396 | private function delete(array $keys) |
||
399 | } |
||
400 | } |
||
401 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: