| Total Complexity | 45 |
| Total Lines | 329 |
| 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 |
||
| 36 | class RelgroupuserHandler extends XoopsPersistableObjectHandler |
||
| 37 | { |
||
| 38 | public $helper; |
||
| 39 | public $isAdmin; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constructor |
||
| 43 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 44 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 47 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 48 | $helper = null |
||
| 49 | ) { |
||
| 50 | /** @var \XoopsModules\Suico\Helper $this ->helper */ |
||
| 51 | if (null === $helper) { |
||
| 52 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 53 | } else { |
||
| 54 | $this->helper = $helper; |
||
| 55 | } |
||
| 56 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 57 | parent::__construct($xoopsDatabase, 'suico_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id'); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * create a new Groups |
||
| 62 | * |
||
| 63 | * @param bool $isNew flag the new objects as "new"? |
||
| 64 | * @return \XoopsObject Groups |
||
| 65 | */ |
||
| 66 | public function create( |
||
| 67 | $isNew = true |
||
| 68 | ) { |
||
| 69 | $obj = parent::create($isNew); |
||
| 70 | if ($isNew) { |
||
| 71 | $obj->setNew(); |
||
| 72 | } else { |
||
| 73 | $obj->unsetNew(); |
||
| 74 | } |
||
| 75 | $obj->helper = $this->helper; |
||
| 76 | return $obj; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * retrieve a Relgroupuser |
||
| 81 | * |
||
| 82 | * @param int $id of the Relgroupuser |
||
| 83 | * @param null $fields |
||
| 84 | * @return mixed reference to the {@link Relgroupuser} object, FALSE if failed |
||
| 85 | */ |
||
| 86 | public function get2( |
||
| 87 | $id = null, |
||
| 88 | $fields = null |
||
| 89 | ) { |
||
| 90 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser') . ' WHERE rel_id=' . $id; |
||
| 91 | if (!$result = $this->db->query($sql)) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | $numrows = $this->db->getRowsNum($result); |
||
| 95 | if (1 === $numrows) { |
||
| 96 | $suico_relgroupuser = new Relgroupuser(); |
||
| 97 | $suico_relgroupuser->assignVars($this->db->fetchArray($result)); |
||
| 98 | return $suico_relgroupuser; |
||
| 99 | } |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * insert a new Relgroupuser in the database |
||
| 105 | * |
||
| 106 | * @param \XoopsObject $xoopsObject reference to the {@link Relgroupuser} |
||
| 107 | * object |
||
| 108 | * @param bool $force |
||
| 109 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 110 | */ |
||
| 111 | public function insert2( |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * delete a Relgroupuser from the database |
||
| 166 | * |
||
| 167 | * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete |
||
| 168 | * @param bool $force |
||
| 169 | * @return bool FALSE if failed. |
||
| 170 | */ |
||
| 171 | public function delete( |
||
| 172 | XoopsObject $xoopsObject, |
||
| 173 | $force = false |
||
| 174 | ) { |
||
| 175 | if (!$xoopsObject instanceof Relgroupuser) { |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | $sql = \sprintf( |
||
| 179 | 'DELETE FROM %s WHERE rel_id = %u', |
||
| 180 | $this->db->prefix('suico_relgroupuser'), |
||
| 181 | $xoopsObject->getVar('rel_id') |
||
| 182 | ); |
||
| 183 | if ($force) { |
||
| 184 | $result = $this->db->queryF($sql); |
||
| 185 | } else { |
||
| 186 | $result = $this->db->query($sql); |
||
| 187 | } |
||
| 188 | if (!$result) { |
||
| 189 | return false; |
||
| 190 | } |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * retrieve suico_relgroupusers from the database |
||
| 196 | * |
||
| 197 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 198 | * @param bool $id_as_key use the UID as key for the array? |
||
| 199 | * @param bool $as_object |
||
| 200 | * @return array array of {@link Relgroupuser} objects |
||
| 201 | */ |
||
| 202 | public function &getObjects( |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * count suico_relgroupusers matching a condition |
||
| 237 | * |
||
| 238 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 239 | * @return int count of suico_relgroupusers |
||
| 240 | */ |
||
| 241 | public function getCount( |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * delete suico_relgroupusers matching a set of conditions |
||
| 258 | * |
||
| 259 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 260 | * @param bool $force |
||
| 261 | * @param bool $asObject |
||
| 262 | * @return bool FALSE if deletion failed |
||
| 263 | */ |
||
| 264 | public function deleteAll( |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $countGroups |
||
| 281 | * @param null $criteria |
||
| 282 | * @param int $shuffle |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getGroups( |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $groupId |
||
| 329 | * @param $start |
||
| 330 | * @param $nbUsers |
||
| 331 | * @param int $isShuffle |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function getUsersFromGroup( |
||
| 367 |