Passed
Push — 2.x ( e966a7...a2237c )
by Terry
02:13
created

ItemRedisDriver::get()   A

Complexity

Conditions 5
Paths 38

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 38
nop 1
dl 0
loc 39
rs 9.4222
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Firewall\Driver;
14
15
use Shieldon\Firewall\Driver\RedisDriver;
16
use Redis;
17
use RedisException;
18
19
/**
20
 * Get Redis driver.
21
 */
22
class ItemRedisDriver
23
{
24
    /**
25
     * Initialize and get the instance.
26
     *
27
     * @param array $setting The configuration of that driver.
28
     *
29
     * @return RedisDriver|null
30
     */
31
    public static function get(array $setting)
32
    {
33
        $instance = null;
34
35
        try {
36
37
            $host = '127.0.0.1';
38
            $port = 6379;
39
40
            if (!empty($setting['host'])) {
41
                $host = $setting['host'];
42
            }
43
44
            if (!empty($setting['port'])) {
45
                $port = $setting['port'];
46
            }
47
48
            // Create a Redis instance.
49
            $redis = new Redis();
50
            $redis->connect($host, $port);
51
52
            if (!empty($setting['auth'])) {
53
                // @codeCoverageIgnoreStart
54
                $redis->auth($setting['auth']);
55
                // @codeCoverageIgnoreEnd
56
            }
57
58
            // Use Redis data driver.
59
            $instance = new RedisDriver($redis);
60
61
        // @codeCoverageIgnoreStart
62
63
        } catch(RedisException $e) {
64
            echo $e->getMessage();
65
        }
66
67
        // @codeCoverageIgnoreEnd
68
69
        return $instance;
70
    }
71
}