| Total Complexity | 49 |
| Total Lines | 516 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like GroupsHandler 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 GroupsHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class GroupsHandler extends XoopsPersistableObjectHandler |
||
| 56 | { |
||
| 57 | public $helper; |
||
| 58 | |||
| 59 | public $isAdmin; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 64 | * @param \XoopsModules\Yogurt\Helper|null $helper |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 67 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 68 | $helper = null |
||
| 69 | ) { |
||
| 70 | /** @var \XoopsModules\Yogurt\Helper $this ->helper */ |
||
| 71 | if (null === $helper) { |
||
| 72 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 73 | } else { |
||
| 74 | $this->helper = $helper; |
||
| 75 | } |
||
| 76 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 77 | parent::__construct($xoopsDatabase, 'yogurt_groups', Groups::class, 'group_id', 'group_title'); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * create a new Groups |
||
| 82 | * |
||
| 83 | * @param bool $isNew flag the new objects as "new"? |
||
| 84 | * @return \XoopsObject Groups |
||
| 85 | */ |
||
| 86 | public function create( |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * retrieve a Groups |
||
| 102 | * |
||
| 103 | * @param int $id of the Groups |
||
| 104 | * @param null $fields |
||
| 105 | * @return mixed reference to the {@link Groups} object, FALSE if failed |
||
| 106 | */ |
||
| 107 | public function get2( |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * insert a new Groups in the database |
||
| 128 | * |
||
| 129 | * @param \XoopsObject $xoopsObject reference to the {@link Groups} |
||
| 130 | * object |
||
| 131 | * @param bool $force |
||
| 132 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 133 | */ |
||
| 134 | public function insert2( |
||
| 135 | XoopsObject $xoopsObject, |
||
| 136 | $force = false |
||
| 137 | ) { |
||
| 138 | global $xoopsConfig; |
||
| 139 | if (!$xoopsObject instanceof Groups) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | if (!$xoopsObject->isDirty()) { |
||
| 143 | return true; |
||
| 144 | } |
||
| 145 | if (!$xoopsObject->cleanVars()) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 149 | ${$k} = $v; |
||
| 150 | } |
||
| 151 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 152 | if ($xoopsObject->isNew()) { |
||
| 153 | // ajout/modification d'un Groups |
||
| 154 | $xoopsObject = new Groups(); |
||
| 155 | $format = 'INSERT INTO %s (group_id, owner_uid, group_title, group_desc, group_img)'; |
||
| 156 | $format .= 'VALUES (%u, %u, %s, %s, %s)'; |
||
| 157 | $sql = \sprintf( |
||
| 158 | $format, |
||
| 159 | $this->db->prefix('yogurt_groups'), |
||
| 160 | $group_id, |
||
| 161 | $owner_uid, |
||
| 162 | $this->db->quoteString($group_title), |
||
| 163 | $this->db->quoteString($group_desc), |
||
| 164 | $this->db->quoteString($group_img) |
||
| 165 | ); |
||
| 166 | $force = true; |
||
| 167 | } else { |
||
| 168 | $format = 'UPDATE %s SET '; |
||
| 169 | $format .= 'group_id=%u, owner_uid=%u, group_title=%s, group_desc=%s, group_img=%s'; |
||
| 170 | $format .= ' WHERE group_id = %u'; |
||
| 171 | $sql = \sprintf( |
||
| 172 | $format, |
||
| 173 | $this->db->prefix('yogurt_groups'), |
||
| 174 | $group_id, |
||
| 175 | $owner_uid, |
||
| 176 | $this->db->quoteString($group_title), |
||
| 177 | $this->db->quoteString($group_desc), |
||
| 178 | $this->db->quoteString($group_img), |
||
| 179 | $group_id |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | if ($force) { |
||
| 183 | $result = $this->db->queryF($sql); |
||
| 184 | } else { |
||
| 185 | $result = $this->db->query($sql); |
||
| 186 | } |
||
| 187 | if (!$result) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | if (empty($group_id)) { |
||
| 191 | $group_id = $this->db->getInsertId(); |
||
| 192 | } |
||
| 193 | $xoopsObject->assignVar('group_id', $group_id); |
||
| 194 | |||
| 195 | return true; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * delete a Groups from the database |
||
| 200 | * |
||
| 201 | * @param \XoopsObject $xoopsObject reference to the Groups to delete |
||
| 202 | * @param bool $force |
||
| 203 | * @return bool FALSE if failed. |
||
| 204 | */ |
||
| 205 | public function delete( |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * retrieve yogurt_groupss from the database |
||
| 231 | * |
||
| 232 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 233 | * @param bool $id_as_key use the UID as key for the array? |
||
| 234 | * @param bool $as_object |
||
| 235 | * @return array array of {@link Groups} objects |
||
| 236 | */ |
||
| 237 | public function &getObjects( |
||
| 238 | ?CriteriaElement $criteriaElement = null, |
||
| 239 | $id_as_key = false, |
||
| 240 | $as_object = true |
||
| 241 | ) { |
||
| 242 | $ret = []; |
||
| 243 | $limit = $start = 0; |
||
| 244 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_groups'); |
||
| 245 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 246 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 247 | if ('' !== $criteriaElement->getSort()) { |
||
| 248 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 249 | } |
||
| 250 | $limit = $criteriaElement->getLimit(); |
||
| 251 | $start = $criteriaElement->getStart(); |
||
| 252 | } |
||
| 253 | $result = $this->db->query($sql, $limit, $start); |
||
| 254 | if (!$result) { |
||
| 255 | return $ret; |
||
| 256 | } |
||
| 257 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 258 | $yogurt_groups = new Groups(); |
||
| 259 | $yogurt_groups->assignVars($myrow); |
||
| 260 | if (!$id_as_key) { |
||
| 261 | $ret[] = &$yogurt_groups; |
||
| 262 | } else { |
||
| 263 | $ret[$myrow['group_id']] = &$yogurt_groups; |
||
| 264 | } |
||
| 265 | unset($yogurt_groups); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $ret; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * retrieve yogurt_groupss from the database |
||
| 273 | * |
||
| 274 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 275 | * @param bool $id_as_key use the UID as key for the array? |
||
| 276 | * @return array array of {@link Groups} objects |
||
| 277 | */ |
||
| 278 | public function getGroups( |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * count yogurt_groupss matching a condition |
||
| 319 | * |
||
| 320 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 321 | * @return int count of yogurt_groupss |
||
| 322 | */ |
||
| 323 | public function getCount( |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * delete yogurt_groupss matching a set of conditions |
||
| 341 | * |
||
| 342 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 343 | * @param bool $force |
||
| 344 | * @param bool $asObject |
||
| 345 | * @return bool FALSE if deletion failed |
||
| 346 | */ |
||
| 347 | public function deleteAll( |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param $maxbytes |
||
| 365 | * @param $xoopsTpl |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function renderFormSubmit( |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $group |
||
| 396 | * @param $maxbytes |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function renderFormEdit( |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $group_title |
||
| 459 | * @param string $group_desc |
||
| 460 | * @param string $group_img |
||
| 461 | * @param string $path_upload |
||
| 462 | * @param int $maxfilebytes |
||
| 463 | * @param int $maxfilewidth |
||
| 464 | * @param int $maxfileheight |
||
| 465 | * @param int $change_img |
||
| 466 | * @param string $group |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | public function receiveGroup( |
||
| 573 |