| Total Complexity | 45 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RelgroupuserHandler 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 RelgroupuserHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class RelgroupuserHandler extends XoopsPersistableObjectHandler |
||
| 41 | { |
||
| 42 | public $helper; |
||
| 43 | |||
| 44 | public $isAdmin; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 49 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 50 | */ |
||
| 51 | |||
| 52 | public function __construct( |
||
| 53 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 54 | $helper = null |
||
| 55 | ) { |
||
| 56 | /** @var \XoopsModules\Suico\Helper $this ->helper */ |
||
| 57 | |||
| 58 | if (null === $helper) { |
||
| 59 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 60 | } else { |
||
| 61 | $this->helper = $helper; |
||
| 62 | } |
||
| 63 | |||
| 64 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 65 | |||
| 66 | parent::__construct($xoopsDatabase, 'suico_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id'); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * create a new Groups |
||
| 71 | * |
||
| 72 | * @param bool $isNew flag the new objects as "new"? |
||
| 73 | * @return \XoopsObject Groups |
||
| 74 | */ |
||
| 75 | |||
| 76 | public function create( |
||
| 77 | $isNew = true |
||
| 78 | ) { |
||
| 79 | $obj = parent::create($isNew); |
||
| 80 | |||
| 81 | if ($isNew) { |
||
| 82 | $obj->setNew(); |
||
| 83 | } else { |
||
| 84 | $obj->unsetNew(); |
||
| 85 | } |
||
| 86 | |||
| 87 | $obj->helper = $this->helper; |
||
| 88 | |||
| 89 | return $obj; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * retrieve a Relgroupuser |
||
| 94 | * |
||
| 95 | * @param int $id of the Relgroupuser |
||
| 96 | * @param null $fields |
||
| 97 | * @return mixed reference to the {@link Relgroupuser} object, FALSE if failed |
||
| 98 | */ |
||
| 99 | |||
| 100 | public function get2( |
||
| 101 | $id = null, |
||
| 102 | $fields = null |
||
| 103 | ) { |
||
| 104 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser') . ' WHERE rel_id=' . $id; |
||
| 105 | |||
| 106 | if (!$result = $this->db->query($sql)) { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | $numrows = $this->db->getRowsNum($result); |
||
| 111 | |||
| 112 | if (1 === $numrows) { |
||
| 113 | $suico_relgroupuser = new Relgroupuser(); |
||
| 114 | |||
| 115 | $suico_relgroupuser->assignVars($this->db->fetchArray($result)); |
||
| 116 | |||
| 117 | return $suico_relgroupuser; |
||
| 118 | } |
||
| 119 | |||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * insert a new Relgroupuser in the database |
||
| 125 | * |
||
| 126 | * @param \XoopsObject $xoopsObject reference to the {@link Relgroupuser} |
||
| 127 | * object |
||
| 128 | * @param bool $force |
||
| 129 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 130 | */ |
||
| 131 | |||
| 132 | public function insert2( |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * delete a Relgroupuser from the database |
||
| 206 | * |
||
| 207 | * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete |
||
| 208 | * @param bool $force |
||
| 209 | * @return bool FALSE if failed. |
||
| 210 | */ |
||
| 211 | |||
| 212 | public function delete( |
||
| 213 | XoopsObject $xoopsObject, |
||
| 214 | $force = false |
||
| 215 | ) { |
||
| 216 | if (!$xoopsObject instanceof Relgroupuser) { |
||
| 217 | return false; |
||
| 218 | } |
||
| 219 | |||
| 220 | $sql = \sprintf( |
||
| 221 | 'DELETE FROM %s WHERE rel_id = %u', |
||
| 222 | $this->db->prefix('suico_relgroupuser'), |
||
| 223 | $xoopsObject->getVar('rel_id') |
||
| 224 | ); |
||
| 225 | |||
| 226 | if ($force) { |
||
| 227 | $result = $this->db->queryF($sql); |
||
| 228 | } else { |
||
| 229 | $result = $this->db->query($sql); |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!$result) { |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | return true; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * retrieve suico_relgroupusers from the database |
||
| 241 | * |
||
| 242 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 243 | * @param bool $id_as_key use the UID as key for the array? |
||
| 244 | * @param bool $as_object |
||
| 245 | * @return array array of {@link Relgroupuser} objects |
||
| 246 | */ |
||
| 247 | |||
| 248 | public function &getObjects( |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * count suico_relgroupusers matching a condition |
||
| 296 | * |
||
| 297 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 298 | * @return int count of suico_relgroupusers |
||
| 299 | */ |
||
| 300 | |||
| 301 | public function getCount( |
||
| 302 | ?CriteriaElement $criteriaElement = null |
||
| 303 | ) { |
||
| 304 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_relgroupuser'); |
||
| 305 | |||
| 306 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 307 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 308 | } |
||
| 309 | |||
| 310 | $result = $this->db->query($sql); |
||
| 311 | |||
| 312 | if (!$result) { |
||
| 313 | return 0; |
||
| 314 | } |
||
| 315 | |||
| 316 | [$count] = $this->db->fetchRow($result); |
||
| 317 | |||
| 318 | return (int)$count; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * delete suico_relgroupusers matching a set of conditions |
||
| 323 | * |
||
| 324 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 325 | * @param bool $force |
||
| 326 | * @param bool $asObject |
||
| 327 | * @return bool FALSE if deletion failed |
||
| 328 | */ |
||
| 329 | |||
| 330 | public function deleteAll( |
||
| 331 | ?CriteriaElement $criteriaElement = null, |
||
| 332 | $force = true, |
||
| 333 | $asObject = false |
||
| 334 | ) { |
||
| 335 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_relgroupuser'); |
||
| 336 | |||
| 337 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 338 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 339 | } |
||
| 340 | |||
| 341 | if (!$result = $this->db->query($sql)) { |
||
| 342 | return false; |
||
| 343 | } |
||
| 344 | |||
| 345 | return true; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $countGroups |
||
| 350 | * @param null $criteria |
||
| 351 | * @param int $shuffle |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | |||
| 355 | public function getGroups( |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param $groupId |
||
| 417 | * @param $start |
||
| 418 | * @param $nbUsers |
||
| 419 | * @param int $isShuffle |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | |||
| 423 | public function getUsersFromGroup( |
||
| 468 | } |
||
| 469 | } |
||
| 470 |