Passed
Branch master (5b6d04)
by Michael
03:42
created

RatingsHandler   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 122
c 1
b 0
f 0
dl 0
loc 212
rs 9.2
wmc 40

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A get() 0 3 1
A deleteAllRatings() 0 7 1
F getItemRating() 0 147 35
A __construct() 0 3 1
A getInsertId() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like RatingsHandler 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 RatingsHandler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Publisher;
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
use Criteria;
16
use CriteriaCompo;
17
use XoopsDatabase;
18
19
20
/**
21
 * Publisher module for xoops
22
 *
23
 * @copyright      module for xoops
24
 * @license        GPL 3.0 or later
25
 * @package        Publisher
26
 * @since          1.0
27
 * @min_xoops      2.5.10
28
 * @author         XOOPS Development Team
29
 */
30
\defined('XOOPS_ROOT_PATH') || exit('Restricted access');
31
32
/**
33
 * Class Object RatingsHandler
34
 */
35
class RatingsHandler extends \XoopsPersistableObjectHandler
36
{
37
    /**
38
     * Constructor
39
     * @param \XoopsDatabase $db
40
     */
41
    public function __construct(XoopsDatabase $db)
42
    {
43
        parent::__construct($db, 'publisher_liking', Ratings::class, 'rate_id', 'rate_itemid');
44
    }
45
46
    /**
47
     * @param bool $isNew
48
     *
49
     * @return Object
50
     */
51
    public function create($isNew = true)
52
    {
53
        return parent::create($isNew);
54
    }
55
56
    /**
57
     * retrieve a field
58
     *
59
     * @param int   $i field id
60
     * @param array $fields
61
     * @return mixed reference to the {@link Get} object
62
     */
63
    public function get($i = null, $fields = null)
64
    {
65
        return parent::get($i, $fields);
66
    }
67
68
    /**
69
     * get inserted id
70
     *
71
     * @param null
72
     * @return int reference to the {@link Get} object
73
     */
74
    public function getInsertId()
75
    {
76
        return $this->db->getInsertId();
77
    }
78
79
    /**
80
     * Get Rating per item in the database
81
     * @param int $itemId
82
     * @param int $source
83
     * @return array
84
     */
85
    public function getItemRating($itemId = 0, $source = 0)
86
    {
87
        $helper = \XoopsModules\Publisher\Helper::getInstance();
88
89
        $itemRating               = [];
90
        $itemRating['nb_ratings'] = 0;
91
        $uid                      = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
92
        $voted                    = false;
93
        $ip                       = \getenv('REMOTE_ADDR');
94
        $current_rating           = 0;
95
96
        if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')
97
            || Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars')
98
            || Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) {
99
            $rating_unitwidth = 25;
100
            if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) {
101
                $max_units = 5;
102
            } else {
103
                $max_units = 10;
104
            }
105
106
            $criteria = new CriteriaCompo();
107
            $criteria->add(new Criteria('rate_itemid', $itemId));
108
            $criteria->add(new Criteria('rate_source', $source));
109
110
            $ratingObjs               = $helper->getHandler('ratings')->getObjects($criteria);
111
            $count                    = count($ratingObjs);
112
            $itemRating['nb_ratings'] = $count;
113
114
            foreach ($ratingObjs as $ratingObj) {
115
                $current_rating += $ratingObj->getVar('rate_value');
116
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
117
                    $voted            = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $voted is dead and can be removed.
Loading history...
118
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
119
                }
120
            }
121
            unset($ratingObj);
122
            unset($criteria);
123
124
            $itemRating['avg_rate_value'] = 0;
125
            if ($count > 0) {
126
                $itemRating['avg_rate_value'] = \number_format($current_rating / $count, 2);
127
            }
128
            if (1 == $count) {
129
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1);
130
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1);
131
            } else {
132
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X);
133
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X);
134
            }
135
            $text                    = \str_replace('%m', $max_units, $text);
136
            $text                    = \str_replace('%t', $itemRating['nb_ratings'], $text);
137
            $shorttext               = \str_replace('%t', $itemRating['nb_ratings'], $shorttext);
138
            $itemRating['text']      = $text;
139
            $itemRating['shorttext'] = $shorttext;
140
            $itemRating['size']      = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
141
            $itemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
