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