Completed
Push — master ( 047d50...4f5633 )
by Michael
02:19
created

PublicRatingHandler::getRate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace XoopsModules\Extgallery;
2
3
/**
4
 * ExtGallery Class Manager
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
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @copyright   {@link https://xoops.org/ XOOPS Project}
14
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
15
 * @author      Zoullou (http://www.zoullou.net)
16
 * @package     ExtGallery
17
 */
18
19
use XoopsModules\Extgallery;
20
21
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
23
24
/**
25
 * Class Extgallery\PublicRating
26
 */
27
class PublicRating extends \XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
{
29
    public $externalKey = [];
30
31
    /**
32
     * Extgallery\PublicRating constructor.
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $this->initVar('rating_id', XOBJ_DTYPE_INT, 0, false);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Extgallery\XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        $this->initVar('photo_id', XOBJ_DTYPE_INT, 0, false);
39
        $this->initVar('uid', XOBJ_DTYPE_INT, 0, false);
40
        $this->initVar('rating_rate', XOBJ_DTYPE_INT, 0, false);
41
42
        $this->externalKey['photo_id'] = [
43
            'className'      => 'PublicPhoto',
44
            'getMethodeName' => 'getPhoto',
45
            'keyName'        => 'photo',
46
            'core'           => false
47
        ];
48
        $this->externalKey['uid']      = [
49
            'className'      => 'User',
50
            'getMethodeName' => 'get',
51
            'keyName'        => 'user',
52
            'core'           => true
53
        ];
54
    }
55
56
    /**
57
     * @param $key
58
     *
59
     * @return mixed
60
     */
61
    public function getExternalKey($key)
62
    {
63
        return $this->externalKey[$key];
64
    }
65
}
66
67
/**
68
 * Class Extgallery\PublicRatingHandler
69
 */
70
class PublicRatingHandler extends Extgallery\PersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
71
{
72
    /**
73
     * @param \XoopsDatabase $db
74
     */
75
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
    {
77
        parent::__construct($db, 'extgallery_publicrating', 'PublicRating', 'rating_id');
78
    }
79
80
    /**
81
     * @param $photoId
82
     * @param $rating
83
     *
84
     * @return bool
85
     */
86
    public function rate($photoId, $rating)
87
    {
88
        /** @var Extgallery\PublicPhotoHandler $photoHandler */
89
        $photoHandler = xoops_getModuleHandler('publicphoto', 'extgallery');
0 ignored issues
show
Bug introduced by
The function xoops_getModuleHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

89
        $photoHandler = /** @scrutinizer ignore-call */ xoops_getModuleHandler('publicphoto', 'extgallery');
Loading history...
90
91
        $userId = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
92
        $rate   = $this->create();
93
        $rate->assignVar('photo_id', $photoId);
94
        $rate->assignVar('uid', $userId);
95
        $rate->assignVar('rating_rate', $rating);
96
97
        if ($this->hasRated($rate)) {
98
            return false;
99
        }
100
101
        if (!$this->insert($rate, true)) {
102
            return false;
103
        }
104
105
        return $photoHandler->updateNbRating($photoId);
106
    }
107
108
    /**
109
     * @param $photoId
110
     *
111
     * @return float
112
     */
113
    public function getRate($photoId)
114
    {
115
        $criteria = new \Criteria('photo_id', $photoId);
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
        $avg      = $this->getAvg($criteria, 'rating_rate');
117
118
        return round($avg);
119
    }
120
121
    /**
122
     * @param $rate
123
     *
124
     * @return bool
125
     */
126
    public function hasRated(&$rate)
127
    {
128
        // If the user is annonymous
129
        if (0 == $rate->getVar('uid')) {
130
            return false;
131
        }
132
133
        $criteria = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
134
        $criteria->add(new \Criteria('photo_id', $rate->getVar('photo_id')));
135
        $criteria->add(new \Criteria('uid', $rate->getVar('uid')));
136
137
        return $this->getCount($criteria) > 0;
138
    }
139
}
140