142
        } elseif (Constants::RATING_LIKES === (int)$helper->getConfig('ratingbars')) {
143
            $criteria = new CriteriaCompo();
144
            $criteria->add(new Criteria('rate_itemid', $itemId));
145
            $criteria->add(new Criteria('rate_source', $source));
146
            $criteria->add(new Criteria('rate_value', 0, '<'));
147
148
            $ratingObjs = $helper->getHandler('Ratings')->getObjects($criteria);
149
            $count      = count($ratingObjs);
150
151
            foreach ($ratingObjs as $ratingObj) {
152
                $current_rating += $ratingObj->getVar('rate_value');
153
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
154
                    $voted            = true;
155
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
156
                }
157
            }
158
            unset($ratingObj);
159
            unset($criteria);
160
            $itemRating['dislikes'] = $count;
161
162
            $criteria = new CriteriaCompo();
163
            $criteria->add(new Criteria('rate_itemid', $itemId));
164
            $criteria->add(new Criteria('rate_source', $source));
165
            $criteria->add(new Criteria('rate_value', 0, '>'));
166
167
            $ratingObjs     = $helper->getHandler('ratings')->getObjects($criteria);
168
            $count          = count($ratingObjs);
169
            $current_rating = 0;
170
            foreach ($ratingObjs as $ratingObj) {
171
                $current_rating += $ratingObj->getVar('rate_value');
172
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
173
                    $voted            = true;
174
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
175
                }
176
            }
177
            unset($ratingObj);
178
            unset($criteria);
179
            $itemRating['likes'] = $count;
180
181
            $count = $itemRating['likes'] + $itemRating['dislikes'];
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
182
            // Facebook Reactions  ==========================================
183
184
        } elseif (Constants::RATING_REACTION === (int)$helper->getConfig('ratingbars')) {
185
            $criteria = new CriteriaCompo();
186
            $criteria->add(new Criteria('rate_itemid', $itemId));
187
            $criteria->add(new Criteria('rate_source', $source));
188
            $criteria->add(new Criteria('rate_value', 0, '<'));
189
190
            $ratingObjs               = $helper->getHandler('ratings')->getObjects($criteria);
191
            $count                    = count($ratingObjs);
192
            $itemRating['nb_ratings'] = $count;
193
194
            foreach ($ratingObjs as $ratingObj) {
195
                $current_rating += $ratingObj->getVar('rate_value');
196
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
197
                    $voted            = true;
198
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
199
                }
200
            }
201
            unset($ratingObj);
202
            unset($criteria);
203
            $itemRating['dislikes'] = $count;
204
205
            $criteria = new CriteriaCompo();
206
            $criteria->add(new Criteria('rate_itemid', $itemId));
207
            $criteria->add(new Criteria('rate_source', $source));
208
            $criteria->add(new Criteria('rate_value', 0, '>'));
209
210
            $ratingObjs     = $helper->getHandler('ratings')->getObjects($criteria);
211
            $count          = count($ratingObjs);
212
            $current_rating = 0;
213
            foreach ($ratingObjs as $ratingObj) {
214
                $current_rating += $ratingObj->getVar('rate_value');
215
                if (($ratingObj->getVar('rate_ip') == $ip && $uid == 0) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
216
                    $voted            = true;
217
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
218
                }
219
            }
220
            unset($ratingObj);
221
            unset($criteria);
222
            $itemRating['likes'] = $count;
223
224
            $count = $itemRating['likes'] + $itemRating['dislikes'];
225
        } else {
226
            $itemRating['uid']        = $uid;
227
            $itemRating['nb_ratings'] = $count;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $count seems to be never defined.
Loading history...
228
            $itemRating['voted']      = $voted;
229
            $itemRating['ip']         = $ip;
230
        }
231
        return $itemRating;
232
    }
233
234
    /**
235
     * delete ratings of given item
236
     * @param mixed $itemId
237
     * @param mixed $source
238
     * @return bool
239
     */
240
    public function deleteAllRatings($itemId, $source)
241
    {
242
        $criteria = new CriteriaCompo();
243
        $criteria->add(new Criteria('rate_itemid', $itemId));
244
        $criteria->add(new Criteria('rate_source', $source));
245
246
        return $this->deleteAll($criteria);
247
    }
248
}
249