StaffRoleHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 115
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 11 2
A insertQuery() 0 11 2
A staffInRole() 0 10 2
A deleteQuery() 0 5 1
A getObjectsByStaff() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
26
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
27
28
/**
29
 * class StaffRoleHandler
30
 */
31
class StaffRoleHandler extends BaseObjectHandler
32
{
33
    public $idfield = 'roleid';
34
    /**
35
     * Name of child class
36
     *
37
     * @var string
38
     */
39
    public $classname = StaffRole::class;
40
    /**
41
     * DB Table Name
42
     *
43
     * @var string
44
     */
45
    public $dbtable = 'xhelp_staffroles';
46
47
    private const TABLE = 'xhelp_staffroles';
48
    private const ENTITY = StaffRole::class;
49
    private const ENTITYNAME = 'StaffRole';
50
    private const KEYNAME = 'uid';
51
    private const IDENTIFIER = 'roleid';
52
53
    /**
54
     * Constructor
55
     *
56
     * @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object
57
     */
58
    public function __construct(\XoopsMySQLDatabase $db = null)
59
    {
60
        $this->init($db);
61
        $this->helper = Helper::getInstance();
62
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
63
    }
64
65
    /**
66
     * @param int      $id
67
     * @param int|null $roleid
68
     * @param int|null $deptid
69
     * @return array|bool
70
     */
71
    public function get($id = null, $fields = null, int $roleid = null, int $deptid = null)
72
    {
73
        $criteria = new \CriteriaCompo(new \Criteria('uid', (string)$id));
74
        $criteria->add(new \Criteria('roleid', (string)$roleid));
75
        $criteria->add(new \Criteria('deptid', (string)$deptid));
76
77
        if (!$role = $this->getObjects($criteria)) {
78
            return false;
79
        }
80
81
        return $role;
82
    }
83
84
    /**
85
     * @param int  $uid
86
     * @param bool $id_as_key
87
     * @return array|bool
88
     */
89
    public function &getObjectsByStaff(int $uid, bool $id_as_key = false)
90
    {
91
        $uid      = $uid;
92
        $criteria = new \Criteria('uid', (string)$uid);
93
94
        $arr = $this->getObjects($criteria, $id_as_key);
95
96
        if (0 == \count($arr)) {
97
            $arr = false;
98
        }
99
100
        return $arr;
101
    }
102
103
    /**
104
     * @param int $uid
105
     * @param int $roleid
106
     * @return bool
107
     */
108
    public function staffInRole(int $uid, int $roleid): bool
109
    {
110
        $criteria = new \CriteriaCompo(new \Criteria('uid', $uid));
111
        $criteria->add(new \Criteria('roleid', $roleid));
112
113
        if (!$role = $this->getObjects($criteria)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
114
            return false;
115
        }
116
117
        return true;
118
    }
119
120
    /**
121
     * @param \XoopsObject $object
122
     * @return string
123
     */
124
    public function insertQuery(\XoopsObject $object): string
125
    {
126
        //TODO mb replace with individual variables
127
        // Copy all object vars into local variables
128
        foreach ($object->cleanVars as $k => $v) {
129
            ${$k} = $v;
130
        }
131
132
        $sql = \sprintf('INSERT INTO `%s` (uid, roleid, deptid) VALUES (%u, %u, %u)', $this->db->prefix($this->dbtable), $uid, $roleid, $deptid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $roleid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $deptid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
133
134
        return $sql;
135
    }
136
137
    /**
138
     * @param \XoopsObject $object
139
     * @return string
140
     */
141
    public function deleteQuery(\XoopsObject $object): string
142
    {
143
        $sql = \sprintf('DELETE FROM `%s` WHERE uid = %u', $this->db->prefix($this->dbtable), $object->getVar('uid'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('uid') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

143
        $sql = \sprintf('DELETE FROM `%s` WHERE uid = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar('uid'));
Loading history...
144
145
        return $sql;
146
    }
147
}   // end of handler class
148