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