Test Setup Failed
Push — master ( 8fa6fb...d3fd71 )
by Alex
02:57
created

PredisProfilerStorage::purge()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 0
dl 0
loc 28
rs 9.5222
c 0
b 0
f 0
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
 * @author Michael Lambert <[email protected]>
24
 */
25
class PredisProfilerStorage implements ProfilerStorageInterface
26
{
27
    const TOKEN_PREFIX = 'sf_profiler_';
28
29
    const REDIS_OPT_SERIALIZER = 1;
30
    const REDIS_OPT_PREFIX = 2;
31
    const REDIS_SERIALIZER_NONE = 0;
32
    const REDIS_SERIALIZER_PHP = 1;
33
34
    protected $dsn;
35
    protected $lifetime;
36
37
    /**
38
     * @var \Predis\Client
0 ignored issues
show
Bug introduced by
The type Predis\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
     */
40
    private $redis;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param string $dsn      A data source name
46
     * @param string $username Not used
47
     * @param string $password Not used
48
     * @param int    $lifetime The lifetime to use for the purge
49
     */
50
    public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
51
    {
52
        $this->dsn = $dsn;
53
        $this->lifetime = (int) $lifetime;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function find($ip, $url, $limit, $method, $start = null, $end = null)
60
    {
61
        $indexName = $this->getIndexName();
62
63
        if (!$indexContent = $this->getValue($indexName, self::REDIS_SERIALIZER_NONE)) {
64
            return array();
65
        }
66
67
        $profileList = array_reverse(explode("\n", $indexContent));
68
        $result = array();
69
70
        foreach ($profileList as $item) {
71
            if (0 === $limit) {
72
                break;
73
            }
74
75
            if ('' == $item) {
76
                continue;
77
            }
78
79
            $values = explode("\t", $item, 7);
80
            list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
81
            $statusCode = isset($values[6]) ? $values[6] : null;
82
83
            $itemTime = (int) $itemTime;
84
85
            if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) {
86
                continue;
87
            }
88
89
            if (!empty($start) && $itemTime < $start) {
90
                continue;
91
            }
92
93
            if (!empty($end) && $itemTime > $end) {
94
                continue;
95
            }
96
97
            $result[] = array(
98
                'token' => $itemToken,
99
                'ip' => $itemIp,
100
                'method' => $itemMethod,
101
                'url' => $itemUrl,
102
                'time' => $itemTime,
103
                'parent' => $itemParent,
104
                'status_code' => $statusCode,
105
            );
106
            --$limit;
107
        }
108
109
        return $result;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function purge()
116
    {
117
        // delete only items from index
118
        $indexName = $this->getIndexName();
119
120
        $indexContent = $this->getValue($indexName, self::REDIS_SERIALIZER_NONE);
121
122
        if (!$indexContent) {
123
            return false;
124
        }
125
126
        $profileList = explode("\n", $indexContent);
127
128
        $result = array();
129
130
        foreach ($profileList as $item) {
131
            if ('' == $item) {
132
                continue;
133
            }
134
135
            if (false !== $pos = strpos($item, "\t")) {
136
                $result[] = $this->getItemName(substr($item, 0, $pos));
137
            }
138
        }
139
140
        $result[] = $indexName;
141
142
        return $this->delete($result);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function read($token)
149
    {
150
        if (empty($token)) {
151
            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...
152
        }
153
154
        $profile = $this->getValue($this->getItemName($token), self::REDIS_SERIALIZER_PHP);
155
156
        if (false !== $profile) {
157
            $profile = $this->createProfileFromData($token, $profile);
158
        }
159
160
        return $profile;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function write(Profile $profile)
167
    {
168
        $data = array(
169
            'token' => $profile->getToken(),
170
            'parent' => $profile->getParentToken(),
171
            'children' => array_map(function ($p) {
172
                return $p->getToken();
173
            }, $profile->getChildren()),
174
            'data' => $profile->getCollectors(),
175
            'ip' => $profile->getIp(),
176
            'method' => $profile->getMethod(),
177
            'url' => $profile->getUrl(),
178
            'time' => $profile->getTime(),
179
        );
180
181
        $profileIndexed = false !== $this->getValue($this->getItemName($profile->getToken()));
182
183
        if ($this->setValue($this->getItemName($profile->getToken()), $data, $this->lifetime, self::REDIS_SERIALIZER_PHP)) {
184
            if (!$profileIndexed) {
185
                // Add to index
186
                $indexName = $this->getIndexName();
187
188
                $indexRow = implode("\t", array(
189
                    $profile->getToken(),
190
                    $profile->getIp(),
191
                    $profile->getMethod(),
192
                    $profile->getUrl(),
193
                    $profile->getTime(),
194
                    $profile->getParentToken(),
195
                    $profile->getStatusCode(),
196
                ))."\n";
197
198
                return $this->appendValue($indexName, $indexRow, $this->lifetime);
199
            }
200
201
            return true;
202
        }
203
204
        return false;
205
    }
206
207
    /**
208
     * Internal convenience method that returns the instance of Redis.
209
     *
210
     * @return \Redis
211
     *
212
     * @throws \RuntimeException
213
     */
214
    protected function getRedis()
215
    {
216
        if (null === $this->redis) {
217
            $data = parse_url($this->dsn);
218
219
            if (false === $data || !isset($data['scheme']) || 'redis' !== $data['scheme'] || !isset($data['host']) || !isset($data['port'])) {
220
                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));
221
            }
222
223
            if (!class_exists('\Predis\Client')) {
224
                throw new \RuntimeException('PredisProfilerStorage requires that the predis is installed.');
225
            }
226
227
            $redis = new \Predis\Client($this->dsn, ['prefix' => self::TOKEN_PREFIX]);
228
229
            $this->redis = $redis;
230
        }
231
232
        return $this->redis;
233
    }
234
235
    /**
236
     * Set instance of the Redis.
237
     *
238
     * @param \Redis $redis
239
     */
240
    public function setRedis($redis)
241
    {
242
        $this->redis = $redis;
0 ignored issues
show
Documentation Bug introduced by
It seems like $redis of type Redis is incompatible with the declared type Predis\Client of property $redis.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
243
    }
244
245
    private function createProfileFromData($token, $data, $parent = null)
246
    {
247
        $profile = new Profile($token);
248
        $profile->setIp($data['ip']);
249
        $profile->setMethod($data['method']);
250
        $profile->setUrl($data['url']);
251
        $profile->setTime($data['time']);
252
        $profile->setCollectors($data['data']);
253
254
        if (!$parent && $data['parent']) {
255
            $parent = $this->read($data['parent']);
256
        }
257
258
        if ($parent) {
259
            $profile->setParent($parent);
260
        }
261
262
        foreach ($data['children'] as $token) {
0 ignored issues
show
introduced by
$token is overwriting one of the parameters of this function.
Loading history...
263
            if (!$token) {
264
                continue;
265
            }
266
267
            if (!$childProfileData = $this->getValue($this->getItemName($token), self::REDIS_SERIALIZER_PHP)) {
268
                continue;
269
            }
270
271
            $profile->addChild($this->createProfileFromData($token, $childProfileData, $profile));
272
        }
273
274
        return $profile;
275
    }
276
277
    /**
278
     * Gets the item name.
279
     *
280
     * @param string $token
281
     *
282
     * @return string
283
     */
284
    private function getItemName($token)
285
    {
286
        $name = $token;
287
288
        if ($this->isItemNameValid($name)) {
289
            return $name;
290
        }
291
292
        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...
293
    }
294
295
    /**
296
     * Gets the name of the index.
297
     *
298
     * @return string
299
     */
300
    private function getIndexName()
301
    {
302
        $name = 'index';
303
304
        if ($this->isItemNameValid($name)) {
305
            return $name;
306
        }
307
308
        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...
309
    }
310
311
    private function isItemNameValid($name)
312
    {
313
        $length = strlen($name);
314
315
        if ($length > 2147483648) {
316
            throw new \RuntimeException(sprintf('The Redis item key "%s" is too long (%s bytes). Allowed maximum size is 2^31 bytes.', $name, $length));
317
        }
318
319
        return true;
320
    }
321
322
    /**
323
     * Retrieves an item from the Redis server.
324
     *
325
     * @param string $key
326
     * @param int    $serializer
327
     *
328
     * @return mixed
329
     */
330
    private function getValue($key, $serializer = self::REDIS_SERIALIZER_NONE)
0 ignored issues
show
Unused Code introduced by
The parameter $serializer is not used and could be removed. ( Ignorable by Annotation )

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

330
    private function getValue($key, /** @scrutinizer ignore-unused */ $serializer = self::REDIS_SERIALIZER_NONE)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
331
    {
332
        $redis = $this->getRedis();
333
334
        return unserialize($redis->get($key), ['allowed_classes' => true]);
335
    }
336
337
    /**
338
     * Stores an item on the Redis server under the specified key.
339
     *
340
     * @param string $key
341
     * @param mixed  $value
342
     * @param int    $expiration
343
     * @param int    $serializer
344
     *
345
     * @return bool
346
     */
347
    private function setValue($key, $value, $expiration = 0, $serializer = self::REDIS_SERIALIZER_NONE)
0 ignored issues
show
Unused Code introduced by
The parameter $serializer is not used and could be removed. ( Ignorable by Annotation )

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

347
    private function setValue($key, $value, $expiration = 0, /** @scrutinizer ignore-unused */ $serializer = self::REDIS_SERIALIZER_NONE)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
348
    {
349
        $redis = $this->getRedis();
350
351
        return $redis->setex($key, $expiration, serialize($value));
352
    }
353
354
    /**
355
     * Appends data to an existing item on the Redis server.
356
     *
357
     * @param string $key
358
     * @param string $value
359
     * @param int    $expiration
360
     *
361
     * @return bool
362
     */
363
    private function appendValue($key, $value, $expiration = 0)
364
    {
365
        $redis = $this->getRedis();
366
367
        if ($redis->exists($key)) {
368
        	$data = unserialize($redis->get($key));
369
        	$data .= $value;
370
371
            return $redis->setex($key, $expiration, serialize($data));
372
        }
373
374
        return $redis->setex($key, $expiration, serialize($value));
375
    }
376
377
    /**
378
     * Removes the specified keys.
379
     *
380
     * @param array $keys
381
     *
382
     * @return bool
383
     */
384
    private function delete(array $keys)
385
    {
386
        return (bool) $this->getRedis()->delete($keys);
387
    }
388
}
389