Passed
Branch master (410c7b)
by Michael
03:30
created

IshotHandler   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 425
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 46
eloc 167
dl 0
loc 425
rs 8.72
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getHotFriends() 0 46 5
A create() 0 14 2
A __construct() 0 13 2
B insert2() 0 82 9
A get2() 0 21 3
A getCount() 0 18 4
B getObjects() 0 44 7
A delete() 0 25 4
A deleteAll() 0 16 4
B getHottest() 0 47 6

How to fix   Complexity   

Complex Class

Complex classes like IshotHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use IshotHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
use CriteriaElement;
18
use XoopsDatabase;
19
use XoopsObject;
20
use XoopsPersistableObjectHandler;
21
22
/**
23
 * @category        Module
24
 * @package         suico
25
 * @copyright       {@link https://xoops.org/ XOOPS Project}
26
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
27
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
28
 */
29
if (!\defined(
30
    'XOOPS_ROOT_PATH'
31
)) {
32
    exit();
33
}
34
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
35
36
// -------------------------------------------------------------------------
37
// ------------------Ishot user handler class -------------------
38
// -------------------------------------------------------------------------
39
40
/**
41
 * suico_ishothandler class.
42
 * This class provides simple mecanisme for Ishot object
43
 */
44
class IshotHandler extends XoopsPersistableObjectHandler
45
{
46
    public $helper;
47
48
    public $isAdmin;
49
50
    /**
51
     * Constructor
52
     * @param \XoopsDatabase|null              $xoopsDatabase
53
     * @param \XoopsModules\Suico\Helper|null $helper
54
     */
55
56
    public function __construct(
57
        ?XoopsDatabase $xoopsDatabase = null,
58
        $helper = null
59
    ) {
60
        /** @var \XoopsModules\Suico\Helper $this ->helper */
61
62
        if (null === $helper) {
63
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
64
        } else {
65
            $this->helper = $helper;
66
        }
67
68
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
69
        //        parent::__construct($db, 'suico_groups', Image::class, 'group_id', 'group_title');
70
    }
71
72
    /**
73
     * create a new Groups
74
     *
75
     * @param bool $isNew flag the new objects as "new"?
76
     * @return \XoopsObject Groups
77
     */
78
79
    public function create(
80
        $isNew = true
81
    ) {
82
        $obj = parent::create($isNew);
83
84
        if ($isNew) {
85
            $obj->setNew();
86
        } else {
87
            $obj->unsetNew();
88
        }
89
90
        $obj->helper = $this->helper;
91
92
        return $obj;
93
    }
94
95
    /**
96
     * retrieve a Ishot
97
     *
98
     * @param int  $id of the Ishot
99
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
100
     * @return mixed reference to the {@link Ishot} object, FALSE if failed
101
     */
102
103
    public function get2(
104
        $id = null,
105
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

105
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    ) {
107
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_ishot') . ' WHERE cod_ishot=' . $id;
108
109
        if (!$result = $this->db->query($sql)) {
110
            return false;
111
        }
112
113
        $numrows = $this->db->getRowsNum($result);
114
115
        if (1 === $numrows) {
116
            $suico_ishot = new Ishot();
117
118
            $suico_ishot->assignVars($this->db->fetchArray($result));
119
120
            return $suico_ishot;
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * insert a new Ishot in the database
128
     *
129
     * @param \XoopsObject $xoopsObject  reference to the {@link Ishot}
130
     *                                   object
131
     * @param bool         $force
132
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
133
     */
134
135
    public function insert2(
136
        XoopsObject $xoopsObject,
137
        $force = false
138
    ) {
139
        global $xoopsConfig;
140
141
        if (!$xoopsObject instanceof Ishot) {
142
            return false;
143
        }
144
145
        if (!$xoopsObject->isDirty()) {
146
            return true;
147
        }
148
149
        if (!$xoopsObject->cleanVars()) {
150
            return false;
151
        }
152
153
        $cod_ishot = $uid_voter = $uid_voted = $ishot = '';
154
155
        foreach ($xoopsObject->cleanVars as $k => $v) {
156
            ${$k} = $v;
157
        }
158
159
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
160
161
        if ($xoopsObject->isNew()) {
162
            // ajout/modification d'un Ishot
163
164
            $xoopsObject = new Ishot();
165
166
            $format = 'INSERT INTO %s (cod_ishot, uid_voter, uid_voted, ishot, DATE)';
167
168
            $format .= 'VALUES (%u, %u, %u, %u, %s)';
169
170
            $sql = \sprintf(
171
                $format,
172
                $this->db->prefix('suico_ishot'),
173
                $cod_ishot,
174
                $uid_voter,
175
                $uid_voted,
176
                $ishot,
177
                $this->db->quoteString($date)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
178
            );
179
180
            $force = true;
181
        } else {
182
            $format = 'UPDATE %s SET ';
183
184
            $format .= 'cod_ishot=%u, uid_voter=%u, uid_voted=%u, ishot=%u, date_created=%s';
185
186
            $format .= ' WHERE cod_ishot = %u';
187
188
            $sql = \sprintf(
189
                $format,
190
                $this->db->prefix('suico_ishot'),
191
                $cod_ishot,
192
                $uid_voter,
193
                $uid_voted,
194
                $ishot,
195
                $this->db->quoteString($date),
196
                $cod_ishot
197
            );
198
        }
199
200
        if ($force) {
201
            $result = $this->db->queryF($sql);
202
        } else {
203
            $result = $this->db->query($sql);
204
        }
205
206
        if (!$result) {
207
            return false;
208
        }
209
210
        if (empty($cod_ishot)) {
0 ignored issues
show
introduced by
The condition empty($cod_ishot) is always true.
Loading history...
211
            $cod_ishot = $this->db->getInsertId();
212
        }
213
214
        $xoopsObject->assignVar('cod_ishot', $cod_ishot);
215
216
        return true;
217
    }
218
219
    /**
220
     * delete a Ishot from the database
221
     *
222
     * @param \XoopsObject $xoopsObject reference to the Ishot to delete
223
     * @param bool         $force
224
     * @return bool FALSE if failed.
225
     */
226
227
    public function delete(
228
        XoopsObject $xoopsObject,
229
        $force = false
230
    ) {
231
        if (!$xoopsObject instanceof Ishot) {
232
            return false;
233
        }
234
235
        $sql = \sprintf(
236
            'DELETE FROM %s WHERE cod_ishot = %u',
237
            $this->db->prefix('suico_ishot'),
238
            $xoopsObject->getVar('cod_ishot')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('cod_ishot') can also be of type array and array; however, parameter $args of sprintf() 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

238
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('cod_ishot')
Loading history...
239
        );
240
241
        if ($force) {
242
            $result = $this->db->queryF($sql);
243
        } else {
244
            $result = $this->db->query($sql);
245
        }
246
247
        if (!$result) {
248
            return false;
249
        }
250
251
        return true;
252
    }
253
254
    /**
255
     * retrieve suico_ishots from the database
256
     *
257
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
258
     * @param bool                                 $id_as_key       use the UID as key for the array?
259
     * @param bool                                 $as_object
260
     * @return array array of {@link Ishot} objects
261
     */
262
263
    public function &getObjects(
264
        ?CriteriaElement $criteriaElement = null,
265
        $id_as_key = false,
266
        $as_object = true
267
    ) {
268
        $ret = [];
269
270
        $limit = $start = 0;
271
272
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_ishot');
273
274
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
275
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

275
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
276
277
            if ('' !== $criteriaElement->getSort()) {
278
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
279
            }
280
281
            $limit = $criteriaElement->getLimit();
282
283
            $start = $criteriaElement->getStart();
284
        }
285
286
        $result = $this->db->query($sql, $limit, $start);
287
288
        if (!$result) {
289
            return $ret;
290
        }
291
292
        while (false !== ($myrow = $this->db->fetchArray($result))) {
293
            $suico_ishot = new Ishot();
294
295
            $suico_ishot->assignVars($myrow);
296
297
            if (!$id_as_key) {
298
                $ret[] = &$suico_ishot;
299
            } else {
300
                $ret[$myrow['cod_ishot']] = &$suico_ishot;
301
            }
302
303
            unset($suico_ishot);
304
        }
305
306
        return $ret;
307
    }
308
309
    /**
310
     * count suico_ishots matching a condition
311
     *
312
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match
313
     * @return int count of suico_ishots
314
     */
315
316
    public function getCount(
317
        ?CriteriaElement $criteriaElement = null
318
    ) {
319
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_ishot');
320
321
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
322
            $sql .= ' ' . $criteriaElement->renderWhere();
323
        }
324
325
        $result = $this->db->query($sql);
326
327
        if (!$result) {
328
            return 0;
329
        }
330
331
        [$count] = $this->db->fetchRow($result);
332
333
        return $count;
334
    }
335
336
    /**
337
     * delete suico_ishots matching a set of conditions
338
     *
339
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement}
340
     * @param bool                                 $force
341
     * @param bool                                 $asObject
342
     * @return bool FALSE if deletion failed
343
     */
344
345
    public function deleteAll(
346
        ?CriteriaElement $criteriaElement = null,
347
        $force = true,
348
        $asObject = false
349
    ) {
350
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_ishot');
351
352
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
353
            $sql .= ' ' . $criteriaElement->renderWhere();
354
        }
355
356
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
357
            return false;
358
        }
