Total Complexity | 40 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like RatingsHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RatingsHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
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) |
||
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) |
||
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() |
||
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; |
||
|
|||
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']; |
||
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) |
||
248 | } |
||
249 | } |
||
250 |