Completed
Push — b0.27.0 ( c529b3...34cc26 )
by Sebastian
05:07
created

MemcachedSessionHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Session;
13
14
use Memcached;
15
use SessionHandlerInterface;
16
17
/**
18
 * Store sessions in Memcached.
19
 *
20
 * Check below link for PHP session Handler
21
 * http://php.net/manual/en/class.sessionhandler.php
22
 */
23
class MemcachedSessionHandler implements SessionHandlerInterface
24
{
25
    /**
26
     * @var Memcached Memcached instance
27
     */
28
    private Memcached $memcached;
29
30
    /**
31
     * @var int Expire time in seconds for stored sessions
32
     */
33
    private int $expire = 0;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param Memcached $memcached Memcached resource
39
     * @param int       $expire    Expire time in seconds for stored sessions
40
     */
41 7
    public function __construct(Memcached $memcached, int $expire)
42
    {
43 7
        $this->memcached = $memcached;
44 7
        $this->expire = $expire;
45 7
    }
46
47
    /**
48
     * Open session storage.
49
     *
50
     * http://php.net/manual/en/sessionhandler.open.php.
51
     *
52
     * @param string $save_path
53
     * @param string $session_name
54
     *
55
     * @return bool
56
     */
57 5
    public function open($save_path, $session_name)
58
    {
59 5
        unset($save_path, $session_name);
60
61 5
        return true;
62
    }
63
64
    /**
65
     * Delete old sessions from storage.
66
     *
67
     * http://php.net/manual/en/sessionhandler.gc.php.
68
     *
69
     * @param int $maxlifetime
70
     *
71
     * @return bool
72
     */
73 1
    public function gc($maxlifetime)
74
    {
75 1
        unset($maxlifetime);
76
77
        //this method is no needed because all object stored expire without external operation
78 1
        return true;
79
    }
80
81
    /**
82
     * Read sessio data from storage.
83
     *
84
     * http://php.net/manual/en/sessionhandler.read.php.
85
     *
86
     * @param string $session_id
87
     *
88
     * @return string
89
     */
90 5
    public function read($session_id)
91
    {
92
        //fix for php7
93 5
        return (string) $this->memcached->get($session_id);
94
    }
95
96
    /**
97
     * Write session data to storage.
98
     *
99
     * http://php.net/manual/en/sessionhandler.write.php.
100
     *
101
     * @param string $session_id
102
     * @param string $session_data
103
     *
104
     * @return bool
105
     */
106 3
    public function write($session_id, $session_data)
107
    {
108 3
        return $this->memcached->set($session_id, $session_data, $this->expire);
109
    }
110
111
    /**
112
     * Close session.
113
     *
114
     * http://php.net/manual/en/sessionhandler.close.php.
115
     *
116
     * @return bool
117
     */
118 5
    public function close()
119
    {
120 5
        return true;
121
    }
122
123
    /**
124
     * Destroy session data.
125
     *
126
     * http://php.net/manual/en/sessionhandler.destroy.php.
127
     *
128
     * @param string $session_id
129
     *
130
     * @return bool
131
     */
132 5
    public function destroy($session_id)
133
    {
134 5
        if ($this->memcached->delete($session_id)) {
135 1
            return true;
136
        }
137
138 4
        if ($this->memcached->getResultCode() === 16) {
139 4
            return true;
140
        }
141
142
        return false;
143
    }
144
}
145