Completed
Push β€” master ( bd84b9...b5be07 )
by zacksleo
05:55 queued 04:45
created

Predis::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: zjw
5
 * Date: 2017/8/25
6
 * Time: δΈ‹εˆ3:45
7
 */
8
9
namespace zacksleo\yii2\oauth2\common\helpers;
10
11
use monsterhunter\oauth2\redis\RedisClient;
12
13
class Predis
14
{
15
    private static $instance;
16
17
    private static $clientSet;
18
19
    /**
20
     * @return Predis
21
     */
22
    public static function getInstance()
23
    {
24
        if (!isset(self::$instance)) {
25
            self::$instance = new self();
26
        }
27
        return self::$instance;
28
    }
29
30
    /**
31
     * @return \monsterhunter\oauth2\redis\RedisClient
32
     */
33
    public function getClient()
34
    {
35
        if (!isset(self::$clientSet['PredisClient'])) {
36
            $client = new RedisClient($this->getParams());
37
            self::$clientSet['PredisClient'] = $client;
38
        }
39
        return self::$clientSet['PredisClient'];
40
    }
41
42
    private function getParams()
43
    {
44
        $params = [
45
            'host' => getenv('TOKEN_REDIS_HOST'),
46
            'port' => getenv('TOKEN_REDIS_PORT'),
47
            'database' => getenv('TOKEN_REDIS_DATABASE'),
48
            'password' => getenv('TOKEN_REDIS_PASSWORD')
49
        ];
50
        if ($params['password'] == 'null' || empty($params['password'])) {
51
            unset($params['password']);
52
        }
53
        return $params;
54
    }
55
}
56