Completed
Pull Request — master (#489)
by Richard
10:39
created

Handler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 162
Duplicated Lines 14.2 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%
Metric Value
wmc 11
lcom 1
cbo 3
dl 23
loc 162
ccs 0
cts 71
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A open() 0 4 1
A close() 0 4 1
B read() 0 25 3
B write() 0 38 3
A destroy() 11 11 1
A gc() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Core\Session;
13
14
use Xoops\Core\Database\Connection;
15
16
/**
17
 * Handler for database session storage
18
 *
19
 * @category  Xoops\Core\Session
20
 * @package   Handler
21
 * @author    Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
22
 * @author    Taiwen Jiang <[email protected]>
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @link      http://xoops.org
27
 */
28
class Handler implements \SessionHandlerInterface
29
{
30
    /**
31
     * @var \Xoops\Core\Database\Connection
32
     */
33
    private $db;
34
35
    /**
36
     * @var string $sessionTable
37
     */
38
    private $sessionTable = 'system_session';
39
40
    /**
41
     * Constructor
42
     */
43
    public function __construct()
44
    {
45
        $this->db = \Xoops::getInstance()->db();
46
    }
47
48
    /**
49
     * Open a session
50
     *
51
     * @param string $save_path not used
52
     * @param string $name      not used
53
     *
54
     * @return bool
55
     */
56
    public function open($save_path, $name)
57
    {
58
        return true;
59
    }
60
61
    /**
62
     * Close a session
63
     *
64
     * @return bool
65
     */
66
    public function close()
67
    {
68
        return true;
69
    }
70
71
    /**
72
     * Read a session from the database
73
     *
74
     * @param string $session_id Id of the session
75
     *
76
     * @return string Session data
77
     */
78
    public function read($session_id)
79
    {
80
        $qb = $this->db->createXoopsQueryBuilder();
81
        $eb = $qb->expr();
82
        $qb ->select('s.session_data')
83
            ->fromPrefix($this->sessionTable, 's')
84
            ->where($eb->eq('s.session_id', ':sessid'))
85
            ->andWhere($eb->gt('s.expires_at', ':expires'))
86
            ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
87
            ->setParameter(':expires', time(), \PDO::PARAM_INT);
88
89
        $session_data = '';
90
        if ($result = $qb->execute()) {
91
            if ($row = $result->fetch(\PDO::FETCH_NUM)) {
92
                list ($session_data) = $row;
93
            }
94
        }
95
96
        // if system is not configured for garbage collect, force it anyway
97
        //if ((ini_get('session.gc_probability') == 0) && (rand(1, 100) <= 5)) {
98
        //    $this->gc(0);
99
        //}
100
101
        return $session_data;
102
    }
103
104
    /**
105
     * Write a session to the database
106
     *
107
     * @param string $session_id   id of session
108
     * @param string $session_data data to store
109
     *
110
     * @return bool
111
     **/
112
    public function write($session_id, $session_data)
0 ignored issues
show
Coding Style introduced by
write uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114
        $expires =  (isset($_SESSION['SESSION_MANAGER_EXPIRES']))
115
            ? (int)($_SESSION['SESSION_MANAGER_EXPIRES'])
116
            : time() + (session_cache_expire() * 60);
117
        $oldIsolation = $this->db->getTransactionIsolation();
118
        $this->db->setTransactionIsolation(Connection::TRANSACTION_REPEATABLE_READ);
119
        $this->db->beginTransaction();
120
        $qb = $this->db->createXoopsQueryBuilder();
121
        $eb = $qb->expr();
122
        $qb ->updatePrefix($this->sessionTable)
123
            ->set('expires_at', ':expires')
124
            ->set('session_data', ':sessdata')
125
            ->where($eb->eq('session_id', ':sessid'))
126
            ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
127
            ->setParameter(':expires', $expires, \PDO::PARAM_INT)
128
            ->setParameter(':sessdata', $session_data, \PDO::PARAM_STR);
129
        $this->db->setForce(true);
130
        $result = $qb->execute();
131
        if ($result<=0) {
132
            $qb = $this->db->createXoopsQueryBuilder();
133
            $qb ->insertPrefix($this->sessionTable)
134
                ->values(array(
135
                    'session_id'   => ':sessid',
136
                    'expires_at'   => ':expires',
137
                    'session_data' => ':sessdata',
138
                    ))
139
                ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
140
                ->setParameter(':expires', $expires, \PDO::PARAM_INT)
141
                ->setParameter(':sessdata', $session_data, \PDO::PARAM_STR);
142
            $this->db->setForce(true);
143
            $result = $qb->execute();
144
        }
145
        $this->db->commit();
146
        $this->db->setTransactionIsolation($oldIsolation);
147
148
        return (boolean) ($result>0);
149
    }
150
151
    /**
152
     * Destroy a session
153
     *
154
     * @param string $session_id Id of session
155
     *
156
     * @return bool
157
     */
158 View Code Duplication
    public function destroy($session_id)
159
    {
160
        $qb = $this->db->createXoopsQueryBuilder();
161
        $eb = $qb->expr();
162
        $qb ->deletePrefix($this->sessionTable)
163
            ->where($eb->eq('session_id', ':sessid'))
164
            ->setParameter(':sessid', $session_id, \PDO::PARAM_STR);
165
        $this->db->setForce(true);
166
        $result = $qb->execute();
167
        return (boolean) ($result>0);
168
    }
169
170
    /**
171
     * Garbage Collector
172
     *
173
     * @param string $maxlifetime Time in seconds until a session expires
174
     *
175
     * @return bool
176
     */
177 View Code Duplication
    public function gc($maxlifetime)
178
    {
179
        $mintime = time();
180
        $qb = $this->db->createXoopsQueryBuilder();
181
        $eb = $qb->expr();
182
        $qb ->deletePrefix($this->sessionTable)
183
            ->where($eb->lt('expires_at', ':expires'))
184
            ->setParameter(':expires', $mintime, \PDO::PARAM_INT);
185
        $this->db->setForce(true);
186
        $result = $qb->execute();
187
        return (boolean) ($result>0);
188
    }
189
}
190