Passed
Pull Request — master (#70)
by Pierre-Henry
03:15
created

ReadtopicHandler::clearGarbage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 49 and the first side effect is on line 35.

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.

Loading history...
2
3
//
4
//  ------------------------------------------------------------------------ //
5
//                XOOPS - PHP Content Management System                      //
6
//                  Copyright (c) 2000-2016 XOOPS.org                        //
7
//                       <https://xoops.org/>                             //
8
//  ------------------------------------------------------------------------ //
9
//  This program is free software; you can redistribute it and/or modify     //
10
//  it under the terms of the GNU General Public License as published by     //
11
//  the Free Software Foundation; either version 2 of the License, or        //
12
//  (at your option) any later version.                                      //
13
//                                                                           //
14
//  You may not change or alter any portion of this comment or credits       //
15
//  of supporting developers from this source code or any supporting         //
16
//  source code which is considered copyrighted (c) material of the          //
17
//  original comment or credit authors.                                      //
18
//                                                                           //
19
//  This program is distributed in the hope that it will be useful,          //
20
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
21
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
22
//  GNU General Public License for more details.                             //
23
//                                                                           //
24
//  You should have received a copy of the GNU General Public License        //
25
//  along with this program; if not, write to the Free Software              //
26
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
27
//  ------------------------------------------------------------------------ //
28
//  Author: phppp (D.J., [email protected])                                  //
29
//  URL: https://xoops.org                                                    //
30
//  Project: Article Project                                                 //
31
//  ------------------------------------------------------------------------ //
32
33
use XoopsModules\Newbb;
34
35
require_once __DIR__ . '/read.php';
36
37
/**
38
 * A handler for read/unread handling
39
 *
40
 * @package       newbb
41
 *
42
 * @author        D.J. (phppp, http://xoopsforge.com)
43
 * @copyright     copyright (c) 2005 XOOPS.org
44
 */
45
46
/**
47
 * Class ReadtopicHandler
48
 */
49
class ReadtopicHandler extends ReadHandler
50
{
51
    /**
52
     * maximum records per forum for one user.
53
     * assigned from $GLOBALS['xoopsModuleConfig']["read_items"]
54
     *
55
     * @var integer
56
     */
57
    private $items_per_forum;
58
59
    /**
60
     * @param \XoopsDatabase|null $db
61
     */
62
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
    {
64
        parent::__construct($db, 'topic');
65
        $newbbConfig           = newbbLoadConfig();
66
        $this->items_per_forum = isset($newbbConfig['read_items']) ? (int)$newbbConfig['read_items'] : 100;
67
    }
68
69
    /**
70
     * clean orphan items from database
71
     *
72
     * @param  string $table_link
73
     * @param  string $field_link
74
     * @param  string $field_object
75
     * @return bool   true on success
76
     */
77
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
78
    {
79
        parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
80
81
        return parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id', 'read_item');
82
    }
83
84
    /**
85
     * Clear garbage
86
     *
87
     * Delete all expired and duplicated records
88
     */
89
    public function clearGarbage()
90
    {
91
        parent::clearGarbage();
92
93
        // TODO: clearItemsExceedMaximumItemsPerForum
94
        return true;
95
    }
96
97
    /**
98
     * @param  int  $status
99
     * @param  int  $forum_id
100
     * @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...
101
     * @return bool
102
     */
103
    public function setReadItems($status = 0, $forum_id = 0, $uid = null)
104
    {
105
        if (empty($this->mode)) {
106
            return true;
107
        }
108
109
        if (1 == $this->mode) {
110
            return $this->setReadItemsCookie($status, $forum_id);
111
        } else {
112
            return $this->setReadItemsDb($status, $forum_id, $uid);
113
        }
114
    }
115
116
    /**
117
     * @param $status
118
     * @param $forum_id
119
     * @return bool
120
     */
121
    public function setReadItemsCookie($status, $forum_id)
122
    {
123
        $cookie_name = 'LT';
124
        $cookie_vars = newbbGetCookie($cookie_name, true);
125
126
        /** @var Newbb\TopicHandler $itemHandler */
127
        $itemHandler = Newbb\Helper::getInstance()->getHandler('Topic');
128
        $criteria    = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
        $criteria->setSort('topic_last_post_id');
130
        $criteria->setOrder('DESC');
131
        $criteria->setLimit($this->items_per_forum);
132
        $items = $itemHandler->getIds($criteria);
133
134
        foreach ($items as $var) {
135
            if (empty($status)) {
136
                if (isset($cookie_vars[$var])) {
137
                    unset($cookie_vars[$var]);
138
                }
139
            } else {
140
                $cookie_vars[$var] = time() /*$items[$var]*/
141
                ;
142
            }
143
        }
144
        newbbSetCookie($cookie_name, $cookie_vars);
145
146
        return true;
147
    }
148
149
    /**
150
     * @param $status
151
     * @param $forum_id
152
     * @param $uid
153
     * @return bool
154
     */
155
    public function setReadItemsDb($status, $forum_id, $uid)
156
    {
157
        if (empty($uid)) {
158
            if (is_object($GLOBALS['xoopsUser'])) {
159
                $uid = $GLOBALS['xoopsUser']->getVar('uid');
160
            } else {
161
                return false;
162
            }
163
        }
164
165
        /** @var Newbb\TopicHandler $itemHandler */
166
        $itemHandler    = Newbb\Helper::getInstance()->getHandler('Topic');
167
        $criteria_topic = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
168
        $criteria_topic->setSort('topic_last_post_id');
169
        $criteria_topic->setOrder('DESC');
170
        $criteria_topic->setLimit($this->items_per_forum);
171
        $criteria_sticky = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
172
        $criteria_sticky->add(new \Criteria('topic_sticky', 1));
173
174
        if (empty($status)) {
175
            $items_id  = $itemHandler->getIds($criteria_topic);
176
            $sticky_id = $itemHandler->getIds($criteria_sticky);
177
            $items     = $items_id + $sticky_id;
178
            $criteria  = new \CriteriaCompo(new \Criteria('uid', $uid));
179
            $criteria->add(new \Criteria('read_item', '(' . implode(', ', $items) . ')', 'IN'));
180
            $this->deleteAll($criteria, true);
181
182
            return true;
183
        }
184
185
        $itemsObject  = $itemHandler->getAll($criteria_topic, ['topic_last_post_id']);
186
        $stickyObject = $itemHandler->getAll($criteria_sticky, ['topic_last_post_id']);
187
        $itemsObject  += $stickyObject;
188
        $items        = [];
189
        foreach (array_keys($itemsObject) as $key) {
190
            $items[$key] = $itemsObject[$key]->getVar('topic_last_post_id');
191
        }
192
        unset($itemsObject, $stickyObject);
193
194
        foreach (array_keys($items) as $key) {
195
            $this->setReadDb($key, $items[$key], $uid);
196
        }
197
198
        return true;
199
    }
200
201
    /**
202
     *
203
     */
204
    public function synchronization()
205
    {
206
        //        return;
207
    }
208
}
209