| Total Complexity | 46 |
| Total Lines | 425 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like IshotHandler 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 IshotHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class IshotHandler extends XoopsPersistableObjectHandler |
||
| 45 | { |
||
| 46 | public $helper; |
||
| 47 | |||
| 48 | public $isAdmin; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 53 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 54 | */ |
||
| 55 | |||
| 56 | public function __construct( |
||
| 57 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 58 | $helper = null |
||
| 59 | ) { |
||
| 60 | /** @var \XoopsModules\Suico\Helper $this ->helper */ |
||
| 61 | |||
| 62 | if (null === $helper) { |
||
| 63 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 64 | } else { |
||
| 65 | $this->helper = $helper; |
||
| 66 | } |
||
| 67 | |||
| 68 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 69 | // parent::__construct($db, 'suico_groups', Image::class, 'group_id', 'group_title'); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * create a new Groups |
||
| 74 | * |
||
| 75 | * @param bool $isNew flag the new objects as "new"? |
||
| 76 | * @return \XoopsObject Groups |
||
| 77 | */ |
||
| 78 | |||
| 79 | public function create( |
||
| 80 | $isNew = true |
||
| 81 | ) { |
||
| 82 | $obj = parent::create($isNew); |
||
| 83 | |||
| 84 | if ($isNew) { |
||
| 85 | $obj->setNew(); |
||
| 86 | } else { |
||
| 87 | $obj->unsetNew(); |
||
| 88 | } |
||
| 89 | |||
| 90 | $obj->helper = $this->helper; |
||
| 91 | |||
| 92 | return $obj; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * retrieve a Ishot |
||
| 97 | * |
||
| 98 | * @param int $id of the Ishot |
||
| 99 | * @param null $fields |
||
| 100 | * @return mixed reference to the {@link Ishot} object, FALSE if failed |
||
| 101 | */ |
||
| 102 | |||
| 103 | public function get2( |
||
| 104 | $id = null, |
||
| 105 | $fields = null |
||
| 106 | ) { |
||
| 107 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_ishot') . ' WHERE cod_ishot=' . $id; |
||
| 108 | |||
| 109 | if (!$result = $this->db->query($sql)) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | |||
| 113 | $numrows = $this->db->getRowsNum($result); |
||
| 114 | |||
| 115 | if (1 === $numrows) { |
||
| 116 | $suico_ishot = new Ishot(); |
||
| 117 | |||
| 118 | $suico_ishot->assignVars($this->db->fetchArray($result)); |
||
| 119 | |||
| 120 | return $suico_ishot; |
||
| 121 | } |
||
| 122 | |||
| 123 | return false; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * insert a new Ishot in the database |
||
| 128 | * |
||
| 129 | * @param \XoopsObject $xoopsObject reference to the {@link Ishot} |
||
| 130 | * object |
||
| 131 | * @param bool $force |
||
| 132 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 133 | */ |
||
| 134 | |||
| 135 | public function insert2( |
||
| 136 | XoopsObject $xoopsObject, |
||
| 137 | $force = false |
||
| 138 | ) { |
||
| 139 | global $xoopsConfig; |
||
| 140 | |||
| 141 | if (!$xoopsObject instanceof Ishot) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!$xoopsObject->isDirty()) { |
||
| 146 | return true; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!$xoopsObject->cleanVars()) { |
||
| 150 | return false; |
||
| 151 | } |
||
| 152 | |||
| 153 | $cod_ishot = $uid_voter = $uid_voted = $ishot = ''; |
||
| 154 | |||
| 155 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 156 | ${$k} = $v; |
||
| 157 | } |
||
| 158 | |||
| 159 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 160 | |||
| 161 | if ($xoopsObject->isNew()) { |
||
| 162 | // ajout/modification d'un Ishot |
||
| 163 | |||
| 164 | $xoopsObject = new Ishot(); |
||
| 165 | |||
| 166 | $format = 'INSERT INTO %s (cod_ishot, uid_voter, uid_voted, ishot, DATE)'; |
||
| 167 | |||
| 168 | $format .= 'VALUES (%u, %u, %u, %u, %s)'; |
||
| 169 | |||
| 170 | $sql = \sprintf( |
||
| 171 | $format, |
||
| 172 | $this->db->prefix('suico_ishot'), |
||
| 173 | $cod_ishot, |
||
| 174 | $uid_voter, |
||
| 175 | $uid_voted, |
||
| 176 | $ishot, |
||
| 177 | $this->db->quoteString($date) |
||
| 178 | ); |
||
| 179 | |||
| 180 | $force = true; |
||
| 181 | } else { |
||
| 182 | $format = 'UPDATE %s SET '; |
||
| 183 | |||
| 184 | $format .= 'cod_ishot=%u, uid_voter=%u, uid_voted=%u, ishot=%u, date_created=%s'; |
||
| 185 | |||
| 186 | $format .= ' WHERE cod_ishot = %u'; |
||
| 187 | |||
| 188 | $sql = \sprintf( |
||
| 189 | $format, |
||
| 190 | $this->db->prefix('suico_ishot'), |
||
| 191 | $cod_ishot, |
||
| 192 | $uid_voter, |
||
| 193 | $uid_voted, |
||
| 194 | $ishot, |
||
| 195 | $this->db->quoteString($date), |
||
| 196 | $cod_ishot |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($force) { |
||
| 201 | $result = $this->db->queryF($sql); |
||
| 202 | } else { |
||
| 203 | $result = $this->db->query($sql); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (!$result) { |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | if (empty($cod_ishot)) { |
||
| 211 | $cod_ishot = $this->db->getInsertId(); |
||
| 212 | } |
||
| 213 | |||
| 214 | $xoopsObject->assignVar('cod_ishot', $cod_ishot); |
||
| 215 | |||
| 216 | return true; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * delete a Ishot from the database |
||
| 221 | * |
||
| 222 | * @param \XoopsObject $xoopsObject reference to the Ishot to delete |
||
| 223 | * @param bool $force |
||
| 224 | * @return bool FALSE if failed. |
||
| 225 | */ |
||
| 226 | |||
| 227 | public function delete( |
||
| 228 | XoopsObject $xoopsObject, |
||
| 229 | $force = false |
||
| 230 | ) { |
||
| 231 | if (!$xoopsObject instanceof Ishot) { |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | |||
| 235 | $sql = \sprintf( |
||
| 236 | 'DELETE FROM %s WHERE cod_ishot = %u', |
||
| 237 | $this->db->prefix('suico_ishot'), |
||
| 238 | $xoopsObject->getVar('cod_ishot') |
||
| 239 | ); |
||
| 240 | |||
| 241 | if ($force) { |
||
| 242 | $result = $this->db->queryF($sql); |
||
| 243 | } else { |
||
| 244 | $result = $this->db->query($sql); |
||
| 245 | } |
||
| 246 | |||
| 247 | if (!$result) { |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | |||
| 251 | return true; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * retrieve suico_ishots from the database |
||
| 256 | * |
||
| 257 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 258 | * @param bool $id_as_key use the UID as key for the array? |
||
| 259 | * @param bool $as_object |
||
| 260 | * @return array array of {@link Ishot} objects |
||
| 261 | */ |
||
| 262 | |||
| 263 | public function &getObjects( |
||
| 264 | ?CriteriaElement $criteriaElement = null, |
||
| 265 | $id_as_key = false, |
||
| 266 | $as_object = true |
||
| 267 | ) { |
||
| 268 | $ret = []; |
||
| 269 | |||
| 270 | $limit = $start = 0; |
||
| 271 | |||
| 272 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_ishot'); |
||
| 273 | |||
| 274 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 275 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 276 | |||
| 277 | if ('' !== $criteriaElement->getSort()) { |
||
| 278 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 279 | } |
||
| 280 | |||
| 281 | $limit = $criteriaElement->getLimit(); |
||
| 282 | |||
| 283 | $start = $criteriaElement->getStart(); |
||
| 284 | } |
||
| 285 | |||
| 286 | $result = $this->db->query($sql, $limit, $start); |
||
| 287 | |||
| 288 | if (!$result) { |
||
| 289 | return $ret; |
||
| 290 | } |
||
| 291 | |||
| 292 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 293 | $suico_ishot = new Ishot(); |
||
| 294 | |||
| 295 | $suico_ishot->assignVars($myrow); |
||
| 296 | |||
| 297 | if (!$id_as_key) { |
||
| 298 | $ret[] = &$suico_ishot; |
||
| 299 | } else { |
||
| 300 | $ret[$myrow['cod_ishot']] = &$suico_ishot; |
||
| 301 | } |
||
| 302 | |||
| 303 | unset($suico_ishot); |
||
| 304 | } |
||
| 305 | |||
| 306 | return $ret; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * count suico_ishots matching a condition |
||
| 311 | * |
||
| 312 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match |
||
| 313 | * @return int count of suico_ishots |
||
| 314 | */ |
||
| 315 | |||
| 316 | public function getCount( |
||
| 317 | ?CriteriaElement $criteriaElement = null |
||
| 318 | ) { |
||
| 319 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_ishot'); |
||
| 320 | |||
| 321 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 322 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 323 | } |
||
| 324 | |||
| 325 | $result = $this->db->query($sql); |
||
| 326 | |||
| 327 | if (!$result) { |
||
| 328 | return 0; |
||
| 329 | } |
||
| 330 | |||
| 331 | [$count] = $this->db->fetchRow($result); |
||
| 332 | |||
| 333 | return $count; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * delete suico_ishots matching a set of conditions |
||
| 338 | * |
||
| 339 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} |
||
| 340 | * @param bool $force |
||
| 341 | * @param bool $asObject |
||
| 342 | * @return bool FALSE if deletion failed |
||
| 343 | */ |
||
| 344 | |||
| 345 | public function deleteAll( |
||
| 346 | ?CriteriaElement $criteriaElement = null, |
||
| 347 | $force = true, |
||
| 348 | $asObject = false |
||
| 349 | ) { |
||
| 350 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_ishot'); |
||
| 351 | |||
| 352 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 353 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 354 | } |
||
| 355 | |||
| 356 | if (!$result = $this->db->query($sql)) { |
||
| 357 | return false; |
||
| 358 | } |
||
| 359 | |||
| 360 | return true; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param null $criteria |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | |||
| 368 | public function getHottest($criteria = null) |
||
| 369 | { |
||
| 370 | $sql = 'SELECT DISTINCTROW uname, user_avatar, uid_voted, COUNT(cod_ishot) AS qtd FROM ' . $this->db->prefix( |
||
| 371 | 'suico_ishot' |
||
| 372 | ) . ', ' . $this->db->prefix( |
||
| 373 | 'users' |
||
| 374 | ); |
||
| 375 | |||
| 376 | if (isset($criteria) && $criteria instanceof CriteriaElement) { |
||
| 377 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 378 | } |
||
| 379 | |||
| 380 | //attention here this is kind of a hack |
||
| 381 | |||
| 382 | $sql .= ' AND uid = uid_voted'; |
||
| 383 | |||
| 384 | if ('' !== $criteria->getGroupby()) { |
||
| 385 | $sql .= $criteria->getGroupby(); |
||
| 386 | } |
||
| 387 | |||
| 388 | if ('' !== $criteria->getSort()) { |
||
| 389 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 390 | } |
||
| 391 | |||
| 392 | $limit = $criteria->getLimit(); |
||
| 393 | |||
| 394 | $start = $criteria->getStart(); |
||
| 395 | |||
| 396 | $result = $this->db->query($sql, $limit, $start); |
||
| 397 | |||
| 398 | $vetor = []; |
||
| 399 | |||
| 400 | $i = 0; |
||
| 401 | |||
| 402 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 403 | $vetor[$i]['qtd'] = $myrow['qtd']; |
||
| 404 | |||
| 405 | $vetor[$i]['uid_voted'] = $myrow['uid_voted']; |
||
| 406 | |||
| 407 | $vetor[$i]['uname'] = $myrow['uname']; |
||
| 408 | |||
| 409 | $vetor[$i]['user_avatar'] = $myrow['user_avatar']; |
||
| 410 | |||
| 411 | $i++; |
||
| 412 | } |
||
| 413 | |||
| 414 | return $vetor; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param null $criteria |
||
| 419 | * @param bool $id_as_key |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | |||
| 423 | public function getHotFriends( |
||
| 469 | } |
||
| 470 | } |
||
| 471 | } |
||
| 472 |