UserstatsHandler::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 */
13
\defined('NEWBB_FUNCTIONS_INI') || require $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
14
15
/**
16
 * user stats
17
 */
18
class UserstatsHandler extends \XoopsPersistableObjectHandler
19
{
20
    /**
21
     * @param \XoopsDatabase|null $db
22
     */
23
    public function __construct(\XoopsDatabase $db = null)
24
    {
25
        parent::__construct($db, 'newbb_user_stats', Userstats::class, 'uid', '');
26
    }
27
28
    /**
29
     * @param \XoopsDatabase|null $db
30
     * @return UserstatsHandler
31
     */
32
    public static function getInstance(\XoopsDatabase $db = null): UserstatsHandler
33
    {
34
        static $instance;
35
        if (null === $instance) {
36
            $instance = new static($db);
37
        }
38
39
        return $instance;
40
    }
41
42
    /**
43
     * @param mixed $id
44
     * @param array|null  $fields
45
     * @return null|\XoopsObject
46
     */
47
    public function get($id = null, $fields = null): ?\XoopsObject //get($id)
48
    {
49
        $object = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
50
        if (!$id = (int)$id) {
51
            return null;
52
        }
53
        $object = $this->create(false);
54
        $object->setVar($this->keyName, $id);
55
        if (!$row = $this->getStats($id)) {
56
            return $object;
57
        }
58
        $object->assignVars($row);
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type true; however, parameter $var_arr of XoopsObject::assignVars() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $object->assignVars(/** @scrutinizer ignore-type */ $row);
Loading history...
59
60
        /*
61
        $sql = "SELECT * FROM " . $this->table . " WHERE ".$this->keyName." = " . $id;
62
        if (!$result = $this->db->query($sql)) {
63
            return $object;
64
        }
65
        while (false !== ($row = $this->db->fetchArray($result))) {
66
            $object->assignVars($row);
67
        }
68
        */
69
70
        return $object;
71
    }
72
73
    /**
74
     * @param int $id
75
     * @return mixed[]|bool
76
     */
77
    public function getStats(int $id)
78
    {
79
        if (empty($id)) {
80
            return null;
81
        }
82
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->keyName . ' = ' . (int)$id;
83
        $result = $this->db->query($sql);
84
        if (!$this->db->isResultSet($result)) {
85
            return null;
86
        }
87
88
        $row = $this->db->fetchArray($result);
89
90
        return $row;
91
    }
92
    /*
93
        function insert(\XoopsObject $object, $force = true)
94
        {
95
            if (!$object->isDirty()) {
96
                $object->setErrors("not isDirty");
97
98
                return $object->getVar($this->keyName);
99
            }
100
            $this->loadHandler("write");
101
            if (!$changedVars = $this->_handler["write"]->cleanVars($object)) {
102
                $object->setErrors("cleanVars failed");
103
104
                return $object->getVar($this->keyName);
105
            }
106
            $queryFunc = empty($force) ? "query" : "queryF";
107
108
            $keys = [];
109
            foreach ($changedVars as $k => $v) {
110
                $keys[] = " {$k} = {$v}";
111
            }
112
            $sql = "REPLACE INTO " . $this->table . " SET ".implode(",",$keys);
113
            if (!$result = $this->db->{$queryFunc}($sql)) {
114
                $object->setErrors("update object error:" . $sql);
115
116
                return false;
117
            }
118
            unset($changedVars);
119
120
            return $object->getVar($this->keyName);
121
        }
122
    */
123
}
124