| Total Complexity | 48 |
| Total Lines | 336 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ReadHandler 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 ReadHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class ReadHandler extends \XoopsPersistableObjectHandler |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Object type. |
||
| 55 | * <ul> |
||
| 56 | * <li>forum</li> |
||
| 57 | * <li>topic</li> |
||
| 58 | * </ul> |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $type; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * seconds records will persist. |
||
| 66 | * assigned from $GLOBALS['xoopsModuleConfig']["read_expire"] |
||
| 67 | * <ul> |
||
| 68 | * <li>positive days = delete all read records exist in the tables before expire time // irmtfan add comment</li> |
||
| 69 | * <li>0 = never expires // irmtfan change comment</li> |
||
| 70 | * <li>-1 or any negative days = never records // irmtfan change comment</li> |
||
| 71 | * </ul> |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | public $lifetime; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * storage mode for records. |
||
| 79 | * assigned from $GLOBALS['xoopsModuleConfig']["read_mode"] |
||
| 80 | * <ul> |
||
| 81 | * <li>0 = never records</li> |
||
| 82 | * <li>1 = uses cookie</li> |
||
| 83 | * <li>2 = stores in database</li> |
||
| 84 | * </ul> |
||
| 85 | * |
||
| 86 | * @var integer |
||
| 87 | */ |
||
| 88 | public $mode; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param \XoopsDatabase $db |
||
| 92 | * @param $type |
||
| 93 | */ |
||
| 94 | public function __construct(\XoopsDatabase $db, $type) |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Clear garbage |
||
| 107 | * |
||
| 108 | * Delete all expired and duplicated records |
||
| 109 | */ |
||
| 110 | // START irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
| 111 | public function clearGarbage() |
||
| 112 | { |
||
| 113 | // irmtfan clear duplicaed rows |
||
| 114 | if (!$result = $this->clearDuplicate()) { |
||
|
|
|||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | $sql = 'DELETE bb FROM ' . $this->table . ' AS bb' . ' LEFT JOIN ' . $this->table . ' AS aa ON bb.read_item = aa.read_item ' . ' WHERE aa.post_id > bb.post_id'; |
||
| 119 | if (!$result = $this->db->queryF($sql)) { |
||
| 120 | //xoops_error($this->db->error()); |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | // irmtfan if read_expire = 0 dont clean |
||
| 124 | if (empty($this->lifetime)) { |
||
| 125 | return true; |
||
| 126 | } |
||
| 127 | // irmtfan move here and rephrase |
||
| 128 | $expire = \time() - (int)$this->lifetime; |
||
| 129 | $sql = 'DELETE FROM ' . $this->table . ' WHERE read_time < ' . $expire; |
||
| 130 | if (!$result = $this->db->queryF($sql)) { |
||
| 131 | //xoops_error($this->db->error()); |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | return true; |
||
| 136 | } |
||
| 137 | |||
| 138 | // END irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $read_item |
||
| 142 | * @param null $uid |
||
| 143 | * @return bool|mixed|null |
||
| 144 | */ |
||
| 145 | public function getRead($read_item, $uid = null) |
||
| 146 | { |
||
| 147 | if (empty($this->mode)) { |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | if (1 == $this->mode) { |
||
| 151 | return $this->getReadCookie($read_item); |
||
| 152 | } |
||
| 153 | |||
| 154 | return $this->getReadDb($read_item, $uid); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $item_id |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | public function getReadCookie($item_id) |
||
| 162 | { |
||
| 163 | $cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
||
| 164 | $cookie_var = $item_id; |
||
| 165 | // irmtfan set true to return array |
||
| 166 | $lastview = \newbbGetCookie($cookie_name, true); |
||
| 167 | |||
| 168 | return @$lastview[$cookie_var]; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $read_item |
||
| 173 | * @param $uid |
||
| 174 | * @return bool|null |
||
| 175 | */ |
||
| 176 | public function getReadDb($read_item, $uid) |
||
| 177 | { |
||
| 178 | if (empty($uid)) { |
||
| 179 | if (\is_object($GLOBALS['xoopsUser'])) { |
||
| 180 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 181 | } else { |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $sql = 'SELECT post_id ' . ' FROM ' . $this->table . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
||
| 186 | if (!$result = $this->db->queryF($sql, 1)) { |
||
| 187 | return null; |
||
| 188 | } |
||
| 189 | [$post_id] = $this->db->fetchRow($result); |
||
| 190 | |||
| 191 | return $post_id; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $read_item |
||
| 196 | * @param $post_id |
||
| 197 | * @param null $uid |
||
| 198 | * @return bool|mixed|void |
||
| 199 | */ |
||
| 200 | public function setRead($read_item, $post_id, $uid = null) |
||
| 201 | { |
||
| 202 | if (empty($this->mode)) { |
||
| 203 | return true; |
||
| 204 | } |
||
| 205 | |||
| 206 | if (1 == $this->mode) { |
||
| 207 | return $this->setReadCookie($read_item, $post_id); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $this->setReadDb($read_item, $post_id, $uid); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $read_item |
||
| 215 | * @param $post_id |
||
| 216 | */ |
||
| 217 | public function setReadCookie($read_item, $post_id) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $read_item |
||
| 227 | * @param $post_id |
||
| 228 | * @param $uid |
||
| 229 | * @return bool|mixed |
||
| 230 | */ |
||
| 231 | public function setReadDb($read_item, $post_id, $uid) |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param $items |
||
| 256 | * @param null $uid |
||
| 257 | * @return array|null |
||
| 258 | */ |
||
| 259 | public function isReadItems($items, $uid = null) |
||
| 260 | { |
||
| 261 | $ret = null; |
||
| 262 | if (empty($this->mode)) { |
||
| 263 | return $ret; |
||
| 264 | } |
||
| 265 | |||
| 266 | if (1 == $this->mode) { |
||
| 267 | $ret = $this->isReadItemsCookie($items); |
||
| 268 | } else { |
||
| 269 | $ret = $this->isReadItemsDb($items, $uid); |
||
| 270 | } |
||
| 271 | |||
| 272 | return $ret; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $items |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function isReadItemsCookie($items) |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $items |
||
| 294 | * @param $uid |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function isReadItemsDb($items, $uid) |
||
| 327 | } |
||
| 328 | |||
| 329 | // START irmtfan add clear duplicated rows function |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function clearDuplicate() |
||
| 387 | } |
||
| 388 | |||
| 391 |