ResponseHandler::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 10
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
 * ResponseHandler class
30
 *
31
 * Response Handler for Response class
32
 *
33
 * @author  Eric Juden <[email protected]> &
34
 */
35
class ResponseHandler extends BaseObjectHandler
36
{
37
    /**
38
     * Name of child class
39
     *
40
     * @var string
41
     */
42
    public $classname = Response::class;
43
    /**
44
     * DB table name
45
     *
46
     * @var string
47
     */
48
    public $dbtable = 'xhelp_responses';
49
50
    private const TABLE = 'xhelp_responses';
51
    private const ENTITY = Response::class;
52
    private const ENTITYNAME = 'Response';
53
    private const KEYNAME = 'id';
54
    private const IDENTIFIER = 'id';
55
56
    /**
57
     * Constructor
58
     *
59
     * @param \XoopsMySQLDatabase|null $db reference to a xoopsDB object
60
     */
61
    public function __construct(\XoopsMySQLDatabase $db = null)
62
    {
63
        $this->init($db);
64
        $this->helper = Helper::getInstance();
65
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
66
    }
67
68
    /**
69
     * @param \XoopsObject $object
70
     * @return string
71
     */
72
    public function insertQuery(\XoopsObject $object): string
73
    {
74
        //TODO mb replace with individual variables
75
        // Copy all object vars into local variables
76
        foreach ($object->cleanVars as $k => $v) {
77
            ${$k} = $v;
78
        }
79
80
        $sql = \sprintf(
81
            'INSERT INTO `%s` (id, uid, ticketid, message, timeSpent, updateTime, userIP, private)
82
            VALUES (%u, %u, %u, %s, %u, %u, %s, %u)',
83
            $this->db->prefix($this->dbtable),
84
            $id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
85
            $uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
86
            $ticketid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticketid seems to be never defined.
Loading history...
87
            $this->db->quoteString($message),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message seems to be never defined.
Loading history...
88
            $timeSpent,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $timeSpent seems to be never defined.
Loading history...
89
            \time(),
90
            $this->db->quoteString($userIP),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userIP seems to be never defined.
Loading history...
91
            $private
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $private seems to be never defined.
Loading history...
92
        );
93
94
        return $sql;
95
    }
96
97
    /**
98
     * @param \XoopsObject $object
99
     * @return string
100
     */
101
    public function updateQuery(\XoopsObject $object): string
102
    {
103
        //TODO mb replace with individual variables
104
        // Copy all object vars into local variables
105
        foreach ($object->cleanVars as $k => $v) {
106
            ${$k} = $v;
107
        }
108
109
        $sql = \sprintf(
110
            'UPDATE `%s` SET uid = %u, ticketid = %u, message = %s, timeSpent = %u,
111
            updateTime = %u, userIP = %s, private = %u WHERE id = %u',
112
            $this->db->prefix($this->dbtable),
113
            $uid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
114
            $ticketid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ticketid seems to be never defined.
Loading history...
115
            $this->db->quoteString($message),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message seems to be never defined.
Loading history...
116
            $timeSpent,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $timeSpent seems to be never defined.
Loading history...
117
            \time(),
118
            $this->db->quoteString($userIP),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userIP seems to be never defined.
Loading history...
119
            $private,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $private seems to be never defined.
Loading history...
120
            $id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
121
        );
122
123
        return $sql;
124
    }
125
126
    /**
127
     * @param \XoopsObject $object
128
     * @return string
129
     */
130
    public function deleteQuery(\XoopsObject $object): string
131
    {
132
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $object->getVar('id'));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('id') 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

132
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar('id'));
Loading history...
133
134
        return $sql;
135
    }
136
137
    /**
138
     * delete a response from the database
139
     *
140
     * @param \XoopsObject $object    reference to the {@link xhelpResponse}
141
     *                                obj to delete
142
     * @param bool         $force
143
     * @return bool FALSE if failed.
144
     */
145
    public function delete(\XoopsObject $object, $force = false): bool
146
    {
147
        // Remove file associated with this response
148
        $fileHandler = $this->helper->getHandler('File');
149
        $criteria    = new \CriteriaCompo(new \Criteria('ticketid', $object->getVar('ticketid')));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar('ticketid') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept 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

149
        $criteria    = new \CriteriaCompo(new \Criteria('ticketid', /** @scrutinizer ignore-type */ $object->getVar('ticketid')));
Loading history...
150
        $criteria->add(new \Criteria('responseid', $object->getVar('responseid')));
151
        if (!$fileHandler->deleteAll($criteria)) {
152
            return false;
153
        }
154
155
        $ret = parent::delete($object, $force);
156
157
        return $ret;
158
    }
159
160
    /**
161
     * Get number of responses by staff members
162
     *
163
     * @param int $ticketid ticket to get count
164
     * @return int Number of staff responses
165
     */
166
    public function getStaffResponseCount(int $ticketid): int
167
    {
168
        $sql = \sprintf('SELECT COUNT(*) FROM `%s` r INNER JOIN %s s ON r.uid = s.uid WHERE r.ticketid = %u', $this->db->prefix($this->dbtable), $this->db->prefix('xhelp_staff'), $ticketid);
169
170
        $ret = $this->db->query($sql);
171
172
        [$count] = $this->db->fetchRow($ret);
173
174
        return (int)$count;
175
    }
176
177
    /**
178
     * Get number of responses by ticketid
179
     *
180
     * @param array $tickets where ticketid is key
181
     * @return bool|array key = ticketid, value = response count
182
     */
183
    public function getResponseCounts(array $tickets)
184
    {
185
        if (\is_array($tickets)) {
0 ignored issues
show
introduced by
The condition is_array($tickets) is always true.
Loading history...
186
            //$criteria = new \Criteria('ticketid', "(". implode(array_keys($tickets), ',') .")", 'IN');
187
            $sql = \sprintf('SELECT COUNT(*) AS numresponses, ticketid FROM `%s` WHERE ticketid IN (%s) GROUP BY ticketid', $this->db->prefix($this->dbtable), \implode(',', \array_keys($tickets)));
188
        } else {
189
            return false;
190
        }
191
        $result = $this->db->query($sql);
192
193
        if (!$result) {
194
            return false;
195
        }
196
197
        // Add each returned record to the result array
198
        while (false !== ($myrow = $this->db->fetchArray($result))) {
199
            $tickets[$myrow['ticketid']] = $myrow['numresponses'];
200
        }
201
202
        return $tickets;
203
    }
204
}
205