Passed
Push — master ( 7a0709...3ff077 )
by Michael
18:54
created

ReportHandler::getAllReports()   B

Complexity

Conditions 10
Paths 144

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 33
nc 144
nop 6
dl 0
loc 49
rs 7.2999
c 0
b 0
f 0

How to fix   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\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
 * Class ReportHandler
17
 */
18
class ReportHandler extends \XoopsPersistableObjectHandler
19
{
20
    /**
21
     * @param \XoopsDatabase|null $db
22
     */
23
    public function __construct(\XoopsDatabase $db = null)
24
    {
25
        parent::__construct($db, 'newbb_report', Report::class, 'report_id', '');
26
    }
27
28
    /**
29
     * @param array $posts
30
     * @return array
31
     */
32
    public function getByPost(array $posts): array
33
    {
34
        $ret = [];
35
        if (!$posts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $posts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
            return $ret;
37
        }
38
        if (!\is_array($posts)) {
0 ignored issues
show
introduced by
The condition is_array($posts) is always true.
Loading history...
39
            $posts = [$posts];
40
        }
41
        $post_criteria = new \Criteria('post_id', '(' . \implode(', ', $posts) . ')', 'IN');
42
        $ret           = $this->getAll($post_criteria);
43
44
        return $ret;
45
    }
46
47
    /**
48
     * @param int|mixed[] $forums
49
     * @param string    $order
50
     * @param int       $perpage
51
     * @param int       $start
52
     * @param int       $report_result
53
     * @param int       $reportId
54
     * @return array
55
     */
56
    public function getAllReports($forums = 0, string $order = 'ASC', int $perpage = 0, int &$start = 0, int $report_result = 0, int $reportId = 0): array
57
    {
58
        $forumCriteria = '';
59
        $row           = [];
60
        if ('DESC' === $order) {
61
            $operator_for_position = '>';
62
        } else {
63
            $order                 = 'ASC';
64
            $operator_for_position = '<';
65
        }
66
        $order_criteria = " ORDER BY r.report_id $order";
67
68
        if ($perpage <= 0) {
69
            $perpage = 10;
70
        }
71
        if (empty($start)) {
72
            $start = 0;
73
        }
74
        $result_criteria = ' AND r.report_result = ' . $report_result;
75
76
        if ($forums) {
77
            $forumCriteria = '';
78
        } elseif (!\is_array($forums)) {
79
            $forums        = [$forums];
80
            $forumCriteria = ' AND p.forum_id IN (' . \implode(',', $forums) . ')';
81
        }
82
        $tables_criteria = ' FROM ' . $this->db->prefix('newbb_report') . ' r, ' . $this->db->prefix('newbb_posts') . ' p WHERE r.post_id= p.post_id';
83
84
        if ($reportId) {
85
            $sql = 'SELECT COUNT(*) as report_count' . $tables_criteria . $forumCriteria . $result_criteria . " AND report_id $operator_for_position $reportId" . $order_criteria;
86
            $result = $this->db->query($sql);
87
            if ($this->db->isResultSet($result)) {
88
                $row = $this->db->fetchArray($result);
89
            }
90
            $position = $row['report_count'] ?? 0;
91
            $start    = (int)($position / $perpage) * $perpage;
92
        }
93
94
        $sql    = 'SELECT r.*, p.subject, p.topic_id, p.forum_id' . $tables_criteria . $forumCriteria . $result_criteria . $order_criteria;
95
        $result = $this->db->query($sql, $perpage, $start);
96
        $ret    = [];
97
        //$reportHandler =  Newbb\Helper::getInstance()->getHandler('Report');
98
        if ($this->db->isResultSet($result)) {
99
            while (false !== ($myrow = $this->db->fetchArray($result))) {
100
                $ret[] = $myrow; // return as array
101
            }
102
        }
103
104
        return $ret;
105
    }
106
107
    /**
108
     * @return void
109
     */
110
    public function synchronization(): void
111
    {
112
        //        return;
113
    }
114
115
    /**
116
     * clean orphan items from database
117
     *
118
     * @param string $table_link
119
     * @param string $field_link
120
     * @param string $field_object
121
     * @return bool   true on success
122
     */
123
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = ''): bool //cleanOrphan()
124
    {
125
        return parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
126
    }
127
}
128