ItemRedisDriver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 2
b 0
f 0
dl 0
loc 49
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 40 6
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
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Firewall\Driver;
24
25
use Shieldon\Firewall\Driver\RedisDriver;
26
use Redis;
27
use Exception;
28
29
/**
30
 * Get Redis driver.
31
 */
32
class ItemRedisDriver
33
{
34
    /**
35
     * Initialize and get the instance.
36
     *
37
     * @param array $setting The configuration of that driver.
38
     *
39
     * @return RedisDriver|null
40
     */
41 3
    public static function get(array $setting)
42
    {
43 3
        $instance = null;
44
45
        try {
46
            $host = '127.0.0.1';
47 3
            $port = 6379;
48 3
49
            if (!empty($setting['host'])) {
50 3
                $host = $setting['host'];
51 2
            }
52
53
            if (!empty($setting['port'])) {
54 3
                $port = $setting['port'];
55 2
            }
56
57
            // Create a Redis instance.
58
            $redis = new Redis();
59 3
            if (empty($setting['port'])) {
60 3
                $redis->connect($host);
61 1
            } else {
62
                $redis->connect($host, $port);
63 2
            }
64
65
            if (!empty($setting['auth'])) {
66 2
                // @codeCoverageIgnoreStart
67
                $redis->auth($setting['auth']);
68
                // @codeCoverageIgnoreEnd
69
            }
70
71
            // Use Redis data driver.
72
            $instance = new RedisDriver($redis);
73 2
74
            // @codeCoverageIgnoreStart
75
        } catch (Exception $e) {
76
            echo $e->getMessage();
77
        }
78
        // @codeCoverageIgnoreEnd
79
80
        return $instance;
81
    }
82
}
83