Passed
Branch master (fde0f8)
by Michael
03:06
created

VoteHandler::getItemRating5()   F

Complexity

Conditions 23
Paths 22

Size

Total Lines 169
Code Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 133
c 0
b 0
f 0
nc 22
nop 2
dl 0
loc 169
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
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
 * @package        Publisher
21
 * @since          1.0
22
 * @min_xoops      2.5.10
23
 * @author         XOOPS Development Team
24
 */
25
26
/**
27
 * Class Object VoteHandler
28
 */
29
class VoteHandler extends \XoopsPersistableObjectHandler
30
{
31
    private const TABLE = 'publisher_rating';
32
    private const ENTITY = Vote::class;
33
    private const ENTITYNAME = 'Vote';
34
    private const KEYNAME = 'ratingid';
35
    private const IDENTIFIER = 'itemid';
36
    private const SOURCE = 'source';
37
38
    /**
39
     * @var Helper
40
     */
41
    public $helper;
42
43
    /**
44
     * Constructor
45
     * @param \XoopsDatabase|null                 $db
46
     * @param \XoopsModules\Publisher\Helper|null $helper
47
     */
48
    public function __construct(\XoopsDatabase $db = null, Helper $helper = null)
49
    {
50
        $this->db = $db;
51
        /** @var Helper $this->helper */
52
        $this->helper = $helper ?? Helper::getInstance();
53
54
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
55
    }
56
57
    /**
58
     * get inserted id
59
     *
60
     * @param null
61
     * @return int reference to the {@link Get} object
62
     */
63
    public function getInsertId(): int
64
    {
65
        return $this->db->getInsertId();
66
    }
67
68
    /**
69
     * Get Rating per item in the database
70
     * @param int|null $itemId
71
     * @param int|null $source
72
     * @return array
73
     */
74
    public function getItemRating($itemId = null, $source = null): array
75
    {
76
        $itemId    = $itemId ?? 0;
77
        $source    = $source ?? 0;
78
        $xoopsUser = $GLOBALS['xoopsUser'];
79
80
        $itemRating            = [];
81
        $itemRating['nb_vote'] = 0;
82
        $uid                   = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
83
        $voted                 = false;
84
        $ip                    = \getenv('REMOTE_ADDR');
85
        $currentRating         = 0;
86
        $count                 = 0;
87
88
        $max_units   = 10;
89
        $ratingbarsValue  = (int)$this->helper->getConfig('ratingbars');
90
        $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM];
91
92
        if (in_array($ratingbarsValue, $ratingArray)) {
93
            $rating_unitwidth = 25;
94
            if (Constants::RATING_5STARS === (int)$this->helper->getConfig('ratingbars')) {
95
                $max_units = 5;
96
            }
97
98
            $criteria = new \CriteriaCompo();
99
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
100
            $criteria->add(new \Criteria(static::SOURCE, $source));
101
102
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler or XoopsModules\Publisher\PermissionHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

102
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->/** @scrutinizer ignore-call */ getObjects($criteria);
Loading history...
103
            $count                 = \count($voteObjs);
104
            $itemRating['nb_vote'] = $count;
105
106
            foreach ($voteObjs as $voteObj) {
107
                $currentRating += $voteObj->getVar('rate');
108
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
109
                    $voted            = true;
110
                    $itemRating['id'] = $voteObj->getVar('ratingid');
111
                }
112
            }
113
            unset($criteria);
114
115
            $itemRating['avg_rate_value'] = 0;
116
            if ($count > 0) {
117
                $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2);
118
            }
119
            if (1 == $count) {
120
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1);
121
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1);
122
            } else {
123
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X);
124
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X);
125
            }
126
            $text                    = \str_replace('%m', $max_units, $text);
127
            $text                    = \str_replace('%t', $itemRating['nb_vote'], $text);
128
            $shorttext               = \str_replace('%t', $itemRating['nb_vote'], $shorttext);
129
            $itemRating['text']      = $text;
130
            $itemRating['shorttext'] = $shorttext;
131
            $itemRating['size']      = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
132
            $itemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
133
134
            $itemRating['ip']    = $ip;
135
            $itemRating['uid']   = $uid;
136
            $itemRating['voted'] = $voted;
137
            // YouTube Liking  ==========================================
138
        } elseif (Constants::RATING_LIKES === (int)$this->helper->getConfig('ratingbars')) {
139
            // get count of "dislikes"
140
            $criteria = new \CriteriaCompo();
141
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
142
            $criteria->add(new \Criteria(static::SOURCE, $source));
143
            $criteria->add(new \Criteria('rate', 0, '<'));
144
145
            $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
146
            $count    = \count($voteObjs);
147
148
            foreach ($voteObjs as $voteObj) {
149
                $currentRating += $voteObj->getVar('rate');
150
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
151
                    $voted            = true;
152
                    $itemRating['id'] = $voteObj->getVar('ratingid');
153
                }
154
            }
155
            unset($criteria);
156
            $itemRating['dislikes'] = $count;
157
158
            // get count of "likes"
159
            $criteria = new \CriteriaCompo();
