Total Complexity | 92 |
Total Lines | 566 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TopicHandler 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 TopicHandler, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Newbb; |
||
22 | class TopicHandler extends \XoopsPersistableObjectHandler |
||
23 | { |
||
24 | /** |
||
25 | * @param \XoopsDatabase $db |
||
26 | */ |
||
27 | public function __construct(\XoopsDatabase $db) |
||
28 | { |
||
29 | parent::__construct($db, 'newbb_topics', Topic::class, 'topic_id', 'topic_title'); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param mixed $id |
||
34 | * @param null|array $fields |
||
35 | * @return mixed|null |
||
36 | */ |
||
37 | public function get($id = null, $fields = null) //get($id, $var = null) |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param \XoopsObject $object |
||
58 | * @param bool $force |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function insert(\XoopsObject $object, $force = true) |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param $object |
||
83 | * @param bool $force |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function approve($object, $force = false) |
||
87 | { |
||
88 | $topic_id = $object->getVar('topic_id'); |
||
89 | if ($force) { |
||
90 | $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . " SET approved = -1 WHERE topic_id = {$topic_id}"; |
||
91 | } else { |
||
92 | $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . " SET approved = 1 WHERE topic_id = {$topic_id}"; |
||
93 | } |
||
94 | if (!$result = $this->db->queryF($sql)) { |
||
95 | //xoops_error($this->db->error()); |
||
96 | return false; |
||
97 | } |
||
98 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
99 | $postsObject = $postHandler->getAll(new \Criteria('topic_id', $topic_id)); |
||
100 | foreach (array_keys($postsObject) as $post_id) { |
||
101 | $postHandler->approve($postsObject[$post_id]); |
||
102 | } |
||
103 | unset($postsObject); |
||
104 | $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats'); |
||
105 | $statsHandler->update($object->getVar('forum_id'), 'topic'); |
||
106 | |||
107 | return true; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * get previous/next topic |
||
112 | * |
||
113 | * @param integer $topic_id current topic ID |
||
114 | * @param integer $action |
||
115 | * <ul> |
||
116 | * <li> -1: previous </li> |
||
117 | * <li> 0: current </li> |
||
118 | * <li> 1: next </li> |
||
119 | * </ul> |
||
120 | * @param integer $forum_id the scope for moving |
||
121 | * <ul> |
||
122 | * <li> >0 : inside the forum </li> |
||
123 | * <li> <= 0: global </li> |
||
124 | * </ul> |
||
125 | * @access public |
||
126 | * @return mixed|null|\XoopsObject |
||
127 | */ |
||
128 | public function &getByMove($topic_id, $action, $forum_id = 0) |
||
129 | { |
||
130 | $topic = null; |
||
131 | if (!empty($action)) { |
||
132 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE 1=1' . (($forum_id > 0) ? ' AND forum_id=' . (int)$forum_id : '') . ' AND topic_id ' . (($action > 0) ? '>' : '<') . (int)$topic_id . ' ORDER BY topic_id ' . (($action > 0) ? 'ASC' : 'DESC') . ' LIMIT 1'; |
||
133 | if ($result = $this->db->query($sql)) { |
||
134 | if ($row = $this->db->fetchArray($result)) { |
||
135 | $topic = $this->create(false); |
||
136 | $topic->assignVars($row); |
||
137 | |||
138 | return $topic; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | $topic = $this->get($topic_id); |
||
143 | |||
144 | return $topic; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param $post_id |
||
149 | * @return null|\XoopsObject |
||
150 | */ |
||
151 | public function &getByPost($post_id) |
||
152 | { |
||
153 | $topic = null; |
||
154 | $sql = 'SELECT t.* FROM ' . $this->db->prefix('newbb_topics') . ' t, ' . $this->db->prefix('newbb_posts') . ' p |
||
155 | WHERE t.topic_id = p.topic_id AND p.post_id = ' . (int)$post_id; |
||
156 | $result = $this->db->query($sql); |
||
157 | if (!$result) { |
||
158 | //xoops_error($this->db->error()); |
||
159 | return $topic; |
||
160 | } |
||
161 | $row = $this->db->fetchArray($result); |
||
162 | $topic = $this->create(false); |
||
163 | $topic->assignVars($row); |
||
164 | |||
165 | return $topic; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param Topic $topic |
||
170 | * @param string $type |
||
171 | * @return mixed |
||
172 | */ |
||
173 | public function getPostCount(&$topic, $type = '') |
||
174 | { |
||
175 | switch ($type) { |
||
176 | case 'pending': |
||
177 | $approved = 0; |
||
178 | break; |
||
179 | case 'deleted': |
||
180 | $approved = -1; |
||
181 | break; |
||
182 | default: |
||
183 | $approved = 1; |
||
184 | break; |
||
185 | } |
||
186 | $criteria = new \CriteriaCompo(new \Criteria('topic_id', $topic->getVar('topic_id'))); |
||
187 | $criteria->add(new \Criteria('approved', $approved)); |
||
188 | /** @var Newbb\PostHandler $postHandler */ |
||
189 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
190 | $count = $postHandler->getCount($criteria); |
||
191 | |||
192 | return $count; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param $topic_id |
||
197 | * @return null|\Post |
||
198 | */ |
||
199 | public function &getTopPost($topic_id) |
||
200 | { |
||
201 | $post = null; |
||
202 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p, |
||
203 | ' . $this->db->prefix('newbb_posts_text') . ' t |
||
204 | WHERE |
||
205 | p.topic_id = ' . $topic_id . ' AND p.pid = 0 |
||
206 | AND t.post_id = p.post_id'; |
||
207 | |||
208 | $result = $this->db->query($sql); |
||
209 | if (!$result) { |
||
210 | //xoops_error($this->db->error()); |
||
211 | return $post; |
||
212 | } |
||
213 | /** @var Newbb\PostHandler $postHandler */ |
||
214 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
215 | $myrow = $this->db->fetchArray($result); |
||
216 | /** @var Newbb\Post $post */ |
||
217 | $post = $postHandler->create(false); |
||
218 | $post->assignVars($myrow); |
||
219 | |||
220 | return $post; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param $topic_id |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function getTopPostId($topic_id) |
||
228 | { |
||
229 | $sql = 'SELECT MIN(post_id) AS post_id FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id = ' . $topic_id . ' AND pid = 0'; |
||
230 | $result = $this->db->query($sql); |
||
231 | if (!$result) { |
||
232 | //xoops_error($this->db->error()); |
||
233 | return false; |
||
234 | } |
||
235 | list($post_id) = $this->db->fetchRow($result); |
||
236 | |||
237 | return $post_id; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param $topic |
||
242 | * @param string $order |
||
243 | * @param int $perpage |
||
244 | * @param $start |
||
245 | * @param int $post_id |
||
246 | * @param string $type |
||
247 | * @return array |
||
248 | */ |
||
249 | public function &getAllPosts(&$topic, $order = 'ASC', $perpage = 10, &$start, $post_id = 0, $type = '') |
||
250 | { |
||
251 | $ret = []; |
||
252 | $perpage = ((int)$perpage > 0) ? (int)$perpage : (empty($GLOBALS['xoopsModuleConfig']['posts_per_page']) ? 10 : $GLOBALS['xoopsModuleConfig']['posts_per_page']); |
||
253 | $start = (int)$start; |
||
254 | switch ($type) { |
||
255 | case 'pending': |
||
256 | $approveCriteria = ' AND p.approved = 0'; |
||
257 | break; |
||
258 | case 'deleted': |
||
259 | $approveCriteria = ' AND p.approved = -1'; |
||
260 | break; |
||
261 | default: |
||
262 | $approveCriteria = ' AND p.approved = 1'; |
||
263 | break; |
||
264 | } |
||
265 | |||
266 | if ($post_id) { |
||
267 | if ('DESC' === $order) { |
||
268 | $operator_for_position = '>'; |
||
269 | } else { |
||
270 | $order = 'ASC'; |
||
271 | $operator_for_position = '<'; |
||
272 | } |
||
273 | //$approveCriteria = ' AND approved = 1'; // any others? |
||
274 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_posts') . ' AS p WHERE p.topic_id=' . (int)$topic->getVar('topic_id') . $approveCriteria . " AND p.post_id $operator_for_position $post_id"; |
||
275 | $result = $this->db->query($sql); |
||
276 | if (!$result) { |
||
277 | //xoops_error($this->db->error()); |
||
278 | return $ret; |
||
279 | } |
||
280 | list($position) = $this->db->fetchRow($result); |
||
281 | $start = (int)($position / $perpage) * $perpage; |
||
282 | } |
||
283 | |||
284 | $sql = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p, ' . $this->db->prefix('newbb_posts_text') . ' t WHERE p.topic_id=' . $topic->getVar('topic_id') . ' AND p.post_id = t.post_id' . $approveCriteria . " ORDER BY p.post_id $order"; |
||
285 | $result = $this->db->query($sql, $perpage, $start); |
||
286 | if (!$result) { |
||
287 | //xoops_error($this->db->error()); |
||
288 | return $ret; |
||
289 | } |
||
290 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
291 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
292 | $post = $postHandler->create(false); |
||
293 | $post->assignVars($myrow); |
||
294 | $ret[$myrow['post_id']] = $post; |
||
295 | unset($post); |
||
296 | } |
||
297 | |||
298 | return $ret; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param $postArray |
||
303 | * @param int $pid |
||
304 | * @return mixed |
||
305 | */ |
||
306 | public function &getPostTree(&$postArray, $pid = 0) |
||
307 | { |
||
308 | // include_once $GLOBALS['xoops']->path('modules/newbb/class/Tree.php'); |
||
309 | $NewBBTree = new Newbb\Tree('newbb_posts'); |
||
310 | $NewBBTree->setPrefix(' '); |
||
311 | $NewBBTree->setPostArray($postArray); |
||
312 | $NewBBTree->getPostTree($postsArray, $pid); |
||
313 | |||
314 | return $postsArray; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param $topic |
||
319 | * @param $postArray |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function showTreeItem(&$topic, &$postArray) |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @param $topic |
||
351 | * @param bool $isApproved |
||
352 | * @return array |
||
353 | */ |
||
354 | public function &getAllPosters(&$topic, $isApproved = true) |
||
355 | { |
||
356 | $sql = 'SELECT DISTINCT uid FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=' . $topic->getVar('topic_id') . ' AND uid>0'; |
||
357 | if ($isApproved) { |
||
358 | $sql .= ' AND approved = 1'; |
||
359 | } |
||
360 | $result = $this->db->query($sql); |
||
361 | if (!$result) { |
||
362 | //xoops_error($this->db->error()); |
||
363 | return []; |
||
364 | } |
||
365 | $ret = []; |
||
366 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
367 | $ret[] = $myrow['uid']; |
||
368 | } |
||
369 | |||
370 | return $ret; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @param \XoopsObject $topic |
||
375 | * @param bool $force |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function delete(\XoopsObject $topic, $force = true) |
||
379 | { |
||
380 | $topic_id = is_object($topic) ? $topic->getVar('topic_id') : (int)$topic; |
||
381 | if (empty($topic_id)) { |
||
382 | return false; |
||
383 | } |
||
384 | $postObject = $this->getTopPost($topic_id); |
||
385 | /** @var Newbb\PostHandler $postHandler */ |
||
386 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
387 | $postHandler->delete($postObject, false, $force); |
||
388 | |||
389 | $newbbConfig = newbbLoadConfig(); |
||
390 | /** @var \XoopsModules\Tag\Handler $tagHandler */ |
||
391 | if (!empty($newbbConfig['do_tag']) && $tagHandler = @xoops_getModuleHandler('tag', 'tag', true)) { |
||
392 | $tagHandler->updateByItem([], $topic_id, 'newbb'); |
||
393 | } |
||
394 | |||
395 | return true; |
||
396 | } |
||
397 | |||
398 | // get permission |
||
399 | // parameter: $type: 'post', 'view', 'reply', 'edit', 'delete', 'addpoll', 'vote', 'attach' |
||
400 | // $gperm_names = "'forum_can_post', 'forum_can_view', 'forum_can_reply', 'forum_can_edit', 'forum_can_delete', 'forum_can_addpoll', 'forum_can_vote', 'forum_can_attach', 'forum_can_noapprove'"; |
||
401 | /** |
||
402 | * @param Newbb\Forum $forum |
||
403 | * @param int $topic_locked |
||
404 | * @param string $type |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function getPermission($forum, $topic_locked = 0, $type = 'view') |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * clean orphan items from database |
||
433 | * |
||
434 | * @param string $table_link |
||
435 | * @param string $field_link |
||
436 | * @param string $field_object |
||
437 | * @return bool true on success |
||
438 | */ |
||
439 | public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan() |
||
440 | { |
||
441 | $this->deleteAll(new \Criteria('topic_time', 0), true, true); |
||
442 | parent::cleanOrphan($this->db->prefix('newbb_forums'), 'forum_id'); |
||
443 | parent::cleanOrphan($this->db->prefix('newbb_posts'), 'topic_id'); |
||
444 | |||
445 | return true; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * clean expired objects from database |
||
450 | * |
||
451 | * @param int $expire time limit for expiration |
||
452 | * @return bool true on success |
||
453 | */ |
||
454 | public function cleanExpires($expire = 0) |
||
455 | { |
||
456 | // irmtfan if 0 no cleanup look include/plugin.php |
||
457 | if (!func_num_args()) { |
||
458 | $newbbConfig = newbbLoadConfig(); |
||
459 | $expire = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7; |
||
460 | $expire = $expire * 24 * 3600; // days to seconds |
||
461 | } |
||
462 | if (empty($expire)) { |
||
463 | return false; |
||
464 | } |
||
465 | $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<=')); |
||
466 | $crit_expire->add(new \Criteria('topic_time', time() - (int)$expire, '<')); |
||
467 | |||
468 | return $this->deleteAll($crit_expire, true/*, true*/); |
||
469 | } |
||
470 | |||
471 | // START irmtfan - rewrite topic synchronization function. add pid sync and remove hard-code db access |
||
472 | |||
473 | /** |
||
474 | * @param null $object |
||
475 | * @param bool $force |
||
476 | * @return bool |
||
477 | */ |
||
478 | public function synchronization($object = null, $force = true) |
||
479 | { |
||
480 | if (!is_object($object)) { |
||
481 | $object = $this->get((int)$object); |
||
482 | } |
||
483 | if (!is_object($object) || !$object->getVar('topic_id')) { |
||
484 | return false; |
||
485 | } |
||
486 | |||
487 | /** @var Newbb\PostHandler $postHandler */ |
||
488 | $postHandler = Newbb\Helper::getInstance()->getHandler('Post'); |
||
489 | $criteria = new \CriteriaCompo(); |
||
490 | $criteria->add(new \Criteria('topic_id', $object->getVar('topic_id')), 'AND'); |
||
491 | $criteria->add(new \Criteria('approved', 1), 'AND'); |
||
492 | $post_ids = $postHandler->getIds($criteria); |
||
493 | if (empty($post_ids)) { |
||
494 | return false; |
||
495 | } |
||
496 | $last_post = max($post_ids); |
||
497 | $top_post = min($post_ids); |
||
498 | $topic_replies = count($post_ids) - 1; |
||
499 | if ($object->getVar('topic_last_post_id') != $last_post) { |
||
500 | $object->setVar('topic_last_post_id', $last_post); |
||
501 | } |
||
502 | if ($object->getVar('topic_replies') != $topic_replies) { |
||
503 | $object->setVar('topic_replies', $topic_replies); |
||
504 | } |
||
505 | $b1 = $this->insert($object, $force); |
||
506 | $criteria->add(new \Criteria('post_id', $top_post, '<>'), 'AND'); |
||
507 | $criteria->add(new \Criteria('pid', '(' . implode(', ', $post_ids) . ')', 'NOT IN'), 'AND'); |
||
508 | $b2 = $postHandler->updateAll('pid', $top_post, $criteria, $force); |
||
509 | $criteria = new \CriteriaCompo(); |
||
510 | $criteria->add(new \Criteria('post_id', $top_post, '='), 'AND'); |
||
511 | $b3 = $postHandler->updateAll('pid', 0, $criteria, $force); |
||
512 | |||
513 | return ($b1 && $b2 && $b3); |
||
514 | } |
||
515 | // END irmtfan - rewrite topic synchronization function. add pid sync and remove hard-code db access |
||
516 | // START irmtfan getActivePolls |
||
517 | /** |
||
518 | * get all active poll modules in the current xoops installtion. |
||
519 | * @access public |
||
520 | * @return array $pollDirs = array($dirname1=>$dirname1, $dirname2=>$dirname2, ...) dirnames of all active poll modules |
||
521 | */ |
||
522 | public function getActivePolls() |
||
523 | { |
||
524 | $pollDirs = []; |
||
525 | $allDirs = xoops_getActiveModules(); |
||
526 | foreach ($allDirs as $dirname) { |
||
527 | // pollresults.php file is exist in all xoopspoll versions and umfrage versions |
||
528 | if (file_exists($GLOBALS['xoops']->path('modules/' . $dirname . '/pollresults.php'))) { |
||
529 | $pollDirs[$dirname] = $dirname; |
||
530 | } |
||
531 | } |
||
532 | |||
533 | return $pollDirs; |
||
534 | } |
||
535 | // END irmtfan getActivePolls |
||
536 | |||
537 | // START irmtfan findPollModule |
||
538 | /** |
||
539 | * find poll module that is in used in the current newbb installtion. |
||
540 | * @access public |
||
541 | * @param array $pollDirs dirnames of all active poll modules |
||
542 | * @return bool|string $dir_def | true | false |
||
543 | * $dir_def: dirname of poll module that is in used in the current newbb installtion. |
||
544 | * true: no poll module is installed | newbb has no topic with poll | newbb has no topic |
||
545 | * false: errors (see below xoops_errors) |
||
546 | */ |
||
547 | public function findPollModule(array $pollDirs = []) |
||
588 | } |
||
589 | // END irmtfan findPollModule |
||
590 | } |
||
591 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.