| Total Complexity | 43 |
| Total Lines | 281 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MessageHandler 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 MessageHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class MessageHandler extends \XoopsPersistableObjectHandler |
||
| 27 | { |
||
| 28 | /** @var \XoopsMySQLDatabase $db */ |
||
| 29 | public $db; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * MessageHandler constructor. |
||
| 33 | * @param \XoopsDatabase|null $db |
||
| 34 | */ |
||
| 35 | public function __construct(\XoopsDatabase $db = null) |
||
| 36 | { |
||
| 37 | $this->db = $db; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param bool $isNew |
||
| 42 | * @return Message |
||
| 43 | */ |
||
| 44 | public function create($isNew = true) |
||
| 45 | { |
||
| 46 | return new Message(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param int|null $id |
||
| 51 | * @param null $fields |
||
|
|
|||
| 52 | * @return bool|string |
||
| 53 | */ |
||
| 54 | public function get($id = null, $fields = null) |
||
| 55 | { |
||
| 56 | $id = (int)$id; |
||
| 57 | if ($id > 0) { |
||
| 58 | $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg') . ' WHERE msg_id=' . $id; |
||
| 59 | if (!$result = $this->db->query($sql)) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | $numrows = $this->db->getRowsNum($result); |
||
| 63 | if (1 == $numrows) { |
||
| 64 | $msg = new Message(); |
||
| 65 | $msg->assignVars($this->db->fetchArray($result)); |
||
| 66 | |||
| 67 | return $msg; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param \XoopsObject $msg |
||
| 76 | * @param bool $force |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function insert(\XoopsObject $msg, $force = true) |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param \XoopsObject $msg |
||
| 172 | * @param bool $force |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function delete(\XoopsObject $msg, $force = false) |
||
| 176 | { |
||
| 177 | global $xoopsModule; |
||
| 178 | if (Message::class !== \get_class($msg)) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | $sql = \sprintf('DELETE FROM `%s` WHERE msg_id = %u', $this->db->prefix('xfguestbook_msg'), $msg->getVar('msg_id')); |
||
| 182 | if (isset($this->commentstable) && '' !== $this->commentstable) { |
||
| 183 | \xoops_comment_delete($xoopsModule->getVar('mid'), $msg_id); |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!$result = $this->db->query($sql)) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param null|\CriteriaElement|\CriteriaCompo $criteria |
||
| 195 | * @param bool $id_as_key |
||
| 196 | * @param bool $as_object |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function &getObjects(\CriteriaElement $criteria = null, $id_as_key = false, $as_object = true)//getObjects(\CriteriaElement $criteria = null) |
||
| 200 | { |
||
| 201 | $ret = []; |
||
| 202 | $limit = $start = 0; |
||
| 203 | $sql = 'SELECT * FROM ' . $this->db->prefix('xfguestbook_msg'); |
||
| 204 | if (null !== $criteria && $criteria instanceof \CriteriaElement) { |
||
| 205 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 206 | $sort = ('' !== $criteria->getSort()) ? $criteria->getSort() : 'msg_id'; |
||
| 207 | $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder(); |
||
| 208 | $limit = $criteria->getLimit(); |
||
| 209 | $start = $criteria->getStart(); |
||
| 210 | } |
||
| 211 | $result = $this->db->query($sql, $limit, $start); |
||
| 212 | if (!$result) { |
||
| 213 | return $ret; |
||
| 214 | } |
||
| 215 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 216 | $msg = new Message(); |
||
| 217 | $msg->assignVars($myrow); |
||
| 218 | $ret[] = $msg; |
||
| 219 | unset($msg); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $ret; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param null|\CriteriaElement|\CriteriaCompo $criteria |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function countMsg(\CriteriaElement $criteria = null) |
||
| 230 | { |
||
| 231 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xfguestbook_msg'); |
||
| 232 | if (null !== $criteria && $criteria instanceof \CriteriaElement) { |
||
| 233 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 234 | } |
||
| 235 | if (!$result = $this->db->query($sql)) { |
||
| 236 | return 0; |
||
| 237 | } |
||
| 238 | [$count] = $this->db->fetchRow($result); |
||
| 239 | |||
| 240 | return $count; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param null|\CriteriaElement|\CriteriaCompo $criteria |
||
| 245 | * @return array|bool |
||
| 246 | */ |
||
| 247 | public function countMsgByCountry(\CriteriaElement $criteria = null) |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param \CriteriaElement|\CriteriaCompo|null $criteria |
||
| 268 | * @return array|bool |
||
| 269 | */ |
||
| 270 | public function countMsgByGender(\CriteriaElement $criteria = null) |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param null|\CriteriaElement|\CriteriaCompo $criteria |
||
| 290 | * @return array|int |
||
| 291 | */ |
||
| 292 | public function getMsgImg(\CriteriaElement $criteria = null) |
||
| 307 | } |
||
| 308 | } |
||
| 309 |