| Total Complexity | 47 |
| Total Lines | 340 |
| 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 declare(strict_types=1); |
||
| 33 | class ReadHandler extends \XoopsPersistableObjectHandler |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var \XoopsMySQLDatabase $db |
||
| 37 | */ |
||
| 38 | public $db; |
||
| 39 | /** |
||
| 40 | * Object type. |
||
| 41 | * <ul> |
||
| 42 | * <li>forum</li> |
||
| 43 | * <li>topic</li> |
||
| 44 | * </ul> |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | public string $type; |
||
| 49 | /** |
||
| 50 | * seconds records will persist. |
||
| 51 | * assigned from $GLOBALS['xoopsModuleConfig']["read_expire"] |
||
| 52 | * <ul> |
||
| 53 | * <li>positive days = delete all read records exist in the tables before expire time // irmtfan add comment</li> |
||
| 54 | * <li>0 = never expires // irmtfan change comment</li> |
||
| 55 | * <li>-1 or any negative days = never records // irmtfan change comment</li> |
||
| 56 | * </ul> |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | public $lifetime; |
||
| 61 | /** |
||
| 62 | * storage mode for records. |
||
| 63 | * assigned from $GLOBALS['xoopsModuleConfig']["read_mode"] |
||
| 64 | * <ul> |
||
| 65 | * <li>0 = never records</li> |
||
| 66 | * <li>1 = uses cookie</li> |
||
| 67 | * <li>2 = stores in database</li> |
||
| 68 | * </ul> |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | public $mode; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param null|\XoopsMySQLDatabase $db |
||
| 76 | * @param string $type |
||
| 77 | */ |
||
| 78 | public function __construct(\XoopsMySQLDatabase $db, $type) |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Clear garbage |
||
| 91 | * |
||
| 92 | * Delete all expired and duplicated records |
||
| 93 | */ |
||
| 94 | // START irmtfan rephrase function to 1- add clearDuplicate and 2- dont clean when read_expire = 0 |
||
| 95 | public function clearGarbage(): bool |
||
| 120 | } |
||
| 121 | |||
| 122 | // END irmtfan rephrase function to 1- add clearDuplicate and 2- don't clean when read_expire = 0 |
||
| 123 | /** |
||
| 124 | * @param int $read_item |
||
| 125 | * @param int|null $uid |
||
| 126 | * @return bool|int |
||
| 127 | */ |
||
| 128 | public function getRead(int $read_item, int $uid = null) |
||
| 129 | { |
||
| 130 | if (empty($this->mode)) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | if (1 == $this->mode) { |
||
| 134 | return $this->getReadCookie($read_item); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this->getReadDb($read_item, $uid); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param int $item_id |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | public function getReadCookie(int $item_id) |
||
| 145 | { |
||
| 146 | $cookie_name = ('forum' === $this->type) ? 'LF' : 'LT'; |
||
| 147 | $cookie_var = $item_id; |
||
| 148 | // irmtfan set true to return array |
||
| 149 | $lastview = \newbbGetCookie($cookie_name, true); |
||
| 150 | |||
| 151 | return @$lastview[$cookie_var]; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param int $read_item |
||
| 156 | * @param int|null $uid |
||
| 157 | * @return bool|int|null |
||
| 158 | */ |
||
| 159 | public function getReadDb(int $read_item, ?int $uid = null) |
||
| 160 | { |
||
| 161 | if (empty($uid)) { |
||
| 162 | if (\is_object($GLOBALS['xoopsUser'])) { |
||
| 163 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 164 | } else { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | $sql = 'SELECT post_id ' . ' FROM ' . $this->table . ' WHERE read_item = ' . (int)$read_item . ' AND uid = ' . (int)$uid; |
||
| 169 | $result = $this->db->queryF($sql, 1); |
||
| 170 | if (!$this->db->isResultSet($result)) { |
||
| 171 | // \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | [$post_id] = $this->db->fetchRow($result); |
||
| 176 | |||
| 177 | return (int)$post_id; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param int $read_item |
||
| 182 | * @param int $post_id |
||
| 183 | * @param int|null $uid |
||
| 184 | * @return bool|mixed|void |
||
| 185 | */ |
||
| 186 | public function setRead(int $read_item, int $post_id, int $uid = null) |
||
| 187 | { |
||
| 188 | if (empty($this->mode)) { |
||
| 189 | return true; |
||
| 190 | } |
||
| 191 | |||
| 192 | if (1 == $this->mode) { |
||
| 193 | return $this->setReadCookie($read_item, $post_id); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $this->setReadDb($read_item, $post_id, $uid); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param int $read_item |
||
| 201 | * @param int $post_id |
||
| 202 | */ |
||
| 203 | public function setReadCookie(int $read_item, int $post_id): void |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param int $read_item |
||
| 213 | * @param int|null $post_id |
||
| 214 | * @param int|null $uid |
||
| 215 | * @return bool|mixed |
||
| 216 | */ |
||
| 217 | public function setReadDb(int $read_item, int $post_id, ?int $uid = null) |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $items |
||
| 242 | * @param string|null $uid |
||
| 243 | * @return array|null |
||
| 244 | */ |
||
| 245 | public function isReadItems(array $items, string $uid = null): ?array |
||
| 246 | { |
||
| 247 | $ret = null; |
||
| 248 | if (empty($this->mode)) { |
||
| 249 | return null; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (1 == $this->mode) { |
||
| 253 | $ret = $this->isReadItemsCookie($items); |
||
| 254 | } else { |
||
| 255 | $ret = $this->isReadItemsDb($items, $uid); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $ret; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param array $items |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function isReadItemsCookie(array $items): array |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $items |
||
| 280 | * @param string|null $uid |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | public function isReadItemsDb(array $items, ?string $uid = null): array |
||
| 313 | } |
||
| 314 | |||
| 315 | // START irmtfan add clear duplicated rows function |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function clearDuplicate(): bool |
||
| 373 | } |
||
| 374 | // END irmtfan add clear duplicated rows function |
||
| 376 |