MysqlPdoSessionHandler::gc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
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 Linna\Storage\ExtendedPDO;
15
use SessionHandlerInterface;
16
17
/**
18
 * Store sessions in Database.
19
 *
20
 * Check below link for PHP session Handler
21
 * http://php.net/manual/en/class.sessionhandler.php
22
 *
23
 * Before use create table session on DB.
24
 *
25
 *   CREATE TABLE `session` (
26
 *     `session_id` char(128) NOT NULL,
27
 *     `session_data` varchar(3096) NOT NULL,
28
 *     `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
29
 *     `last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
30
 *     PRIMARY KEY (`session_id`)
31
 *   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
32
 */
33
class MysqlPdoSessionHandler implements SessionHandlerInterface
34
{
35
    /**
36
     * @var ExtendedPDO Database Connection
37
     */
38
    protected ExtendedPDO $pdo;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param ExtendedPDO $pdo
44
     */
45 7
    public function __construct(ExtendedPDO $pdo)
46
    {
47 7
        $this->pdo = $pdo;
48 7
    }
49
50
    /**
51
     * Open session storage.
52
     *
53
     * http://php.net/manual/en/sessionhandler.open.php.
54
     *
55
     * @param string $save_path
56
     * @param string $session_name
57
     *
58
     * @return bool
59
     */
60 5
    public function open($save_path, $session_name)
61
    {
62 5
        unset($save_path, $session_name);
63
64 5
        return true;
65
    }
66
67
    /**
68
     * Delete old sessions from storage.
69
     *
70
     * http://php.net/manual/en/sessionhandler.gc.php.
71
     *
72
     * @param int $maxlifetime
73
     *
74
     * @return bool
75
     */
76 1
    public function gc($maxlifetime)
77
    {
78 1
        $this->pdo->queryWithParam(
79 1
            'DELETE FROM session WHERE last_update < DATE_SUB(NOW(), INTERVAL :maxlifetime SECOND)',
80 1
            [[':maxlifetime', $maxlifetime, \PDO::PARAM_INT]]
81
        );
82
83 1
        return $this->pdo->getLastOperationStatus();
84
    }
85
86
    /**
87
     * Read session data from storage.
88
     *
89
     * http://php.net/manual/en/sessionhandler.read.php.
90
     *
91
     * @param string $session_id
92
     *
93
     * @return string
94
     */
95 5
    public function read($session_id)
96
    {
97
        //string casting is a fix for PHP 7
98
        //when strict type are enable
99 5
        return (string) $this->pdo->queryWithParam(
100 5
            'SELECT session_data FROM session WHERE session_id = :session_id',
101 5
            [[':session_id', $session_id, \PDO::PARAM_STR]]
102 5
        )->fetchColumn();
103
    }
104
105
    /**
106
     * Write session data to storage.
107
     *
108
     * http://php.net/manual/en/sessionhandler.write.php.
109
     *
110
     * @param string $session_id
111
     * @param string $session_data
112
     *
113
     * @return bool
114
     */
115 4
    public function write($session_id, $session_data)
116
    {
117 4
        $this->pdo->queryWithParam(
118 4
            'INSERT INTO session SET session_id = :session_id, session_data = :session_data ON DUPLICATE KEY UPDATE session_data = :session_data',
119
            [
120 4
                [':session_id', $session_id, \PDO::PARAM_STR],
121 4
                [':session_data', $session_data, \PDO::PARAM_STR]
122
            ]
123
        );
124
125 4
        return $this->pdo->getLastOperationStatus();
126
    }
127
128
    /**
129
     * Close session.
130
     *
131
     * http://php.net/manual/en/sessionhandler.close.php.
132
     *
133
     * @return bool
134
     */
135 5
    public function close()
136
    {
137 5
        return true;
138
    }
139
140
    /**
141
     * Destroy session data.
142
     *
143
     * http://php.net/manual/en/sessionhandler.destroy.php.
144
     *
145
     * @param string $session_id
146
     *
147
     * @return bool
148
     */
149 5
    public function destroy($session_id)
150
    {
151 5
        $this->pdo->queryWithParam(
152 5
            'DELETE FROM session WHERE session_id = :session_id',
153 5
            [[':session_id', $session_id, \PDO::PARAM_STR]]
154
        );
155
156 5
        return $this->pdo->getLastOperationStatus();
157
    }
158
}
159