TicketFieldHandler::mysqlDBType()   F
last analyzed

Complexity

Conditions 29
Paths 29

Size

Total Lines 127
Code Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
eloc 108
c 0
b 0
f 0
nc 29
nop 1
dl 0
loc 127
rs 3.3333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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       Brian Wahoff <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CONSTANTS_INCLUDED')) {
23
    exit();
24
}
25
26
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
27
28
/**
29
 * class TicketFieldHandler
30
 */
31
class TicketFieldHandler extends BaseObjectHandler
32
{
33
    /**
34
     * Name of child class
35
     *
36
     * @var string
37
     */
38
    public $classname = TicketField::class;
39
    /**
40
     * DB Table Name
41
     *
42
     * @var string
43
     */
44
    public $dbtable = 'xhelp_ticket_fields';
45
    public $id      = 'id';
46
47
    private const TABLE = 'xhelp_ticket_fields';
48
    private const ENTITY = TicketField::class;
49
    private const ENTITYNAME = 'TicketField';
50
    private const KEYNAME = 'id';
51
    private const IDENTIFIER = 'name';
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 \XoopsObject $object
67
     * @return string
68
     */
69
    public function insertQuery(\XoopsObject $object): string
70
    {
71
        //TODO mb replace with individual variables
72
        // Copy all object vars into local variables
73
        foreach ($object->cleanVars as $k => $v) {
74
            ${$k} = $v;
75
        }
76
77
        $sql = \sprintf(
78
            'INSERT INTO `%s` (id, NAME, description, fieldname, controltype, datatype, required, fieldlength, weight, fieldvalues, defaultvalue, VALIDATION)
79
            VALUES (%u, %s, %s, %s, %u, %s, %u, %u, %s, %s, %s, %s)',
80
            $this->db->prefix($this->dbtable),
81
            $id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
82
            $this->db->quoteString($name),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
83
            $this->db->quoteString($description),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
84
            $this->db->quoteString($fieldname),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldname seems to be never defined.
Loading history...
85
            $controltype,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $controltype seems to be never defined.
Loading history...
86
            $this->db->quoteString($datatype),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $datatype seems to be never defined.
Loading history...
87
            $required,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $required seems to be never defined.
Loading history...
88
            $fieldlength,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldlength seems to be never defined.
Loading history...
89
            $weight,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
90
            $this->db->quoteString($fieldvalues),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldvalues seems to be never defined.
Loading history...
91
            $this->db->quoteString($defaultvalue),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultvalue seems to be never defined.
Loading history...
92
            $this->db->quoteString($validation)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validation seems to be never defined.
Loading history...
93
        );
94
95
        return $sql;
96
    }
97
98
    /**
99
     * @param \XoopsObject $object
100
     * @return string
101
     */
102
    public function updateQuery(\XoopsObject $object): string
103
    {
104
        //TODO mb replace with individual variables
105
        // Copy all object vars into local variables
106
        foreach ($object->cleanVars as $k => $v) {
107
            ${$k} = $v;
108
        }
109
110
        $sql = \sprintf(
111
            'UPDATE `%s` SET NAME = %s, description = %s, fieldname = %s, controltype = %u, datatype = %s, required = %u, fieldlength = %u, weight = %u, fieldvalues = %s,
112
            defaultvalue = %s, VALIDATION = %s WHERE id = %u',
113
            $this->db->prefix($this->dbtable),
114
            $this->db->quoteString($name),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
115
            $this->db->quoteString($description),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
116
            $this->db->quoteString($fieldname),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldname seems to be never defined.
Loading history...
117
            $controltype,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $controltype seems to be never defined.
Loading history...
118
            $this->db->quoteString($datatype),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $datatype seems to be never defined.
Loading history...
119
            $required,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $required seems to be never defined.
Loading history...
120
            $fieldlength,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldlength seems to be never defined.
Loading history...
121
            $weight,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $weight seems to be never defined.
Loading history...
122
            $this->db->quoteString($fieldvalues),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldvalues seems to be never defined.
Loading history...
123
            $this->db->quoteString($defaultvalue),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultvalue seems to be never defined.
Loading history...
124
            $this->db->quoteString($validation),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validation seems to be never defined.
Loading history...
125
            $id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
126
        );
127
128
        return $sql;
129
    }
130
131
    /**
132
     * @param \XoopsObject $object
133
     * @return string
134
     */
135
    public function deleteQuery(\XoopsObject $object): string
136
    {
137
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), $object->getVar($this->id));
0 ignored issues
show
Bug introduced by
It seems like $object->getVar($this->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

137
        $sql = \sprintf('DELETE FROM `%s` WHERE id = %u', $this->db->prefix($this->dbtable), /** @scrutinizer ignore-type */ $object->getVar($this->id));
Loading history...
138
139
        return $sql;
140
    }
141
142
    /**
143
     * @param \XoopsObject $object
144
     * @param bool         $force
145
     * @return bool
146
     */