359
360
        return true;
361
    }
362
363
    /**
364
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
365
     * @return array
366
     */
367
368
    public function getHottest($criteria = null)
369
    {
370
        $sql = 'SELECT DISTINCTROW uname, user_avatar, uid_voted, COUNT(cod_ishot) AS qtd FROM ' . $this->db->prefix(
371
                'suico_ishot'
372
            ) . ', ' . $this->db->prefix(
373
                'users'
374
            );
375
376
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
377
            $sql .= ' ' . $criteria->renderWhere();
378
        }
379
380
        //attention here this is kind of a hack
381
382
        $sql .= ' AND uid = uid_voted';
383
384
        if ('' !== $criteria->getGroupby()) {
385
            $sql .= $criteria->getGroupby();
386
        }
387
388
        if ('' !== $criteria->getSort()) {
389
            $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
390
        }
391
392
        $limit = $criteria->getLimit();
393
394
        $start = $criteria->getStart();
395
396
        $result = $this->db->query($sql, $limit, $start);
397
398
        $vetor = [];
399
400
        $i = 0;
401
402
        while (false !== ($myrow = $this->db->fetchArray($result))) {
403
            $vetor[$i]['qtd'] = $myrow['qtd'];
404
405
            $vetor[$i]['uid_voted'] = $myrow['uid_voted'];
406
407
            $vetor[$i]['uname'] = $myrow['uname'];
408
409
            $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
410
411
            $i++;
412
        }
