Completed
Push — master ( 10a28c...325b74 )
by Anton
13s
created

Redis::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Session\Adapter;
12
13
use Bluz\Common\Exception\ComponentException;
14
use Bluz\Common\Exception\ConfigurationException;
15
16
/**
17
 * Redis session handler
18
 *
19
 * @package Bluz\Session\Adapter
20
 */
21
class Redis extends AbstractAdapter implements \SessionHandlerInterface
22
{
23
    /**
24
     * @var array default Redis settings
25
     */
26
    protected $settings = [
27
        'host' => '127.0.0.1',
28
        'port' => 6379,
29
        'timeout' => 0,
30
        'persistence' => false,
31
    ];
32
33
    /**
34
     * Check and setup Redis server
35
     *
36
     * @param  array $settings
37
     *
38
     * @throws ComponentException
39
     * @throws ConfigurationException
40
     */
41
    public function __construct($settings = [])
42
    {
43
        // check Redis extension
44
        if (!extension_loaded('redis')) {
45
            throw new ComponentException(
46
                'Redis extension not installed/enabled.
47
                Install and/or enable Redis extension [http://pecl.php.net/package/redis].
48
                See phpinfo() for more information'
49
            );
50
        }
51
52
        // check Redis settings
53
        if (!is_array($settings) || empty($settings)) {
54
            throw new ConfigurationException(
55
                'Redis configuration is missed. Please check `session` configuration section'
56
            );
57
        }
58
59
        // Update settings
60
        $this->settings = array_replace_recursive($this->settings, $settings);
61
    }
62
63
    /**
64
     * Initialize session
65
     *
66
     * @param  string $savePath
67
     * @param  string $sessionName
68
     *
69
     * @return bool
70
     */
71
    public function open($savePath, $sessionName)
72
    {
73
        parent::open($savePath, $sessionName);
74
75
        $this->handler = new \Redis();
76
77
        if ($this->settings['persistence']) {
78
            $this->handler->pconnect($this->settings['host'], $this->settings['port'], $this->settings['timeout']);
79
        } else {
80
            $this->handler->connect($this->settings['host'], $this->settings['port'], $this->settings['timeout']);
81
        }
82
83
        if (isset($this->settings['options'])) {
84
            foreach ($this->settings['options'] as $key => $value) {
85
                $this->handler->setOption($key, $value);
86
            }
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * Read session data
94
     *
95
     * @param  string $id
96
     *
97
     * @return string
98
     */
99
    public function read($id)
100
    {
101
        return $this->handler->get($this->prepareId($id)) ?: '';
102
    }
103
104
    /**
105
     * Write session data
106
     *
107
     * @param  string $id
108
     * @param  string $data
109
     *
110
     * @return bool
111
     */
112
    public function write($id, $data)
113
    {
114
        return $this->handler->set($this->prepareId($id), $data, (int)$this->ttl);
115
    }
116
117
    /**
118
     * Destroy a session
119
     *
120
     * @param  integer $id
121
     *
122
     * @return bool
123
     */
124
    public function destroy($id)
125
    {
126
        $this->handler->del($this->prepareId($id));
127
        return true;
128
    }
129
}
130