147
    public function insert(\XoopsObject $object, $force = true): bool
148
    {
149
        /** @var \XoopsModules\Xhelp\TicketFieldDepartmentHandler $ticketFieldDepartmentHandler */
150
        $ticketFieldDepartmentHandler = $this->helper->getHandler('TicketFieldDepartment');
151
        if ($object->isNew()) {
152
            $add_field = true;
153
            $fieldname = $object->getVar('fieldname');
154
        } else {
155
            $old_obj = $this->get($object->getVar('id'));
156
157
            $old_name = $old_obj->getVar('fieldname');
158
            $new_name = $object->getVar('fieldname');
159
160
            $add_field   = false;
161
            $alter_table = ($old_name != $new_name)
162
                           || ($old_obj->getVar('fieldlength') != $object->getVar('fieldlength'))
163
                           || ($old_obj->getVar('controltype') != $object->getVar('controltype'))
164
                           || ($old_obj->getVar('datatype') != $object->getVar('datatype'));
165
        }
166
167
        //Store base object
168
        $ret = parent::insert($object, $force);
169
        if ($ret) {
170
            //Update Joiner Records
171
            $ret2 = $ticketFieldDepartmentHandler->removeFieldFromAllDept($object->getVar('id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $ret2 is dead and can be removed.
Loading history...
172
173
            $depts = $object->getDepartments();
0 ignored issues
show
Bug introduced by
The method getDepartments() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Xhelp\TicketField. ( Ignorable by Annotation )

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

173
            /** @scrutinizer ignore-call */ 
174
            $depts = $object->getDepartments();
Loading history...
174
175
            if (\count($depts)) {
176
                $ret = $ticketFieldDepartmentHandler->addDepartmentToField($depts, $object->getVar('id'));
177
            }
178
179
            $mysql = $this->mysqlDBType($object);
180
181
            if ($add_field) {
182
                Utility::addDBField('xhelp_ticket_values', $fieldname, $mysql['fieldtype'], $mysql['length']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fieldname does not seem to be defined for all execution paths leading up to this point.
Loading history...
183
            } elseif ($alter_table) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $alter_table does not seem to be defined for all execution paths leading up to this point.
Loading history...
184
                Utility::renameDBField('xhelp_ticket_values', $old_name, $new_name, $mysql['fieldtype'], $mysql['length']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $new_name does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $old_name does not seem to be defined for all execution paths leading up to this point.
Loading history...
185
            }
186
        }
187
188
        return $ret;
189
    }
190
191
    /**
192
     * @param \XoopsObject $object
193
     * @param bool         $force
194
     * @return bool
195
     */
196
    public function delete(\XoopsObject $object, $force = false): bool
197
    {
198
        //Remove FieldDepartment Records
199
        /** @var \XoopsModules\Xhelp\TicketFieldDepartmentHandler $ticketFieldDepartmentHandler */
200
        $ticketFieldDepartmentHandler = $this->helper->getHandler('TicketFieldDepartment');
201
        $fieldId                      = $object->getVar('id');
202
        if (!$ret = $ticketFieldDepartmentHandler->removeFieldFromAllDept($fieldId, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
203
            $object->setErrors('Unable to remove field from departments');
204
        }
205
206
        //Remove values from ticket values table
207
        if (!$ret = Utility::removeDBField('xhelp_ticket_values', $object->getVar('fieldname'))) {
208
            $object->setErrors('Unable to remove field from ticket values table');
209
        }
210
211
        //Remove obj from table
212
        $ret = $this->delete($object, $force);
213
214
        return $ret;
215
    }
216
217
    /**
218
     * @param int $dept
219
     * @return array
220
     */
221
    public function getByDept(int $dept): array
222
    {
223
        /** @var TicketFieldDepartmentHandler $ticketFieldDepartmentHandler */
224
        $ticketFieldDepartmentHandler = $this->helper->getHandler('TicketFieldDepartment');
225
        $ret                          = $ticketFieldDepartmentHandler->fieldsByDepartment($dept);
226
227
        return $ret;
228
    }
229
230
    /**
231
     * @param \XoopsObject $object
232
     * @return array
233
     */
234
    private function mysqlDBType(\XoopsObject $object): array
235
    {
236
        $controltype = $object->getVar('controltype');
237
        $datatype    = $object->getVar('datatype');
238
        $fieldlength = $object->getVar('fieldlength');
239
240
        $mysqldb           = [];
241
        $mysqldb['length'] = $fieldlength;
242
        switch ($controltype) {
243
            case \XHELP_CONTROL_TXTBOX:
244
245
                switch ($datatype) {
246
                    case \_XHELP_DATATYPE_TEXT:
247
                        if ($fieldlength <= 255) {
248
                            $mysqldb['fieldtype'] = 'VARCHAR';
249
                        } elseif ($fieldlength <= 65535) {
250
                            $mysqldb['fieldtype'] = 'TEXT';
251
                        } elseif ($fieldlength <= 16777215) {
252
                            $mysqldb['fieldtype'] = 'MEDIUMTEXT';
253
                        } else {
254
                            $mysqldb['fieldtype'] = 'LONGTEXT';
255
                        }
256
                        break;
257
                    case \_XHELP_DATATYPE_NUMBER_INT:
258
                        $mysqldb['fieldtype'] = 'INT';
259
                        $mysqldb['length']    = 0;
260
                        break;
261
                    case \_XHELP_DATATYPE_NUMBER_DEC:
262
                        $mysqldb['fieldtype'] = 'DECIMAL';
263
                        $mysqldb['length']    = '7,4';
264
265
                    // no break
266
                    default:
267
                        $mysqldb['fieldtype'] = 'VARCHAR';
268
                        $mysqldb['length']    = 255;
269
                        break;
270
                }
271
                break;
272
            case \XHELP_CONTROL_TXTAREA:
273
                if ($fieldlength <= 255) {
274
                    $mysqldb['fieldtype'] = 'VARCHAR';
275
                } elseif ($fieldlength <= 65535) {
276
                    $mysqldb['fieldtype'] = 'TEXT';
277
                    $mysqldb['length']    = 0;
278
                } elseif ($fieldlength <= 16777215) {
279
                    $mysqldb['fieldtype'] = 'MEDIUMTEXT';
280
                    $mysqldb['length']    = 0;
281
                } else {
282
                    $mysqldb['fieldtype'] = 'LONGTEXT';
283
                    $mysqldb['length']    = 0;
284
                }
285
                break;
286
            case \XHELP_CONTROL_SELECT:
287
                switch ($datatype) {
288
                    case \_XHELP_DATATYPE_TEXT:
289
                        if ($fieldlength <= 255) {
290
                            $mysqldb['fieldtype'] = 'VARCHAR';
291
                        } elseif ($fieldlength <= 65535) {
292
                            $mysqldb['fieldtype'] = 'TEXT';
293
                        } elseif ($fieldlength <= 16777215) {
294
                            $mysqldb['fieldtype'] = 'MEDIUMTEXT';
295
                        } else {
296
                            $mysqldb['fieldtype'] = 'LONGTEXT';
297
                        }
298
                        break;
299
                    case \_XHELP_DATATYPE_NUMBER_INT:
300
                        $mysqldb['fieldtype'] = 'INT';
301
                        $mysqldb['length']    = 0;
302
                        break;
303
                    case \_XHELP_DATATYPE_NUMBER_DEC:
304
                        $mysqldb['fieldtype'] = 'DECIMAL';
305
                        $mysqldb['length']    = '7,4';
306
307
                    // no break
308
                    default:
309
                        $mysqldb['fieldtype'] = 'VARCHAR';
310
                        $mysqldb['length']    = 255;
311
                        break;
312
                }
313
                break;
314
            case \XHELP_CONTROL_YESNO:
315
                $mysqldb['fieldtype'] = 'TINYINT';
316
                $mysqldb['length']    = 1;
317
                break;
318
            case \XHELP_CONTROL_RADIOBOX:
319
                switch ($datatype) {
320
                    case \_XHELP_DATATYPE_TEXT:
321
                        if ($fieldlength <= 255) {
322
                            $mysqldb['fieldtype'] = 'VARCHAR';
323
                        } elseif ($fieldlength <= 65535) {
324
                            $mysqldb['fieldtype'] = 'TEXT';
325
                        } elseif ($fieldlength <= 16777215) {
326
                            $mysqldb['fieldtype'] = 'MEDIUMTEXT';
327
                        } else {
328
                            $mysqldb['fieldtype'] = 'LONGTEXT';
329
                        }
330
                        break;
331
                    case \_XHELP_DATATYPE_NUMBER_INT:
332
                        $mysqldb['fieldtype'] = 'INT';
333
                        $mysqldb['length']    = 0;
334
                        break;
335
                    case \_XHELP_DATATYPE_NUMBER_DEC:
336
                        $mysqldb['fieldtype'] = 'DECIMAL';
337
                        $mysqldb['length']    = '7,4';
338
339
                    // no break
340
                    default:
341
                        $mysqldb['fieldtype'] = 'VARCHAR';
342
                        $mysqldb['length']    = 255;
343
                        break;
344
                }
345
                break;
346
            case \XHELP_CONTROL_DATETIME:
347
                $mysqldb['fieldtype'] = 'INT';
348
                $mysqldb['length']    = 0;
349
                break;
350
            case \XHELP_CONTROL_FILE:
351
                $mysqldb['fieldtype'] = 'VARCHAR';
352
                $mysqldb['length']    = 255;
353
                break;
354
            default:
355
                $mysqldb['fieldtype'] = 'VARCHAR';
356
                $mysqldb['length']    = 255;
357
                break;
358
        }
359
360
        return $mysqldb;
361
    }
362
}
363