RedisProfilerStorage::getRedis()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 7
nop 0
dl 0
loc 30
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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 Sitetheory\Bundle\ProfilerStorageBundle\Profiler;
13
14
use Symfony\Component\HttpKernel\Profiler\Profile;
15
16
/**
17
 * RedisProfilerStorage stores profiling information in Redis.
18
 *
19
 * Class RedisProfilerStorage
20
 *
21
 * @author Andrej Hudec <[email protected]>
22
 * @author Stephane PY <[email protected]>
23
 */
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Symfony\Component\HttpKe...torageInterface::read() of Symfony\Component\HttpKernel\Profiler\Profile.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $profile also could return the type string which is incompatible with the return type mandated by Symfony\Component\HttpKe...torageInterface::read() of Symfony\Component\HttpKernel\Profiler\Profile.
Loading history...
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function write(Profile $profile)
166
    {
167
        $data = array(
168
            'token' => $profile->getToken(),
169
            'parent' => $profile->getParentToken(),
170
            'children' => array_map(function ($p) {
171
                return $p->getToken();
172
            }, $profile->getChildren()),
173
            'data' => $profile->getCollectors(),
174
            'ip' => $profile->getIp(),
175
            'method' => $profile->getMethod(),
176
            'url' => $profile->getUrl(),
177
            'time' => $profile->getTime(),
178
        );
179
180
        $profileIndexed = false !== $this->getValue($this->getItemName($profile->getToken()));
181
182
        if ($this->setValue($this->getItemName($profile->getToken()), $data, $this->lifetime, self::REDIS_SERIALIZER_PHP)) {
183
            if (!$profileIndexed) {
184
                // Add to index
185
                $indexName = $this->getIndexName();
186
187
                $indexRow = implode("\t", array(
188
                    $profile->getToken(),
189
                    $profile->getIp(),
190
                    $profile->getMethod(),
191
                    $profile->getUrl(),
192
                    $profile->getTime(),
193
                    $profile->getParentToken(),
194
                    $profile->getStatusCode(),
195
                ))."\n";
196
197
                return $this->appendValue($indexName, $indexRow, $this->lifetime);
198
            }
199
200
            return true;
201
        }
202
203
        return false;
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));
0 ignored issues
show
Bug introduced by
substr($data['path'], 1) of type string is incompatible with the type integer expected by parameter $dbindex of Redis::select(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
                $redis->select(/** @scrutinizer ignore-type */ substr($data['path'], 1));
Loading history...
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) {
0 ignored issues
show
introduced by
$token is overwriting one of the parameters of this function.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
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)
397
    {
398
        return (bool) $this->getRedis()->delete($keys);
399
    }
400
}
401