| Total Complexity | 40 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like NotesHandler 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 NotesHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class NotesHandler extends XoopsPersistableObjectHandler |
||
| 49 | { |
||
| 50 | public $helper; |
||
| 51 | |||
| 52 | public $isAdmin; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor |
||
| 56 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 57 | * @param \XoopsModules\Yogurt\Helper|null $helper |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 60 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 61 | $helper = null |
||
| 62 | ) { |
||
| 63 | /** @var \XoopsModules\Yogurt\Helper $this ->helper */ |
||
| 64 | if (null === $helper) { |
||
| 65 | $this->helper = Helper::getInstance(); |
||
|
|
|||
| 66 | } else { |
||
| 67 | $this->helper = $helper; |
||
| 68 | } |
||
| 69 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 70 | parent::__construct($xoopsDatabase, 'yogurt_notes', Notes::class, 'note_id', 'note_id'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * create a new Groups |
||
| 75 | * |
||
| 76 | * @param bool $isNew flag the new objects as "new"? |
||
| 77 | * @return \XoopsObject Groups |
||
| 78 | */ |
||
| 79 | public function create( |
||
| 80 | $isNew = true |
||
| 81 | ) { |
||
| 82 | $obj = parent::create($isNew); |
||
| 83 | if ($isNew) { |
||
| 84 | $obj->setNew(); |
||
| 85 | } else { |
||
| 86 | $obj->unsetNew(); |
||
| 87 | } |
||
| 88 | $obj->helper = $this->helper; |
||
| 89 | |||
| 90 | return $obj; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * retrieve aNotes |
||
| 95 | * |
||
| 96 | * @param int $id of theNotes |
||
| 97 | * @param null $fields |
||
| 98 | * @return mixed reference to the {@linkNotes} object, FALSE if failed |
||
| 99 | */ |
||
| 100 | public function get2( |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * insert a new Notes in the database |
||
| 121 | * |
||
| 122 | * @param \XoopsObject $xoopsObject reference to the {@linkNotes} |
||
| 123 | * object |
||
| 124 | * @param bool $force |
||
| 125 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 126 | */ |
||
| 127 | public function insert2( |
||
| 128 | XoopsObject $xoopsObject, |
||
| 129 | $force = false |
||
| 130 | ) { |
||
| 131 | global $xoopsConfig; |
||
| 132 | if (!$xoopsObject instanceof Notes) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | if (!$xoopsObject->isDirty()) { |
||
| 136 | return true; |
||
| 137 | } |
||
| 138 | if (!$xoopsObject->cleanVars()) { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | $noteId = $note_from = $note_to = $date_created = $private = ''; |
||
| 143 | |||
| 144 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 145 | ${$k} = $v; |
||
| 146 | } |
||
| 147 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 148 | |||
| 149 | if ($xoopsObject->isNew()) { |
||
| 150 | // add / modify a Notes |
||
| 151 | $xoopsObject = new Notes(); |
||
| 152 | $format = 'INSERT INTO %s (note_id, note_text, note_from, note_to, date_created, private)'; |
||
| 153 | $format .= 'VALUES (%u, %s, %u, %u, %u,%u)'; |
||
| 154 | $sql = \sprintf( |
||
| 155 | $format, |
||
| 156 | $this->db->prefix('yogurt_notes'), |
||
| 157 | $noteId, |
||
| 158 | $this->db->quoteString($note_text), |
||
| 159 | $note_from, |
||
| 160 | $note_to, |
||
| 161 | $date_created, |
||
| 162 | $private |
||
| 163 | ); |
||
| 164 | $force = true; |
||
| 165 | } else { |
||
| 166 | $format = 'UPDATE %s SET '; |
||
| 167 | $format .= 'note_id=%u, note_text=%s, note_from=%u, note_to=%u, date_created=%u, private=%u'; |
||
| 168 | $format .= ' WHERE note_id = %u'; |
||
| 169 | $sql = \sprintf( |
||
| 170 | $format, |
||
| 171 | $this->db->prefix('yogurt_notes'), |
||
| 172 | $noteId, |
||
| 173 | $this->db->quoteString($note_text), |
||
| 174 | $note_from, |
||
| 175 | $note_to, |
||
| 176 | $date_created, |
||
| 177 | $private, |
||
| 178 | $noteId |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | if ($force) { |
||
| 182 | $result = $this->db->queryF($sql); |
||
| 183 | } else { |
||
| 184 | $result = $this->db->query($sql); |
||
| 185 | } |
||
| 186 | if (!$result) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | if (empty($noteId)) { |
||
| 190 | $noteId = $this->db->getInsertId(); |
||
| 191 | } |
||
| 192 | $xoopsObject->assignVar('note_id', $noteId); |
||
| 193 | |||
| 194 | return true; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * delete aNotes from the database |
||
| 199 | * |
||
| 200 | * @param \XoopsObject $xoopsObject reference to theNotes to delete |
||
| 201 | * @param bool $force |
||
| 202 | * @return bool FALSE if failed. |
||
| 203 | */ |
||
| 204 | public function delete( |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * retrieve yogurt_notes from the database |
||
| 230 | * |
||
| 231 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} conditions to be met |
||
| 232 | * @param bool $id_as_key use the UID as key for the array? |
||
| 233 | * @param bool $as_object |
||
| 234 | * @return array array of {@linkNotes} objects |
||
| 235 | */ |
||
| 236 | public function &getObjects( |
||
| 237 | ?CriteriaElement $criteriaElement = null, |
||
| 238 | $id_as_key = false, |
||
| 239 | $as_object = true |
||
| 240 | ) { |
||
| 241 | $ret = []; |
||
| 242 | $limit = $start = 0; |
||
| 243 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_notes'); |
||
| 244 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 245 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 246 | if ('' !== $criteriaElement->getSort()) { |
||
| 247 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 248 | } |
||
| 249 | $limit = $criteriaElement->getLimit(); |
||
| 250 | $start = $criteriaElement->getStart(); |
||
| 251 | } |
||
| 252 | $result = $this->db->query($sql, $limit, $start); |
||
| 253 | if (!$result) { |
||
| 254 | return $ret; |
||
| 255 | } |
||
| 256 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 257 | $yogurt_notes = new Notes(); |
||
| 258 | $yogurt_notes->assignVars($myrow); |
||
| 259 | if (!$id_as_key) { |
||
| 260 | $ret[] = &$yogurt_notes; |
||
| 261 | } else { |
||
| 262 | $ret[$myrow['note_id']] = &$yogurt_notes; |
||
| 263 | } |
||
| 264 | unset($yogurt_notes); |
||
| 265 | } |
||
| 266 | |||
| 267 | return $ret; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * count yogurt_notes matching a condition |
||
| 272 | * |
||
| 273 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match |
||
| 274 | * @return int count of yogurt_notes |
||
| 275 | */ |
||
| 276 | public function getCount( |
||
| 277 | ?CriteriaElement $criteriaElement = null |
||
| 278 | ) { |
||
| 279 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_notes'); |
||
| 280 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 281 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 282 | } |
||
| 283 | $result = $this->db->query($sql); |
||
| 284 | if (!$result) { |
||
| 285 | return 0; |
||
| 286 | } |
||
| 287 | [$count] = $this->db->fetchRow($result); |
||
| 288 | |||
| 289 | return (int)$count; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * delete yogurt_notes matching a set of conditions |
||
| 294 | * |
||
| 295 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} |
||
| 296 | * @param bool $force |
||
| 297 | * @param bool $asObject |
||
| 298 | * @return bool FALSE if deletion failed |
||
| 299 | */ |
||
| 300 | public function deleteAll( |
||
| 301 | ?CriteriaElement $criteriaElement = null, |
||
| 302 | $force = true, |
||
| 303 | $asObject = false |
||
| 304 | ) { |
||
| 305 | $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_notes'); |
||
| 306 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 307 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 308 | } |
||
| 309 | if (!$result = $this->db->query($sql)) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | |||
| 313 | return true; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param $nbNotes |
||
| 318 | * @param \CriteriaElement|\CriteriaCompo|null $criteria |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | public function getNotes( |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 |