413
414
        return $vetor;
415
    }
416
417
    /**
418
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
419
     * @param bool $id_as_key
420
     * @return array
421
     */
422
423
    public function getHotFriends(
424
        $criteria = null,
425
        $id_as_key = false
0 ignored issues
show
Unused Code introduced by
The parameter $id_as_key is not used and could be removed. ( Ignorable by Annotation )

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

425
        /** @scrutinizer ignore-unused */ $id_as_key = false

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
426
    ) {
427
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
428
429
        $limit = $start = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $start is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
430
431
        $sql = 'SELECT uname, user_avatar, uid_voted FROM ' . $this->db->prefix(
432
                'suico_ishot'
433
            ) . ', ' . $this->db->prefix(
434
                'users'
435
            );
436
437
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
438
            $sql .= ' ' . $criteria->renderWhere();
439
440
            //attention here this is kind of a hack
441
442
            $sql .= ' AND uid = uid_voted AND ishot=1';
443
444
            if ('' !== $criteria->getSort()) {
445
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
446
            }
447
448
            $limit = $criteria->getLimit();
449
450
            $start = $criteria->getStart();
451
452
            $result = $this->db->query($sql, $limit, $start);
453
454
            $vetor = [];
455
456
            $i = 0;
457
458
            while (false !== ($myrow = $this->db->fetchArray($result))) {
459
                $vetor[$i]['uid_voted'] = $myrow['uid_voted'];
460
461
                $vetor[$i]['uname'] = $myrow['uname'];
462
463
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
464
465
                $i++;
466
            }
467
468
            return $vetor;
469
        }
470
    }
471
}
472