RldHandler::setLikeDislike()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 24
nop 2
dl 0
loc 31
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xooghost;
4
5
/**
6
 * Xooghost module
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       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @package         Xooghost
18
 * @since           2.6.0
19
 * @author          Laurent JEN (Aka DuGris)
20
 */
21
use Xoops\Core\Database\Connection;
22
23
/**
24
 * Class XooghostRldHandler
25
 */
26
class RldHandler extends \XoopsPersistableObjectHandler
27
{
28
    public $db;
29
30
    /**
31
     * @param null|\Xoops\Core\Database\Connection $db
32
     */
33
    public function __construct(Connection $db = null)
34
    {
35
        $this->db = $db;
36
        parent::__construct($db, 'xooghost_rld', Rld::class, 'xooghost_rld_id', 'xooghost_rld_page');
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public static function getInstance()
43
    {
44
        static $instance;
45
        if (!isset($instance)) {
46
            $class = __CLASS__;
47
            $instance = new $class($db);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db seems to be never defined.
Loading history...
48
        }
49
50
        return $instance;
51
    }
52
53
    /**
54
     * @param $page_id
55
     *
56
     * @return int
57
     */
58
    public function getVotes($page_id)
59
    {
60
        $criteria = new \CriteriaCompo();
61
        $criteria->add(new \Criteria('xooghost_rld_page', $page_id));
62
        $criteria->add(new \Criteria('xooghost_rld_rates', 0, '!='));
63
64
        return parent::getCount($criteria);
65
    }
66
67
    /**
68
     * @param $page_id
69
     *
70
     * @return int
71
     */
72
    public function getbyUser($page_id)
73
    {
74
        $xoops = \Xoops::getInstance();
75
        $uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0;
76
        $ip = $xoops->getEnv('REMOTE_ADDR');
77
78
        $criteria = new \CriteriaCompo();
79
        $criteria->add(new \Criteria('xooghost_rld_page', $page_id));
80
81
        $criteria2 = new \CriteriaCompo();
82
        $criteria2->add(new \Criteria('xooghost_rld_uid', $uid), 'OR');
0 ignored issues
show
Bug introduced by
It seems like $uid can also be of type string[]; however, parameter $value of Criteria::__construct() does only seem to accept string, 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

82
        $criteria2->add(new \Criteria('xooghost_rld_uid', /** @scrutinizer ignore-type */ $uid), 'OR');
Loading history...
83
        $criteria2->add(new \Criteria('xooghost_rld_ip', $ip), 'OR');
84
        $criteria->add($criteria2, 'AND');
85
        $tmp = $this->getObjects($criteria, false, false);
86
        if (0 != count($tmp)) {
87
            return $tmp[0]['xooghost_rld_rates'];
88
        }
89
90
        return 0;
91
    }
92
93
    /**
94
     * @param $page_id
95
     * @param $like_dislike
96
     *
97
     * @return bool
98
     */
99
    public function setLikeDislike($page_id, $like_dislike)
100
    {
101
        $xoops = \Xoops::getInstance();
102
        $uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0;
103
        $ip = $xoops->getEnv('REMOTE_ADDR');
104
        $like = (1 == $like_dislike) ? 1 : 0;
105
        $dislike = (0 == $like_dislike) ? 1 : 0;
106
107
        $criteria = new \CriteriaCompo();
108
        $criteria->add(new \Criteria('xooghost_rld_page', $page_id));
109
110
        $criteria2 = new \CriteriaCompo();
111
        $criteria2->add(new \Criteria('xooghost_rld_uid', $uid), 'OR');
0 ignored issues
show
Bug introduced by
It seems like $uid can also be of type string[]; however, parameter $value of Criteria::__construct() does only seem to accept string, 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

111
        $criteria2->add(new \Criteria('xooghost_rld_uid', /** @scrutinizer ignore-type */ $uid), 'OR');
Loading history...
112
        $criteria2->add(new \Criteria('xooghost_rld_ip', $ip), 'OR');
113
        $criteria->add($criteria2, 'AND');
114
        $tmp = $this->getObjects($criteria, false, false);
115
        if (0 == count($tmp)) {
116
            $rldObject = $this->create();
117
            $rldObject->setVar('xooghost_rld_page', $page_id);
118
            $rldObject->setVar('xooghost_rld_uid', $uid);
119
            $rldObject->setVar('xooghost_rld_time', time());
120
            $rldObject->setVar('xooghost_rld_ip', $ip);
121
            $rldObject->setVar('xooghost_rld_rates', 0);
122
            $rldObject->setVar('xooghost_rld_like', $like);
123
            $rldObject->setVar('xooghost_rld_dislike', $dislike);
124
            if ($this->insert($rldObject)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->insert($rldObject) of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
125
                return true;
126
            }
127
        }
128
129
        return false;
130
    }
131
132
    /**
133
     * @param $page_id
134
     * @param $vote
135
     *
136
     * @return array|bool
137
     */
138
    public function setRate($page_id, $vote)
139
    {
140
        $xoops = \Xoops::getInstance();
141
        $uid = $xoops->isUser() ? $xoops->user->getVar('uid') : 0;
142
        $ip = $xoops->getEnv('REMOTE_ADDR');
143
144
        $criteria = new \CriteriaCompo();
145
        $criteria->add(new \Criteria('xooghost_rld_page', $page_id));
146
147
        $criteria2 = new \CriteriaCompo();
148
        $criteria2->add(new \Criteria('xooghost_rld_uid', $uid), 'OR');
0 ignored issues
show
Bug introduced by
It seems like $uid can also be of type string[]; however, parameter $value of Criteria::__construct() does only seem to accept string, 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

148
        $criteria2->add(new \Criteria('xooghost_rld_uid', /** @scrutinizer ignore-type */ $uid), 'OR');
Loading history...
149
        $criteria2->add(new \Criteria('xooghost_rld_ip', $ip), 'OR');
150
        $criteria->add($criteria2, 'AND');
151
        $tmp = $this->getObjects($criteria, false, false);
152
        if (0 == count($tmp)) {
153
            $rldObject = $this->create();
154
            $rldObject->setVar('xooghost_rld_page', $page_id);
155
            $rldObject->setVar('xooghost_rld_uid', $uid);
156
            $rldObject->setVar('xooghost_rld_time', time());
157
            $rldObject->setVar('xooghost_rld_ip', $ip);
158
            $rldObject->setVar('xooghost_rld_rates', $vote);
159
            $rldObject->setVar('xooghost_rld_like', 0);
160
            $rldObject->setVar('xooghost_rld_dislike', 0);
161
            if ($tmp = $this->insert($rldObject)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $tmp is dead and can be removed.
Loading history...
162
                return $this->getAverage($page_id, $vote);
163
            }
164
        }
165
166
        return false;
167
    }
168
169
    /**
170
     * @param $page_id
171
     * @param $vote
172
     *
173
     * @return array
174
     */
175
    private function getAverage($page_id, $vote)
176
    {
177
        $criteria = new \CriteriaCompo();
178
        $criteria->add(new \Criteria('xooghost_rld_page', $page_id));
179
        $criteria->add(new \Criteria('xooghost_rld_rates', 0, '!='));
180
181
        $res = $this->getObjects($criteria, false, false);
182
        $rates = 0;
183
        $voters = 0;
184
        foreach ($res as $k => $v) {
185
            $rates += $v['xooghost_rld_rates'];
186
            ++$voters;
187
        }
188
189
        return ['voters' => $voters, 'average' => ($rates / $voters), 'vote' => $vote];
190
    }
191
}
192