1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
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 <contact@terryl.in>
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

47 3
            $host = '127.0.0.1';
48 3
            $port = 6379;
49

50 3
            if (!empty($setting['host'])) {
51 3
                $host = $setting['host'];
52
            }
53

54 3
            if (!empty($setting['port'])) {
55 3
                $port = $setting['port'];
56
            }
57

58
            // Create a Redis instance.
59 3
            $redis = new Redis();
60 3
            $redis->connect($host, $port);
61

62 3
            if (!empty($setting['auth'])) {
63
                // @codeCoverageIgnoreStart
64
                $redis->auth($setting['auth']);
65
                // @codeCoverageIgnoreEnd
66
            }
67

68
            // Use Redis data driver.
69 3
            $instance = new RedisDriver($redis);
70

71
            // @codeCoverageIgnoreStart
72

73
        } catch (Exception $e) {
74
            // echo $e->getMessage();
75
        }
76

77
        // @codeCoverageIgnoreEnd
78

79 3
        return $instance;
80
    }
81
}

Read our documentation on viewing source code .

Loading