| Total Complexity | 46 |
| Total Lines | 342 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like IshotHandler 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 IshotHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 42 | class IshotHandler extends XoopsPersistableObjectHandler |
||
| 43 | { |
||
| 44 | public $helper; |
||
| 45 | |||
| 46 | public $isAdmin; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor |
||
| 50 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 51 | * @param \XoopsModules\Yogurt\Helper|null $helper |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 64 | // parent::__construct($db, 'yogurt_groups', Image::class, 'group_id', 'group_title'); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * create a new Groups |
||
| 69 | * |
||
| 70 | * @param bool $isNew flag the new objects as "new"? |
||
| 71 | * @return \XoopsObject Groups |
||
| 72 | */ |
||
| 73 | public function create( |
||
| 74 | $isNew = true |
||
| 75 | ) { |
||
| 76 | $obj = parent::create($isNew); |
||
| 77 | if ($isNew) { |
||
| 78 | $obj->setNew(); |
||
| 79 | } else { |
||
| 80 | $obj->unsetNew(); |
||
| 81 | } |
||
| 82 | $obj->helper = $this->helper; |
||
| 83 | |||
| 84 | return $obj; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * retrieve a Ishot |
||
| 89 | * |
||
| 90 | * @param int $id of the Ishot |
||
| 91 | * @param null $fields |
||
| 92 | * @return mixed reference to the {@link Ishot} object, FALSE if failed |
||
| 93 | */ |
||
| 94 | public function get2( |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * insert a new Ishot in the database |
||
| 115 | * |
||
| 116 | * @param \XoopsObject $xoopsObject reference to the {@link Ishot} |
||
| 117 | * object |
||
| 118 | * @param bool $force |
||
| 119 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 120 | */ |
||
| 121 | public function insert2( |
||
| 122 | XoopsObject $xoopsObject, |
||
| 123 | $force = false |
||
| 124 | ) { |
||
| 125 | global $xoopsConfig; |
||
| 126 | if (!$xoopsObject instanceof Ishot) { |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | if (!$xoopsObject->isDirty()) { |
||
| 130 | return true; |
||
| 131 | } |
||
| 132 | if (!$xoopsObject->cleanVars()) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | $cod_ishot = $uid_voter = $uid_voted = $ishot = ''; |
||
| 137 | |||
| 138 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 139 | ${$k} = $v; |
||
| 140 | } |
||
| 141 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 142 | |||
| 143 | if ($xoopsObject->isNew()) { |
||
| 144 | // ajout/modification d'un Ishot |
||
| 145 | $xoopsObject = new Ishot(); |
||
| 146 | $format = 'INSERT INTO %s (cod_ishot, uid_voter, uid_voted, ishot, DATE)'; |
||
| 147 | $format .= 'VALUES (%u, %u, %u, %u, %s)'; |
||
| 148 | $sql = \sprintf( |
||
| 149 | $format, |
||
| 150 | $this->db->prefix('yogurt_ishot'), |
||
| 151 | $cod_ishot, |
||
| 152 | $uid_voter, |
||
| 153 | $uid_voted, |
||
| 154 | $ishot, |
||
| 155 | $this->db->quoteString($date) |
||
| 156 | ); |
||
| 157 | $force = true; |
||
| 158 | } else { |
||
| 159 | $format = 'UPDATE %s SET '; |
||
| 160 | $format .= 'cod_ishot=%u, uid_voter=%u, uid_voted=%u, ishot=%u, date_created=%s'; |
||
| 161 | $format .= ' WHERE cod_ishot = %u'; |
||
| 162 | $sql = \sprintf( |
||
| 163 | $format, |
||
| 164 | $this->db->prefix('yogurt_ishot'), |
||
| 165 | $cod_ishot, |
||
| 166 | $uid_voter, |
||
| 167 | $uid_voted, |
||
| 168 | $ishot, |
||
| 169 | $this->db->quoteString($date), |
||
| 170 | $cod_ishot |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | if ($force) { |
||
| 174 | $result = $this->db->queryF($sql); |
||
| 175 | } else { |
||
| 176 | $result = $this->db->query($sql); |
||
| 177 | } |
||
| 178 | if (!$result) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | if (empty($cod_ishot)) { |
||
| 182 | $cod_ishot = $this->db->getInsertId(); |
||
| 183 | } |
||
| 184 | $xoopsObject->assignVar('cod_ishot', $cod_ishot); |
||
| 185 | |||
| 186 | return true; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * delete a Ishot from the database |
||
| 191 | * |
||
| 192 | * @param \XoopsObject $xoopsObject reference to the Ishot to delete |
||
| 193 | * @param bool $force |
||
| 194 | * @return bool FALSE if failed. |
||
| 195 | */ |
||
| 196 | public function delete( |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * retrieve yogurt_ishots from the database |
||
| 222 | * |
||
| 223 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 224 | * @param bool $id_as_key use the UID as key for the array? |
||
| 225 | * @param bool $as_object |
||
| 226 | * @return array array of {@link Ishot} objects |
||
| 227 | */ |
||
| 228 | public function &getObjects( |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * count yogurt_ishots matching a condition |
||
| 264 | * |
||
| 265 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match |
||
| 266 | * @return int count of yogurt_ishots |
||
| 267 | */ |
||
| 268 | public function getCount( |
||
| 269 | ?CriteriaElement $criteriaElement = null |
||
| 270 | ) { |
||
| 271 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_ishot'); |
||
| 272 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 273 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 274 | } |
||
| 275 | $result = $this->db->query($sql); |
||
| 276 | if (!$result) { |
||
| 277 | return 0; |
||
| 278 | } |
||
| 279 | [$count] = $this->db->fetchRow($result); |
||
| 280 | |||
| 281 | return $count; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * delete yogurt_ishots matching a set of conditions |
||
| 286 | * |
||
| 287 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} |
||
| 288 | * @param bool $force |
||
| 289 | * @param bool $asObject |
||
| 290 | * @return bool FALSE if deletion failed |
||
| 291 | */ |
||
| 292 | public function deleteAll( |
||
| 293 | ?CriteriaElement $criteriaElement = null, |
||
| 294 | $force = true, |
||
| 295 | $asObject = false |
||
| 296 | ) { |
||
| 297 | $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_ishot'); |
||
| 298 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 299 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 300 | } |
||
| 301 | if (!$result = $this->db->query($sql)) { |
||
| 302 | return false; |
||
| 303 | } |
||
| 304 | |||
| 305 | return true; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param null $criteria |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getHottest($criteria = null) |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param null $criteria |
||
| 349 | * @param bool $id_as_key |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getHotFriends( |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 |