RedisDriver::createFileName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OpenOauth\Core\DatabaseDriver;
4
5
use OpenOauth\Core\DatabaseDriver\BaseDriver;
6
use OpenOauth\Core\Config;
7
use Predis\Client;
8
9
/**
10
 * 文件缓存驱动.
11
 *
12
 */
13
class RedisDriver extends BaseDriver
14
{
15
    private $redis;
16
    private $pre;
17
18 View Code Duplication
    public function __construct($configs = [], $dir = 'Database:OpenOauth:')
0 ignored issues
show
Duplication introduced by
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...
19
    {
20
        parent::__construct($dir);
21
22
        $this->pre = $this->databaseDir;
23
24
        $config = $configs + ['host' => '127.0.0.1', 'port' => '6379', 'database' => '0', 'scheme' => 'tcp', 'auth' => null];
25
26
        ini_set('default_socket_timeout', 50);//socket连接超时时间;
27
28
        $this->redis = new Client(
29
            [
30
                'scheme' => $config['scheme'],
31
                'host'   => $config['host'],
32
                'port'   => $config['port'],
33
            ]);
34
35
        if (!empty($config['auth'])) {
36
            $this->redis->auth($config['auth']);
37
        }
38
39
        $this->redis->select($config['database']);
40
41
        if (!$this->redis) {
42
            exit('Redis初始化连接失败-database');
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
43
        }
44
    }
45
46
    /**
47
     * 根据缓存名获取缓存内容.
48
     *
49
     * @param string $name
50
     *
51
     * @return bool|mixed|string
52
     */
53 View Code Duplication
    public function _get($name)
0 ignored issues
show
Duplication introduced by
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...
54
    {
55
        $name = $this->createFileName($name);
56
        $data = $this->redis->get($name);
57
        if ($data) {
58
            $data = $this->unpackData($data);
59
        }
60
61
        return $data;
62
    }
63
64
    /**
65
     * 根据缓存名 设置缓存值和超时时间.
66
     *
67
     * @param string $name  缓存名
68
     * @param void   $value 缓存值
69
     *
70
     * @return boolean;
0 ignored issues
show
Documentation introduced by
The doc-type boolean; could not be parsed: Expected "|" or "end of type", but got ";" at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
     */
72
    public function _set($name, $value)
73
    {
74
        $name = $this->createFileName($name);
75
        $data = $this->packData($value);
76
77
        return $this->redis->set($name, $data);
78
    }
79
80
    /**
81
     * 数据打包.
82
     *
83
     * @param void $data    缓存值
84
     * @param int  $expires 超时时间
0 ignored issues
show
Bug introduced by
There is no parameter named $expires. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
     *
86
     * @return string
87
     */
88
    private function packData($data)
89
    {
90
        return serialize($data);
91
    }
92
93
    /**
94
     * 数据解包.
95
     *
96
     * @param $data
97
     *
98
     * @return mixed
99
     */
100
    private function unpackData($data)
101
    {
102
        return unserialize($data);
103
    }
104
105
    /**
106
     * 创建缓存文件名.
107
     *
108
     * @param string $name 缓存名
109
     *
110
     * @return string
111
     */
112
    private function createFileName($name)
113
    {
114
        return $this->pre . md5($name);
115
    }
116
}
117