Failed Conditions
Push — master ( 5591cb...4c31fe )
by Florian
02:12
created

PdoHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 67.74%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 182
ccs 21
cts 31
cp 0.6774
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareStatement() 0 8 2
A __construct() 0 3 1
A write() 0 6 1
A close() 0 3 1
A destroy() 0 6 1
A read() 0 10 1
A gc() 0 3 1
A open() 0 3 1
A setTable() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phauthentic\Infrastructure\Http\Session\Handler;
6
7
use PDO;
8
use PDOStatement;
9
use RuntimeException;
10
use SessionHandlerInterface;
11
12
/**
13
 * Generic PDO Session Handler
14
 */
15
class PdoHandler implements SessionHandlerInterface
16
{
17
    /**
18
     * PDO Object
19
     *
20
     * @var \PDO
21
     */
22
    protected $pdo;
23
24
    /**
25
     * Session table name
26
     *
27
     * @var string
28
     */
29
    protected $table = 'sessions';
30
31
    /**
32
     * Session table field map
33
     *
34
     * @var array
35
     */
36
    protected $fieldMap = [
37
        'sid' => 'id',
38
        'expires' => 'expires',
39
        'data' => 'data'
40
    ];
41
42
    /**
43
     * Constructor
44
     *
45
     * @param \PDO $pdo PDO Object
46
     */
47 1
    public function __construct(PDO $pdo)
48
    {
49 1
        $this->pdo = $pdo;
50 1
    }
51
52
    /**
53
     * Sets the table name
54
     *
55
     * @param string $table Table name
56
     * @return void
57
     */
58
    public function setTable(string $table)
59
    {
60
        $this->table = $table;
61
    }
62
63
    /**
64
     * Close the session
65
     *
66
     * @link http://php.net/manual/en/sessionhandlerinterface.close.php
67
     * @return bool <p>
68
     * The return value (usually TRUE on success, FALSE on failure).
69
     * Note this value is returned internally to PHP for processing.
70
     * </p>
71
     * @since 5.4.0
72
     */
73
    public function close()
74
    {
75
        return true;
76
    }
77
78
    /**
79
     * Destroy a session
80
     *
81
     * @link http://php.net/manual/en/sessionhandlerinterface.destroy.php
82
     * @param string $session_id The session ID being destroyed.
83
     * @return bool <p>
84
     * The return value (usually TRUE on success, FALSE on failure).
85
     * Note this value is returned internally to PHP for processing.
86
     * </p>
87
     * @since 5.4.0
88
     */
89 1
    public function destroy($session_id)
90
    {
91 1
        $statement = $this->prepareStatement("DELETE FROM {$this->table} WHERE id = :sid");
92
93 1
        return $statement->execute([
94 1
            'sid' => $session_id
95
        ]);
96
    }
97
98
    /**
99
     * Cleanup old sessions
100
     *
101
     * @link http://php.net/manual/en/sessionhandlerinterface.gc.php
102
     * @param int $maxlifetime <p>
103
     * Sessions that have not updated for
104
     * the last maxlifetime seconds will be removed.
105
     * </p>
106
     * @return bool <p>
107
     * The return value (usually TRUE on success, FALSE on failure).
108
     * Note this value is returned internally to PHP for processing.
109
     * </p>
110
     * @since 5.4.0
111
     */
112
    public function gc($maxlifetime)
113
    {
114
        return true;
115
    }
116
117
    /**
118
     * Initialize session
119
     *
120
     * @link http://php.net/manual/en/sessionhandlerinterface.open.php
121
     * @param string $save_path The path where to store/retrieve the session.
122
     * @param string $name The session name.
123
     * @return bool <p>
124
     * The return value (usually TRUE on success, FALSE on failure).
125
     * Note this value is returned internally to PHP for processing.
126
     * </p>
127
     * @since 5.4.0
128
     */
129
    public function open($save_path, $name)
130
    {
131
        return true;
132
    }
133
134
    /**
135
     * Read session data
136
     *
137
     * @link http://php.net/manual/en/sessionhandlerinterface.read.php
138
     * @param string $session_id The session id to read data for.
139
     * @return string <p>
140
     * Returns an encoded string of the read data.
141
     * If nothing was read, it must return an empty string.
142
     * Note this value is returned internally to PHP for processing.
143
     * </p>
144
     * @since 5.4.0
145
     */
146 1
    public function read($session_id)
147
    {
148 1
        $statement = $this->prepareStatement("SELECT * FROM {$this->table} WHERE id = :sid");
149 1
        $statement->execute([
150 1
            'sid' => $session_id
151
        ]);
152
153 1
        $result = $statement->fetch(PDO::FETCH_ASSOC);
154
155 1
        return $result['data'] ?? '';
156
    }
157
158
    /**
159
     * Write session data
160
     *
161
     * @link http://php.net/manual/en/sessionhandlerinterface.write.php
162
     * @param string $session_id The session id.
163
     * @param string $session_data <p>
164
     * The encoded session data. This data is the
165
     * result of the PHP internally encoding
166
     * the $_SESSION superglobal to a serialized
167
     * string and passing it as this parameter.
168
     * Please note sessions use an alternative serialization method.
169
     * </p>
170
     * @return bool <p>
171
     * The return value (usually TRUE on success, FALSE on failure).
172
     * Note this value is returned internally to PHP for processing.
173
     * </p>
174
     * @since 5.4.0
175
     */
176 1
    public function write($session_id, $session_data)
177
    {
178 1
        $statement = $this->pdo->prepare("INSERT INTO {$this->table} (id, data, expires) VALUES (?,?,?)");
179
180 1
        return $statement->execute([
181 1
            $session_id, $session_data, time()
182
        ]);
183
    }
184
185
    /**
186
     * @param string $sql
187
     * @return bool|\PDOStatement
188
     */
189 1
    protected function prepareStatement(string $sql)
190
    {
191 1
        $statement = $this->pdo->prepare($sql);
192 1
        if (!$statement instanceof PDOStatement) {
193
            throw new RuntimeException($this->pdo->errorInfo()[2]);
194
        }
195
196 1
        return $statement;
197
    }
198
}
199