Completed
Pull Request — master (#628)
by Richard
19:02 queued 04:58
created

XoopsOnlineHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 88.1%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 98
rs 10
c 0
b 0
f 0
ccs 37
cts 42
cp 0.881
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 7 2
A gc() 0 7 2
A write() 0 35 5
A __construct() 0 8 1
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       XOOPS Project (http://xoops.org)
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package         kernel
15
 * @since           2.0.0
16
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 * @version         $Id$
18
 */
19
20
namespace Xoops\Core\Kernel\Handlers;
21
22
use Xoops\Core\Database\Connection;
23
use Xoops\Core\Kernel\Criteria;
24
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
25
26
/**
27
 * A handler for "Who is Online?" information
28
 *
29
 * @category  Xoops\Core\Kernel\Handlers\XoopsOnlineHandler
30
 * @package   Xoops\Core\Kernel
31
 * @author    Kazumi Ono <[email protected]>
32
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
33
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34
 * @link      http://xoops.org
35
 */
36
class XoopsOnlineHandler extends XoopsPersistableObjectHandler
37
{
38
    /**
39
     * Constructor
40
     *
41
     * @param Connection|null $db database
42
     */
43 8
    public function __construct(Connection $db = null)
44
    {
45 8
        parent::__construct(
46 8
            $db,
47 8
            'system_online',
48 8
            '\Xoops\Core\Kernel\Handlers\XoopsOnline',
49 8
            'online_uid',
50 8
            'online_uname'
51
        );
52 8
    }
53
54
    /**
55
     * Write online information to the database
56
     *
57
     * @param int    $uid    UID of the active user
58
     * @param string $uname  Username
59
     * @param string $time   time
60
     * @param string $module Current module
61
     * @param string $ip     User's IP address
62
     *
63
     * @return bool TRUE on success
64
     */
65 1
    public function write($uid, $uname, $time, $module, $ip)
66
    {
67 1
        $criteria = array();
68 1
        $criteria['online_uid'] = $uid;
69 1
        if ($uid == 0) {
70
            $criteria['online_ip'] = $ip;
71
        }
72 1
        $rows = $this->db2->updatePrefix(
73 1
            'system_online',
74
            array(
75 1
               'online_uname'   => $uname,
76 1
               'online_updated' => $time,
77 1
               'online_module'  => $module,
78
            ),
79 1
            $criteria
80
        );
81 1
        if ($rows === false) {
0 ignored issues
show
introduced by
The condition $rows === false is always false.
Loading history...
82
            return false;
83
        }
84 1
        if ($rows == 0) {
85 1
            $rows = $this->db2->insertPrefix(
86 1
                'system_online',
87
                array(
88 1
                    'online_uid'     => $uid,
89 1
                    'online_uname'   => $uname,
90 1
                    'online_updated' => $time,
91 1
                    'online_ip'      => $ip,
92 1
                    'online_module'  => $module,
93
                )
94
            );
95
        }
96 1
        if ($rows === false) {
0 ignored issues
show
introduced by
The condition $rows === false is always false.
Loading history...
97
            return false;
98
        }
99 1
        return ($rows>0);
100
    }
101
102
    /**
103
     * Delete online information for a user
104
     *
105
     * @param int $uid UID
106
     *
107
     * @return bool TRUE on success
108
     */
109 1
    public function destroy($uid)
110
    {
111 1
        $criteria = new Criteria('online_uid', (int)($uid));
112 1
        if (false === $this->deleteAll($criteria)) {
113
            return false;
114
        }
115 1
        return true;
116
    }
117
118
    /**
119
     * Garbage Collection
120
     *
121
     * Delete all online information that has not been updated for a certain time
122
     *
123
     * @param int $expire Expiration time in seconds
124
     *
125
     * @return bool
126
     */
127 1
    public function gc($expire)
128
    {
129 1
        $criteria = new Criteria('online_updated', time() - (int)($expire), '<');
130 1
        if (false === $this->deleteAll($criteria)) {
131
            return false;
132
        }
133 1
        return true;
134
    }
135
}
136