Test Failed
Push — b0.27.0 ( c18288 )
by Sebastian
08:24 queued 03:50
created

PgsqlPdoSessionHandler::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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