Passed
Branch master (c76c49)
by Michael
12:58
created

RatingsHandler::getItemRating()   F

Complexity

Conditions 35
Paths 22

Size

Total Lines 148
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 35
eloc 114
c 4
b 0
f 0
nc 22
nop 2
dl 0
loc 148
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\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
/**
16
 * Publisher module for xoops
17
 *
18
 * @copyright      module for xoops
19
 * @license        GPL 3.0 or later
20
 * @since          1.0
21
 * @min_xoops      2.5.10
22
 * @author         XOOPS Development Team
23
 */
24
\defined('XOOPS_ROOT_PATH') || exit('Restricted access');
25
26
/**
27
 * Class Object RatingsHandler
28
 */
29
class RatingsHandler extends \XoopsPersistableObjectHandler
30
{
31
    private const TABLE      = 'publisher_liking';
32
    private const ENTITY     = Ratings::class;
33
    private const ENTITYNAME = 'Ratings';
34
    private const KEYNAME    = 'rate_id';
35
    private const IDENTIFIER = 'rate_itemid';
36
37
    /**
38
     * Constructor
39
     */
40
    public function __construct(\XoopsDatabase $db)
41
    {
42
        $this->db = $db;
43
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
44
    }
45
46
    /**
47
     * @param bool $isNew
48
     *
49
     * @return \XoopsObject
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
        $currentRating            = 0;
95
        $count                    = 0;
96
97
        if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')
98
            || Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars')
99
            || Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) {
100
            $rating_unitwidth = 25;
101
            if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) {
102
                $max_units = 5;
103
            } else {
104
                $max_units = 10;
105
            }
106
107
            $criteria = new \CriteriaCompo();
108
            $criteria->add(new \Criteria('rate_itemid', $itemId));
109
            $criteria->add(new \Criteria('rate_source', $source));
110
111
            $ratingObjs               = $helper->getHandler('ratings')
112
                                               ->getObjects($criteria);
113
            $count                    = \count($ratingObjs);
114
            $itemRating['nb_ratings'] = $count;
115
116
            foreach ($ratingObjs as $ratingObj) {
117
                $currentRating += $ratingObj->getVar('rate_value');
118
                if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
119
                    $voted            = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $voted is dead and can be removed.
Loading history...
120
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
121
                }
122
            }
123
            unset($criteria);
124
125
            $itemRating['avg_rate_value'] = 0;
126
            if ($count > 0) {
127
                $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2);
128
            }
129
            if (1 == $count) {
130
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1);
131
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1);
132
            } else {
133
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X);
134
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X);
135
            }
136
            $text                    = \str_replace('%m', (string)$max_units, $text);
137
            $text                    = \str_replace('%t', (string)$itemRating['nb_ratings'], $text);
138
            $shorttext               = \str_replace('%t', (string)$itemRating['nb_ratings'], $shorttext);
139
            $itemRating['text']      = $text;
140
            $itemRating['shorttext'] = $shorttext;
141
            $itemRating['size']      = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
142
            $itemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
143
        } elseif (Constants::RATING_LIKES === (int)$helper->getConfig('ratingbars')) {
144
            $criteria = new \CriteriaCompo();
145
            $criteria->add(new \Criteria('rate_itemid', $itemId));
146
            $criteria->add(new \Criteria('rate_source', $source));
147
            $criteria->add(new \Criteria('rate_value', 0, '<'));
148
149
            $ratingObjs = $helper->getHandler('Ratings')
150
                                 ->getObjects($criteria);
151
            $count      = \count($ratingObjs);
152
153
            foreach ($ratingObjs as $ratingObj) {
154
                $currentRating += $ratingObj->getVar('rate_value');
155
                if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
156
                    $voted            = true;
157
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
158
                }
159
            }
160
            unset($criteria);
161
            $itemRating['dislikes'] = $count;
162
163
            $criteria = new \CriteriaCompo();
164
            $criteria->add(new \Criteria('rate_itemid', $itemId));
165
            $criteria->add(new \Criteria('rate_source', $source));
166
            $criteria->add(new \Criteria('rate_value', 0, '>'));
167
168
            $ratingObjs    = $helper->getHandler('ratings')
169
                                    ->getObjects($criteria);
170
            $count         = \count($ratingObjs);
171
            $currentRating = 0;
172
            foreach ($ratingObjs as $ratingObj) {
173
                $currentRating += $ratingObj->getVar('rate_value');
174
                if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
175
                    $voted            = true;
176
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
177
                }
178
            }
179
            unset($criteria);
180
            $itemRating['likes'] = $count;
181
182
            $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...
183
            // Facebook Reactions  ==========================================
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')
191
                                               ->getObjects($criteria);
192
            $count                    = \count($ratingObjs);
193
            $itemRating['nb_ratings'] = $count;
194
195
            foreach ($ratingObjs as $ratingObj) {
196
                $currentRating += $ratingObj->getVar('rate_value');
197
                if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
198
                    $voted            = true;
199
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
200
                }
201
            }
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')
211
                                    ->getObjects($criteria);
212
            $count         = \count($ratingObjs);
213
            $currentRating = 0;
214
            foreach ($ratingObjs as $ratingObj) {
215
                $currentRating += $ratingObj->getVar('rate_value');
216
                if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) {
217
                    $voted            = true;
218
                    $itemRating['id'] = $ratingObj->getVar('rate_id');
219
                }
220
            }
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;
228
            $itemRating['voted']      = $voted;
229
            $itemRating['ip']         = $ip;
230
        }
231
232
        return $itemRating;
233
    }
234
235
    /**
236
     * delete ratings of given item
237
     * @param mixed $itemId
238
     * @param mixed $source
239
     * @return bool
240
     */
241
    public function deleteAllRatings($itemId, $source)
242
    {
243
        $criteria = new \CriteriaCompo();
244
        $criteria->add(new \Criteria('rate_itemid', $itemId));
245
        $criteria->add(new \Criteria('rate_source', $source));
246
247
        return $this->deleteAll($criteria);
248
    }
249
}
250