PublicRatingHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Extgallery;
4
5
/**
6
 * ExtGallery Class Manager
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
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright   {@link https://xoops.org/ XOOPS Project}
16
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @author      Zoullou (http://www.zoullou.net)
18
 * @package     ExtGallery
19
 */
20
21
use XoopsModules\Extgallery;
22
23
/**
24
 * Class Extgallery\PublicRatingHandler
25
 */
26
class PublicRatingHandler extends Extgallery\PersistableObjectHandler
27
{
28
    /**
29
     * @param \XoopsDatabase|null $db
30
     */
31
    public function __construct(\XoopsDatabase $db = null)
32
    {
33
        parent::__construct($db, 'extgallery_publicrating', 'PublicRating', 'rating_id');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Extgallery\...tHandler::__construct() does only seem to accept XoopsDatabase, 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

33
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'extgallery_publicrating', 'PublicRating', 'rating_id');
Loading history...
34
    }
35
36
    /**
37
     * @param $photoId
38
     * @param $rating
39
     *
40
     * @return bool
41
     */
42
    public function rate($photoId, $rating)
43
    {
44
        /** @var Extgallery\PublicPhotoHandler $photoHandler */
45
        $photoHandler = $helper->getHandler('Publicphoto', 'extgallery');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $helper seems to be never defined.
Loading history...
46
47
        $userId = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
48
        $rate   = $this->create();
49
        $rate->assignVar('photo_id', $photoId);
50
        $rate->assignVar('uid', $userId);
51
        $rate->assignVar('rating_rate', $rating);
52
53
        if ($this->hasRated($rate)) {
54
            return false;
55
        }
56
57
        if (!$this->insert($rate, true)) {
58
            return false;
59
        }
60
61
        return $photoHandler->updateNbRating($photoId);
62
    }
63
64
    /**
65
     * @param $photoId
66
     *
67
     * @return float
68
     */
69
    public function getRate($photoId)
70
    {
71
        $criteria = new \Criteria('photo_id', $photoId);
72
        $avg      = $this->getAvg($criteria, 'rating_rate');
73
74
        return \round($avg);
75
    }
76
77
    /**
78
     * @param $rate
79
     *
80
     * @return bool
81
     */
82
    public function hasRated($rate)
83
    {
84
        // If the user is annonymous
85
        if (0 == $rate->getVar('uid')) {
86
            return false;
87
        }
88
89
        $criteria = new \CriteriaCompo();
90
        $criteria->add(new \Criteria('photo_id', $rate->getVar('photo_id')));
91
        $criteria->add(new \Criteria('uid', $rate->getVar('uid')));
92
93
        return $this->getCount($criteria) > 0;
94
    }
95
}
96