160
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
161
            $criteria->add(new \Criteria(static::SOURCE, $source));
162
            $criteria->add(new \Criteria('rate', 0, '>'));
163
164
            $voteObjs      = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
165
            $count         = \count($voteObjs);
166
            $currentRating = 0;
167
            foreach ($voteObjs as $voteObj) {
168
                $currentRating += $voteObj->getVar('rate');
169
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
170
                    $voted            = true;
171
                    $itemRating['id'] = $voteObj->getVar('ratingid');
172
                }
173
            }
174
            unset($criteria);
175
            $itemRating['likes'] = $count;
176
177
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes'];
178
            $itemRating['ip']      = $ip;
179
            $itemRating['uid']     = $uid;
180
            $itemRating['voted']   = $voted;
181
            // Facebook Reactions  ==========================================
182
        } elseif (Constants::RATING_REACTION === (int)$this->helper->getConfig('ratingbars')) {
183
            $criteria = new \CriteriaCompo();
184
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
185
            $criteria->add(new \Criteria(static::SOURCE, $source));
186
            $criteria->add(new \Criteria('rate', 0, '<'));
187
188
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
189
            $count                 = \count($voteObjs);
190
            $itemRating['nb_vote'] = $count;
191
192
            foreach ($voteObjs as $voteObj) {
193
                $currentRating += $voteObj->getVar('rate');
194
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
195
                    $voted            = true;
196
                    $itemRating['id'] = $voteObj->getVar('ratingid');
197
                }
198
            }
199
            unset($criteria);
200
            $itemRating['dislikes'] = $count;
201
202
            $criteria = new \CriteriaCompo();
203
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
204
            $criteria->add(new \Criteria(static::SOURCE, $source));
205
            $criteria->add(new \Criteria('rate', 0, '>'));
206
207
            $voteObjs      = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
208
            $count         = \count($voteObjs);
209
            $currentRating = 0;
210
            foreach ($voteObjs as $voteObj) {
211
                $currentRating += $voteObj->getVar('rate');
212
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
213
                    $voted            = true;
214
                    $itemRating['id'] = $voteObj->getVar('ratingid');
215
                }
216
            }
217
            unset($criteria);
218
            $itemRating['likes'] = $count;
219
220
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes'];
221
            $itemRating['ip']      = $ip;
222
            $itemRating['uid']     = $uid;
223
            $itemRating['voted']   = $voted;
224
        } else {
225
            $itemRating['uid']     = $uid;
226
            $itemRating['nb_vote'] = $count;
227
            $itemRating['voted']   = $voted;
228
            $itemRating['ip']      = $ip;
229
        }
230
        return $itemRating;
231
    }
232
233
    /**
234
     * Get Rating per item in the database
235
     * @param null     $itemObj
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $itemObj is correct as it would always require null to be passed?
Loading history...
236
     * @param int|null $source
237
     * @return array
238
     */
239
    public function getItemRating5($itemObj = null, $source = null): array
240
    {
241
        $itemId    = $itemObj->itemid();
0 ignored issues
show
Bug introduced by
The method itemid() does not exist on null. ( Ignorable by Annotation )

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

241
        /** @scrutinizer ignore-call */ 
242
        $itemId    = $itemObj->itemid();

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...
242
        $source    = $source ?? 0;
243
        $xoopsUser = $GLOBALS['xoopsUser'];
244
245
        $itemRating            = [];
246
        $itemRating['nb_vote'] = 0;
247
        $uid                   = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0;
248
        $voted                 = false;
249
        $ip                    = \getenv('REMOTE_ADDR');
250
        $currentRating         = 0;
251
        $count                 = 0;
252
253
        $max_units   = 10;
254
        $ratingbarsValue  = $itemObj->votetype();
255
        $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM];
256
257
        if (in_array($ratingbarsValue, $ratingArray)) {
258
            $rating_unitwidth = 25;
259
            if (Constants::RATING_5STARS === $ratingbarsValue) {
260
                $max_units = 5;
261
            }
262
263
            $criteria = new \CriteriaCompo();
264
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
265
            $criteria->add(new \Criteria(static::SOURCE, $source));
266
267
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
268
            $count                 = \count($voteObjs);
269
            $itemRating['nb_vote'] = $count;
270
271
            foreach ($voteObjs as $voteObj) {
272
                $currentRating += $voteObj->getVar('rate');
273
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
274
                    $voted            = true;
275
                    $itemRating['id'] = $voteObj->getVar('ratingid');
276
                }
277
            }
278
            unset($criteria);
279
280
            $itemRating['avg_rate_value'] = 0;
281
            if ($count > 0) {
282
                $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2);
283
            }
284
            if (1 == $count) {
285
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1);
286
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1);
287
            } else {
288
                $text      = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X);
289
                $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X);
290
            }
291
            $text                    = \str_replace('%m', $max_units, $text);
292
            $text                    = \str_replace('%t', $itemRating['nb_vote'], $text);
293
            $shorttext               = \str_replace('%t', $itemRating['nb_vote'], $shorttext);
