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