| Total Complexity | 45 |
| Total Lines | 347 |
| 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 |
||
| 46 | class RelgroupuserHandler extends XoopsPersistableObjectHandler |
||
| 47 | { |
||
| 48 | public $helper; |
||
| 49 | |||
| 50 | public $isAdmin; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 55 | * @param \XoopsModules\Yogurt\Helper|null $helper |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 58 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 59 | $helper = null |
||
| 60 | ) { |
||
| 61 | /** @var \XoopsModules\Yogurt\Helper $this ->helper */ |
||
| 62 | if (null === $helper) { |
||
| 63 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 64 | } else { |
||
| 65 | $this->helper = $helper; |
||
| 66 | } |
||
| 67 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 68 | parent::__construct($xoopsDatabase, 'yogurt_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * create a new Groups |
||
| 73 | * |
||
| 74 | * @param bool $isNew flag the new objects as "new"? |
||
| 75 | * @return \XoopsObject Groups |
||
| 76 | */ |
||
| 77 | public function create( |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * retrieve a Relgroupuser |
||
| 93 | * |
||
| 94 | * @param int $id of the Relgroupuser |
||
| 95 | * @param null $fields |
||
| 96 | * @return mixed reference to the {@link Relgroupuser} object, FALSE if failed |
||
| 97 | */ |
||
| 98 | public function get2( |
||
| 99 | $id = null, |
||
| 100 | $fields = null |
||
| 101 | ) { |
||
| 102 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_relgroupuser') . ' WHERE rel_id=' . $id; |
||
| 103 | if (!$result = $this->db->query($sql)) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | $numrows = $this->db->getRowsNum($result); |
||
| 107 | if (1 === $numrows) { |
||
| 108 | $yogurt_relgroupuser = new Relgroupuser(); |
||
| 109 | $yogurt_relgroupuser->assignVars($this->db->fetchArray($result)); |
||
| 110 | |||
| 111 | return $yogurt_relgroupuser; |
||
| 112 | } |
||
| 113 | |||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * insert a new Relgroupuser in the database |
||
| 119 | * |
||
| 120 | * @param \XoopsObject $xoopsObject reference to the {@link Relgroupuser} |
||
| 121 | * object |
||
| 122 | * @param bool $force |
||
| 123 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 124 | */ |
||
| 125 | public function insert2( |
||
| 126 | XoopsObject $xoopsObject, |
||
| 127 | $force = false |
||
| 128 | ) { |
||
| 129 | global $xoopsConfig; |
||
| 130 | if (!$xoopsObject instanceof Relgroupuser) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | if (!$xoopsObject->isDirty()) { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | if (!$xoopsObject->cleanVars()) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 140 | ${$k} = $v; |
||
| 141 | } |
||
| 142 | $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 143 | if ($xoopsObject->isNew()) { |
||
| 144 | // ajout/modification d'un Relgroupuser |
||
| 145 | $xoopsObject = new Relgroupuser(); |
||
| 146 | $format = 'INSERT INTO %s (rel_id, rel_group_id, rel_user_uid)'; |
||
| 147 | $format .= 'VALUES (%u, %u, %u)'; |
||
| 148 | $sql = \sprintf($format, $this->db->prefix('yogurt_relgroupuser'), $relId, $rel_group_id, $rel_user_uid); |
||
| 149 | $force = true; |
||
| 150 | } else { |
||
| 151 | $format = 'UPDATE %s SET '; |
||
| 152 | $format .= 'rel_id=%u, rel_group_id=%u, rel_user_uid=%u'; |
||
| 153 | $format .= ' WHERE rel_id = %u'; |
||
| 154 | $sql = \sprintf( |
||
| 155 | $format, |
||
| 156 | $this->db->prefix('yogurt_relgroupuser'), |
||
| 157 | $relId, |
||
| 158 | $rel_group_id, |
||
| 159 | $rel_user_uid, |
||
| 160 | $relId |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | if ($force) { |
||
| 164 | $result = $this->db->queryF($sql); |
||
| 165 | } else { |
||
| 166 | $result = $this->db->query($sql); |
||
| 167 | } |
||
| 168 | if (!$result) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | if (empty($relId)) { |
||
| 172 | $relId = $this->db->getInsertId(); |
||
| 173 | } |
||
| 174 | $xoopsObject->assignVar('rel_id', $relId); |
||
| 175 | |||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * delete a Relgroupuser from the database |
||
| 181 | * |
||
| 182 | * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete |
||
| 183 | * @param bool $force |
||
| 184 | * @return bool FALSE if failed. |
||
| 185 | */ |
||
| 186 | public function delete( |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * retrieve yogurt_relgroupusers from the database |
||
| 212 | * |
||
| 213 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 214 | * @param bool $id_as_key use the UID as key for the array? |
||
| 215 | * @param bool $as_object |
||
| 216 | * @return array array of {@link Relgroupuser} objects |
||
| 217 | */ |
||
| 218 | public function &getObjects( |
||
| 219 | ?CriteriaElement $criteriaElement = null, |
||
| 220 | $id_as_key = false, |
||
| 221 | $as_object = true |
||
| 222 | ) { |
||
| 223 | $ret = []; |
||
| 224 | $limit = $start = 0; |
||
| 225 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_relgroupuser'); |
||
| 226 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 227 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 228 | if ('' !== $criteriaElement->getSort()) { |
||
| 229 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 230 | } |
||
| 231 | $limit = $criteriaElement->getLimit(); |
||
| 232 | $start = $criteriaElement->getStart(); |
||
| 233 | } |
||
| 234 | $result = $this->db->query($sql, $limit, $start); |
||
| 235 | if (!$result) { |
||
| 236 | return $ret; |
||
| 237 | } |
||
| 238 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 239 | $yogurt_relgroupuser = new Relgroupuser(); |
||
| 240 | $yogurt_relgroupuser->assignVars($myrow); |
||
| 241 | if (!$id_as_key) { |
||
| 242 | $ret[] = &$yogurt_relgroupuser; |
||
| 243 | } else { |
||
| 244 | $ret[$myrow['rel_id']] = &$yogurt_relgroupuser; |
||
| 245 | } |
||
| 246 | unset($yogurt_relgroupuser); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $ret; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * count yogurt_relgroupusers matching a condition |
||
| 254 | * |
||
| 255 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 256 | * @return int count of yogurt_relgroupusers |
||
| 257 | */ |
||
| 258 | public function getCount( |
||
| 259 | ?CriteriaElement $criteriaElement = null |
||
| 260 | ) { |
||
| 261 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_relgroupuser'); |
||
| 262 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 263 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 264 | } |
||
| 265 | $result = $this->db->query($sql); |
||
| 266 | if (!$result) { |
||
| 267 | return 0; |
||
| 268 | } |
||
| 269 | [$count] = $this->db->fetchRow($result); |
||
| 270 | |||
| 271 | return (int)$count; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * delete yogurt_relgroupusers matching a set of conditions |
||
| 276 | * |
||
| 277 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 278 | * @param bool $force |
||
| 279 | * @param bool $asObject |
||
| 280 | * @return bool FALSE if deletion failed |
||
| 281 | */ |
||
| 282 | public function deleteAll( |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $nbgroups |
||
| 300 | * @param null $criteria |
||
| 301 | * @param int $shuffle |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function getGroups( |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param $groupId |
||
| 352 | * @param $start |
||
| 353 | * @param $nbUsers |
||
| 354 | * @param int $isShuffle |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function getUsersFromGroup( |
||
| 393 | } |
||
| 394 | } |
||
| 395 |