Passed
Push — 6.0 ( f409e2...e26961 )
by liu
02:35
created

Redis::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\session\driver;
14
15
use think\Exception;
16
use think\session\SessionHandler;
17
18
class Redis implements SessionHandler
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
19
{
20
    /** @var \Redis */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
21
    protected $handler = null;
22
    protected $config  = [
23
        'host'       => '127.0.0.1', // redis主机
24
        'port'       => 6379, // redis端口
25
        'password'   => '', // 密码
26
        'select'     => 0, // 操作库
27
        'expire'     => 3600, // 有效期(秒)
28
        'timeout'    => 0, // 超时时间(秒)
29
        'persistent' => true, // 是否长连接
30
        'name'       => '', // session key前缀
31
    ];
32
33
    public function __construct(array $config = [])
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
34
    {
35
        $this->config = array_merge($this->config, $config);
36
37
        $this->init();
38
    }
39
40
    /**
41
     * 打开Session
42
     * @access public
43
     * @return bool
44
     * @throws Exception
45
     */
46
    public function init(): bool
47
    {
48
        if (extension_loaded('redis')) {
49
            $this->handler = new \Redis;
50
51
            // 建立连接
52
            $func = $this->config['persistent'] ? 'pconnect' : 'connect';
53
            $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
54
55
            if ('' != $this->config['password']) {
56
                $this->handler->auth($this->config['password']);
57
            }
58
59
            if (0 != $this->config['select']) {
60
                $this->handler->select($this->config['select']);
61
            }
62
        } elseif (class_exists('\Predis\Client')) {
63
            $params = [];
64
            foreach ($this->config as $key => $val) {
65
                if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication'])) {
66
                    $params[$key] = $val;
67
                    unset($this->config[$key]);
68
                }
69
            }
70
            $this->handler = new \Predis\Client($this->config, $params);
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...
71
        } else {
72
            throw new \BadFunctionCallException('not support: redis');
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * 读取Session
80
     * @access public
81
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @return string
83
     */
84
    public function read(string $sessID): array
85
    {
86
        return $this->handler->get($this->config['prefix'] . $sessID);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->handler->g...ig['prefix'] . $sessID) returns the type string which is incompatible with the type-hinted return array.
Loading history...
87
    }
88
89
    /**
90
     * 写入Session
91
     * @access public
92
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @param  array  $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
94
     * @return bool
95
     */
96
    public function write(string $sessID, array $data): bool
97
    {
98
        if ($this->config['expire'] > 0) {
99
            $result = $this->handler->setex($this->config['prefix'] . $sessID, $this->config['expire'], $data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type string expected by parameter $value of Redis::setex(). ( Ignorable by Annotation )

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

99
            $result = $this->handler->setex($this->config['prefix'] . $sessID, $this->config['expire'], /** @scrutinizer ignore-type */ $data);
Loading history...
100
        } else {
101
            $result = $this->handler->set($this->config['prefix'] . $sessID, $data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type string expected by parameter $value of Redis::set(). ( Ignorable by Annotation )

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

101
            $result = $this->handler->set($this->config['prefix'] . $sessID, /** @scrutinizer ignore-type */ $data);
Loading history...
102
        }
103
104
        return $result ? true : false;
105
    }
106
107
    /**
108
     * 删除Session
109
     * @access public
110
     * @param  string $sessID
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
111
     * @return bool
112
     */
113
    public function delete(string $sessID): bool
114
    {
115
        return $this->handler->delete($this->config['prefix'] . $sessID) > 0;
116
    }
117
118
}
119