Completed
Push — master ( 185853...b7deb5 )
by Marcel
01:56
created

src/Storages/Drivers/RedisStorage.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace BotMan\BotMan\Storages\Drivers;
4
5
use Redis;
6
use RuntimeException;
7
use Illuminate\Support\Collection;
8
use BotMan\BotMan\Interfaces\StorageInterface;
9
10
class RedisStorage implements StorageInterface
11
{
12
    const KEY_PREFIX = 'botman:storage:';
13
14
    /**
15
     * @var Redis
16
     */
17
    private $redis;
18
    private $host;
19
    private $port;
20
    private $auth;
21
22
    /**
23
     * RedisCache constructor.
24
     * @param $host
25
     * @param $port
26
     * @param $auth
27
     */
28 View Code Duplication
    public function __construct($host = '127.0.0.1', $port = 6379, $auth = null)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        if (! class_exists(Redis::class)) {
31
            throw new RuntimeException('phpredis extension is required for RedisStorage');
32
        }
33
        $this->host = $host;
34
        $this->port = $port;
35
        $this->auth = $auth;
36
        $this->connect();
37
    }
38
39
    /**
40
     * Save an item in the storage with a specific key and data.
41
     *
42
     * @param  array $data
43
     * @param  string $key
44
     */
45
    public function save(array $data, $key)
46
    {
47
        $this->redis->set($this->decorateKey($key), $data);
48
    }
49
50
    /**
51
     * Retrieve an item from the storage by key.
52
     *
53
     * @param  string $key
54
     * @return Collection
55
     */
56
    public function get($key)
57
    {
58
        $value = $this->redis->get($this->decorateKey($key));
59
60
        return $value ? Collection::make($value) : new Collection();
61
    }
62
63
    /**
64
     * Delete a stored item by its key.
65
     *
66
     * @param  string $key
67
     */
68
    public function delete($key)
69
    {
70
        $this->redis->del($this->decorateKey($key));
71
    }
72
73
    /**
74
     * Return all stored entries.
75
     *
76
     * @return array
77
     */
78
    public function all()
79
    {
80
        $entries = [];
81
        while ($keys = $this->redis->scan($it, self::KEY_PREFIX.'*')) {
0 ignored issues
show
The variable $it does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
            foreach ($keys as $key) {
0 ignored issues
show
The expression $keys of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
83
                $entries[substr($key, strlen(self::KEY_PREFIX))] = Collection::make($this->redis->get($key));
84
            }
85
        }
86
87
        return $entries;
88
    }
89
90
    /**
91
     * Namespace botman keys in redis.
92
     *
93
     * @param $key
94
     * @return string
95
     */
96
    private function decorateKey($key)
97
    {
98
        return self::KEY_PREFIX.$key;
99
    }
100
101 View Code Duplication
    private function connect()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $this->redis = new Redis();
104
        $this->redis->connect($this->host, $this->port);
105
        if ($this->auth !== null) {
106
            $this->redis->auth($this->auth);
107
        }
108
109
        if (function_exists('igbinary_serialize') && defined('Redis::SERIALIZER_IGBINARY')) {
110
            $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
111
        } else {
112
            $this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
113
        }
114
    }
115
116
    public function __wakeup()
117
    {
118
        $this->connect();
119
    }
120
}
121