| Total Complexity | 40 |
| Total Lines | 309 |
| Duplicated Lines | 0 % |
| Changes | 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 declare(strict_types=1); |
||
| 35 | class NotesHandler extends XoopsPersistableObjectHandler |
||
| 36 | { |
||
| 37 | public Helper $helper; |
||
| 38 | public $isAdmin; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 43 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 46 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 47 | $helper = null |
||
| 48 | ) { |
||
| 49 | /** @var \XoopsModules\Suico\Helper $this- >helper */ |
||
| 50 | if (null === $helper) { |
||
| 51 | $this->helper = Helper::getInstance(); |
||
| 52 | } else { |
||
| 53 | $this->helper = $helper; |
||
| 54 | } |
||
| 55 | $this->isAdmin = $this->helper->isUserAdmin(); |
||
| 56 | parent::__construct($xoopsDatabase, 'suico_notes', Notes::class, 'note_id', 'note_id'); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * create a new Groups |
||
| 61 | * |
||
| 62 | * @param bool $isNew flag the new objects as "new"? |
||
| 63 | * @return \XoopsObject Groups |
||
| 64 | */ |
||
| 65 | public function create( |
||
| 66 | $isNew = true |
||
| 67 | ) { |
||
| 68 | $obj = parent::create($isNew); |
||
| 69 | if ($isNew) { |
||
| 70 | $obj->setNew(); |
||
| 71 | } else { |
||
| 72 | $obj->unsetNew(); |
||
| 73 | } |
||
| 74 | $obj->helper = $this->helper; |
||
| 75 | |||
| 76 | return $obj; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * retrieve aNotes |
||
| 81 | * |
||
| 82 | * @param int|null $id of theNotes |
||
| 83 | * @param null $fields |
||
|
|
|||
| 84 | * @return false|\XoopsModules\Suico\Notes reference to the {@linkNotes} object, FALSE if failed |
||
| 85 | */ |
||
| 86 | public function get2( |
||
| 87 | $id = null, |
||
| 88 | $fields = null |
||
| 89 | ) { |
||
| 90 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_notes') . ' WHERE note_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_notes = new Notes(); |
||
| 97 | $suico_notes->assignVars($this->db->fetchArray($result)); |
||
| 98 | |||
| 99 | return $suico_notes; |
||
| 100 | } |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * insert a new Notes in the database |
||
| 107 | * |
||
| 108 | * @param \XoopsObject $object reference to the {@linkNotes} |
||
| 109 | * object |
||
| 110 | * @param bool $force |
||
| 111 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 112 | */ |
||
| 113 | public function insert2( |
||
| 114 | XoopsObject $object, |
||
| 115 | $force = false |
||
| 116 | ) { |
||
| 117 | global $xoopsConfig; |
||
| 118 | if (!$object instanceof Notes) { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | if (!$object->isDirty()) { |
||
| 122 | return true; |
||
| 123 | } |
||
| 124 | if (!$object->cleanVars()) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | $private = ''; |
||
| 128 | $date_created = ''; |
||
| 129 | $note_to = ''; |
||
| 130 | $note_from = ''; |
||
| 131 | $noteId = ''; |
||
| 132 | foreach ($object->cleanVars as $k => $v) { |
||
| 133 | ${$k} = $v; |
||
| 134 | } |
||
| 135 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 136 | if ($object->isNew()) { |
||
| 137 | // add / modify a Notes |
||
| 138 | $object = new Notes(); |
||
| 139 | $format = 'INSERT INTO %s (note_id, note_text, note_from, note_to, date_created, private)'; |
||
| 140 | $format .= 'VALUES (%u, %s, %u, %u, %u,%u)'; |
||
| 141 | $sql = \sprintf( |
||
| 142 | $format, |
||
| 143 | $this->db->prefix('suico_notes'), |
||
| 144 | $noteId, |
||
| 145 | $this->db->quoteString($note_text), |
||
| 146 | $note_from, |
||
| 147 | $note_to, |
||
| 148 | $date_created, |
||
| 149 | $private |
||
| 150 | ); |
||
| 151 | $force = true; |
||
| 152 | } else { |
||
| 153 | $format = 'UPDATE %s SET '; |
||
| 154 | $format .= 'note_id=%u, note_text=%s, note_from=%u, note_to=%u, date_created=%u, private=%u'; |
||
| 155 | $format .= ' WHERE note_id = %u'; |
||
| 156 | $sql = \sprintf( |
||
| 157 | $format, |
||
| 158 | $this->db->prefix('suico_notes'), |
||
| 159 | $noteId, |
||
| 160 | $this->db->quoteString($note_text), |
||
| 161 | $note_from, |
||
| 162 | $note_to, |
||
| 163 | $date_created, |
||
| 164 | $private, |
||
| 165 | $noteId |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | if ($force) { |
||
| 169 | $result = $this->db->queryF($sql); |
||
| 170 | } else { |
||
| 171 | $result = $this->db->query($sql); |
||
| 172 | } |
||
| 173 | if (!$result) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | if (empty($noteId)) { |
||
| 177 | $noteId = $this->db->getInsertId(); |
||
| 178 | } |
||
| 179 | $object->assignVar('note_id', $noteId); |
||
| 180 | |||
| 181 | return true; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * delete aNotes from the database |
||
| 186 | * |
||
| 187 | * @param \XoopsObject $object reference to theNotes to delete |
||
| 188 | * @param bool $force |
||
| 189 | * @return bool FALSE if failed. |
||
| 190 | */ |
||
| 191 | public function delete( |
||
| 192 | XoopsObject $object, |
||
| 193 | $force = false |
||
| 194 | ) { |
||
| 195 | if (!$object instanceof Notes) { |
||
| 196 | return false; |
||
| 197 | } |
||
| 198 | $sql = \sprintf( |
||
| 199 | 'DELETE FROM %s WHERE note_id = %u', |
||
| 200 | $this->db->prefix('suico_notes'), |
||
| 201 | (int)$object->getVar('note_id') |
||
| 202 | ); |
||
| 203 | if ($force) { |
||
| 204 | $result = $this->db->queryF($sql); |
||
| 205 | } else { |
||
| 206 | $result = $this->db->query($sql); |
||
| 207 | } |
||
| 208 | if (!$result) { |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | |||
| 212 | return true; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * retrieve suico_notes from the database |
||
| 217 | * |
||
| 218 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} conditions to be met |
||
| 219 | * @param bool $id_as_key use the UID as key for the array? |
||
| 220 | * @param bool $as_object |
||
| 221 | * @return array array of {@linkNotes} objects |
||
| 222 | */ |
||
| 223 | public function &getObjects( |
||
| 224 | ?CriteriaElement $criteria = null, |
||
| 225 | $id_as_key = false, |
||
| 226 | $as_object = true |
||
| 227 | ) { |
||
| 228 | $ret = []; |
||
| 229 | $start = 0; |
||
| 230 | $limit = 0; |
||
| 231 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_notes'); |
||
| 232 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 233 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 234 | if ('' !== $criteria->getSort()) { |
||
| 235 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 236 | } |
||
| 237 | $limit = $criteria->getLimit(); |
||
| 238 | $start = $criteria->getStart(); |
||
| 239 | } |
||
| 240 | $result = $this->db->query($sql, $limit, $start); |
||
| 241 | if (!$result) { |
||
| 242 | return $ret; |
||
| 243 | } |
||
| 244 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 245 | $suico_notes = new Notes(); |
||
| 246 | $suico_notes->assignVars($myrow); |
||
| 247 | if ($id_as_key) { |
||
| 248 | $ret[$myrow['note_id']] = &$suico_notes; |
||
| 249 | } else { |
||
| 250 | $ret[] = &$suico_notes; |
||
| 251 | } |
||
| 252 | unset($suico_notes); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $ret; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * count suico_notes matching a condition |
||
| 260 | * |
||
| 261 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} to match |
||
| 262 | * @return int count of suico_notes |
||
| 263 | */ |
||
| 264 | public function getCount( |
||
| 265 | ?CriteriaElement $criteria = null |
||
| 266 | ) { |
||
| 267 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_notes'); |
||
| 268 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 269 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 270 | } |
||
| 271 | $result = $this->db->query($sql); |
||
| 272 | if (!$result) { |
||
| 273 | return 0; |
||
| 274 | } |
||
| 275 | [$count] = $this->db->fetchRow($result); |
||
| 276 | |||
| 277 | return (int)$count; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * delete suico_notes matching a set of conditions |
||
| 282 | * |
||
| 283 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link CriteriaElement} |
||
| 284 | * @param bool $force |
||
| 285 | * @param bool $asObject |
||
| 286 | * @return bool FALSE if deletion failed |
||
| 287 | */ |
||
| 288 | public function deleteAll( |
||
| 289 | ?CriteriaElement $criteria = null, |
||
| 290 | $force = true, |
||
| 291 | $asObject = false |
||
| 292 | ) { |
||
| 293 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_notes'); |
||
| 294 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 295 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 296 | } |
||
| 297 | if (!$result = $this->db->query($sql)) { |
||
| 298 | return false; |
||
| 299 | } |
||
| 300 | |||
| 301 | return true; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param $countNotes |
||
| 306 | * @param \CriteriaElement|\CriteriaCompo|null $criteria |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function getNotes( |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 |