ReadforumHandler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setReadItems() 0 11 3
A cleanOrphan() 0 5 1
A setReadItemsDb() 0 24 5
A setReadItemsCookie() 0 15 3
A synchronization() 0 2 1
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
//
6
//  ------------------------------------------------------------------------ //
7
//                XOOPS - PHP Content Management System                      //
8
//                  Copyright (c) 2000-2020 XOOPS.org                        //
9
//                       <https://xoops.org>                             //
10
//  ------------------------------------------------------------------------ //
11
//  This program is free software; you can redistribute it and/or modify     //
12
//  it under the terms of the GNU General Public License as published by     //
13
//  the Free Software Foundation; either version 2 of the License, or        //
14
//  (at your option) any later version.                                      //
15
//                                                                           //
16
//  You may not change or alter any portion of this comment or credits       //
17
//  of supporting developers from this source code or any supporting         //
18
//  source code which is considered copyrighted (c) material of the          //
19
//  original comment or credit authors.                                      //
20
//                                                                           //
21
//  This program is distributed in the hope that it will be useful,          //
22
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
23
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
24
//  GNU General Public License for more details.                             //
25
//                                                                           //
26
//  You should have received a copy of the GNU General Public License        //
27
//  along with this program; if not, write to the Free Software              //
28
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
29
//  ------------------------------------------------------------------------ //
30
//  Author: phppp (D.J., [email protected])                                  //
31
//  URL: https://xoops.org                                                    //
32
//  Project: Article Project                                                 //
33
//  ------------------------------------------------------------------------ //
34
35
use XoopsModules\Newbb;
36
37
require_once __DIR__ . '/Read.php';
38
39
/**
40
 * A handler for read/unread handling
41
 *
42
 * @package       newbb
43
 *
44
 * @author        D.J. (phppp, http://xoopsforge.com)
45
 * @copyright     copyright (c) 2005 XOOPS.org
46
 */
47
48
/**
49
 * Class ReadforumHandler
50
 */
51
class ReadforumHandler extends Newbb\ReadHandler
52
{
53
    /**
54
     * @param \XoopsDatabase|null $db
55
     */
56
    public function __construct(\XoopsDatabase $db = null)
57
    {
58
        parent::__construct($db, 'forum');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Newbb\ReadHandler::__construct() does only seem to accept XoopsDatabase, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'forum');
Loading history...
59
    }
60
61
    /**
62
     * clean orphan items from database
63
     *
64
     * @param string $table_link
65
     * @param string $field_link
66
     * @param string $field_object
67
     * @return bool   true on success
68
     */
69
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
70
    {
71
        parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
72
73
        return parent::cleanOrphan($this->db->prefix('newbb_forums'), 'forum_id', 'read_item');
74
    }
75
76
    /**
77
     * @param int  $status
78
     * @param null $uid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uid is correct as it would always require null to be passed?
Loading history...
79
     * @return bool
80
     */
81
    public function setReadItems($status = 0, $uid = null)
82
    {
83
        if (empty($this->mode)) {
84
            return true;
85
        }
86
87
        if (1 == $this->mode) {
88
            return $this->setReadItemsCookie($status);
0 ignored issues
show
Bug introduced by
The call to XoopsModules\Newbb\Readf...r::setReadItemsCookie() has too few arguments starting with items. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            return $this->/** @scrutinizer ignore-call */ setReadItemsCookie($status);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89
        }
90
91
        return $this->setReadItemsDb($status, $uid);
92
    }
93
94
    /**
95
     * @param $status
96
     * @param $items
97
     * @return bool
98
     */
99
    public function setReadItemsCookie($status, $items)
0 ignored issues
show
Unused Code introduced by
The parameter $items is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function setReadItemsCookie($status, /** @scrutinizer ignore-unused */ $items)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $cookie_name = 'LF';
102
        $items       = [];
103
        if (!empty($status)) {
104
            /** @var Newbb\ForumHandler $itemHandler */
105
            $itemHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
106
            $items_id    = $itemHandler->getIds();
107
            foreach ($items_id as $key) {
108
                $items[$key] = \time();
109
            }
110
        }
111
        \newbbSetCookie($cookie_name, $items);
112
113
        return true;
114
    }
115
116
    /**
117
     * @param $status
118
     * @param $uid
119
     * @return bool
120
     */
121
    public function setReadItemsDb($status, $uid)
122
    {
123
        if (empty($uid)) {
124
            if (\is_object($GLOBALS['xoopsUser'])) {
125
                $uid = $GLOBALS['xoopsUser']->getVar('uid');
126
            } else {
127
                return false;
128
            }
129
        }
130
        if (empty($status)) {
131
            $this->deleteAll(new \Criteria('uid', $uid));
132
133
            return true;
134
        }
135
136
        /** @var Newbb\ForumHandler $itemHandler */
137
        $itemHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
138
        $itemsObject = $itemHandler->getAll(null, ['forum_last_post_id']);
139
        foreach (\array_keys($itemsObject) as $key) {
140
            $this->setReadDb($key, $itemsObject[$key]->getVar('forum_last_post_id'), $uid);
141
        }
142
        unset($itemsObject);
143
144
        return true;
145
    }
146
147
    /**
148
     * @return bool|void
149
     */
150
    public function synchronization()
151
    {
152
        //        return;
153
    }
154
}
155