294
            $itemRating['text']      = $text;
295
            $itemRating['shorttext'] = $shorttext;
296
            $itemRating['size']      = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px';
297
            $itemRating['maxsize']   = ($max_units * $rating_unitwidth) . 'px';
298
299
            $itemRating['ip']    = $ip;
300
            $itemRating['uid']   = $uid;
301
            $itemRating['voted'] = $voted;
302
            // YouTube Liking  ==========================================
303
        } elseif (Constants::RATING_LIKES === $ratingbarsValue) {
304
            // get count of "dislikes"
305
            $criteria = new \CriteriaCompo();
306
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
307
            $criteria->add(new \Criteria(static::SOURCE, $source));
308
            $criteria->add(new \Criteria('rate', 0, '<'));
309
310
            $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
311
            $count    = \count($voteObjs);
312
313
            foreach ($voteObjs as $voteObj) {
314
                $currentRating += $voteObj->getVar('rate');
315
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
316
                    $voted            = true;
317
                    $itemRating['id'] = $voteObj->getVar('ratingid');
318
                }
319
            }
320
            unset($criteria);
321
            $itemRating['dislikes'] = $count;
322
323
            // get count of "likes"
324
            $criteria = new \CriteriaCompo();
325
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
326
            $criteria->add(new \Criteria(static::SOURCE, $source));
327
            $criteria->add(new \Criteria('rate', 0, '>'));
328
329
            $voteObjs      = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
330
            $count         = \count($voteObjs);
331
            $currentRating = 0;
332
            foreach ($voteObjs as $voteObj) {
333
                $currentRating += $voteObj->getVar('rate');
334
                if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) {
335
                    $voted            = true;
336
                    $itemRating['id'] = $voteObj->getVar('ratingid');
337
                }
338
            }
339
            unset($criteria);
340
            $itemRating['likes'] = $count;
341
342
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes'];
343
            $itemRating['ip']      = $ip;
344
            $itemRating['uid']     = $uid;
345
            $itemRating['voted']   = $voted;
346
            // Facebook Reactions  ==========================================
347
        } elseif (Constants::RATING_REACTION === $ratingbarsValue) {
348
            $criteria = new \CriteriaCompo();
349
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
350
            $criteria->add(new \Criteria(static::SOURCE, $source));
351
            $criteria->add(new \Criteria('rate', 1));
352
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
353
            $count                 = \count($voteObjs);
354
            $itemRating['likes'] = $count;
355
356
            $criteria = new \CriteriaCompo();
357
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
358
            $criteria->add(new \Criteria(static::SOURCE, $source));
359
            $criteria->add(new \Criteria('rate', 2));
360
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
361
            $count                 = \count($voteObjs);
362
            $itemRating['love'] = $count;
363
364
            $criteria = new \CriteriaCompo();
365
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
366
            $criteria->add(new \Criteria(static::SOURCE, $source));
367
            $criteria->add(new \Criteria('rate', 3));
368
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
369
            $count                 = \count($voteObjs);
370
            $itemRating['smile'] = $count;
371
372
            $criteria = new \CriteriaCompo();
373
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
374
            $criteria->add(new \Criteria(static::SOURCE, $source));
375
            $criteria->add(new \Criteria('rate', 4));
376
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
377
            $count                 = \count($voteObjs);
378
            $itemRating['wow'] = $count;
379
380
            $criteria = new \CriteriaCompo();
381
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
382
            $criteria->add(new \Criteria(static::SOURCE, $source));
383
            $criteria->add(new \Criteria('rate', 5));
384
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
385
            $count                 = \count($voteObjs);
386
            $itemRating['sad'] = $count;
387
388
            $criteria = new \CriteriaCompo();
389
            $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
390
            $criteria->add(new \Criteria(static::SOURCE, $source));
391
            $criteria->add(new \Criteria('rate', 6));
392
            $voteObjs              = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria);
393
            $count                 = \count($voteObjs);
394
            $itemRating['angry'] = $count;
395
396
397
            $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['love'] + $itemRating['smile'] + $itemRating['wow'] + $itemRating['sad'] + $itemRating['angry'];
398
            $itemRating['ip']      = $ip;
399
            $itemRating['uid']     = $uid;
400
            $itemRating['voted']   = $voted;
401
        } else {
402
            $itemRating['uid']     = $uid;
403
            $itemRating['nb_vote'] = $count;
404
            $itemRating['voted']   = $voted;
405
            $itemRating['ip']      = $ip;
406
        }
407
        return $itemRating;
408
    }
409
410
411
412
413
414
415
    /**
416
     * delete vote of given item
417
     * @param mixed $itemId
418
     * @param mixed $source
419
     * @return bool
420
     */
421
    public function deleteAllVote($itemId, $source): bool
422
    {
423
        $criteria = new \CriteriaCompo();
424
        $criteria->add(new \Criteria(static::IDENTIFIER, $itemId));
425
        $criteria->add(new \Criteria(static::SOURCE, $source));
426
427
        return $this->deleteAll($criteria);
428
    }
429
}
430