CategoryHandler   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getIdsByPermission() 0 6 1
A synchronization() 0 3 1
A delete() 0 17 3
A getByPermission() 0 11 2
A getPermission() 0 11 4
A deletePermission() 0 6 1
A insert() 0 12 3
A applyPermissionTemplate() 0 6 1
1
<?php
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB 5.0x,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 * @package        module::newbb
13
 */
14
15
use XoopsModules\Newbb;
16
17
 /**
18
 * Class CategoryHandler
19
 */
20
class CategoryHandler extends \XoopsPersistableObjectHandler
21
{
22
    /**
23
     * @param null|\XoopsDatabase $db
24
     */
25
    public function __construct(\XoopsDatabase $db = null)
26
    {
27
        parent::__construct($db, 'newbb_categories', Category::class, 'cat_id', 'cat_title');
28
    }
29
30
    /**
31
     * @param string $perm
32
     * @return mixed
33
     */
34
    public function getIdsByPermission($perm = 'access')
35
    {
36
        /** var Newbb\PermissionHandler $permHandler */
37
        $permHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Permission');
38
39
        return $permHandler->getCategories($perm);
40
    }
41
42
    /**
43
     * @param string $permission
44
     * @param null   $tags
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tags is correct as it would always require null to be passed?
Loading history...
45
     * @param bool   $asObject
46
     * @return array
47
     */
48
    public function &getByPermission($permission = 'access', $tags = null, $asObject = true)
49
    {
50
        $categories = [];
51
        if (!$valid_ids = $this->getIdsByPermission($permission)) {
52
            return $categories;
53
        }
54
        $criteria = new \Criteria('cat_id', '(' . \implode(', ', $valid_ids) . ')', 'IN');
55
        $criteria->setSort('cat_order');
56
        $categories = $this->getAll($criteria, $tags, $asObject);
57
58
        return $categories;
59
    }
60
61
    /**
62
     * @param \XoopsObject $category
63
     * @param bool         $force
64
     * @return mixed
65
     */
66
    public function insert(\XoopsObject $category, $force = true)
67
    {
68
        $className = Category::class;
69
        if (!($category instanceof $className)) {
70
            return false;
71
        }
72
        parent::insert($category, $force);
73
        if ($category->isNew()) {
74
            $this->applyPermissionTemplate($category);
75
        }
76
77
        return $category->getVar('cat_id');
78
    }
79
80
    /**
81
     * @param \XoopsObject $category
82
     * @param bool         $force
83
     * @return bool|mixed
84
     * @internal param Category $category
85
     */
86
    public function delete(\XoopsObject $category, $force = false)//delete(Category $category)
87
    {
88
        $className = Category::class;
89
        if (!($category instanceof $className)) {
90
            return false;
91
        }
92
        /** @var Newbb\ForumHandler $forumHandler */
93
        $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
94
        $forumHandler->deleteAll(new \Criteria('cat_id', $category->getVar('cat_id')), true, true);
0 ignored issues
show
Bug introduced by
It seems like $category->getVar('cat_id') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, 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

94
        $forumHandler->deleteAll(new \Criteria('cat_id', /** @scrutinizer ignore-type */ $category->getVar('cat_id')), true, true);
Loading history...
95
        $result = parent::delete($category);
96
        if ($result) {
97
            // Delete group permissions
98
            return $this->deletePermission($category);
99
        }
100
        $category->setErrors('delete category error');
101
102
        return false;
103
    }
104
105
    /**
106
     * Check permission for a category
107
     *
108
     * @param Category|int $category object or id
109
     * @param string       $perm     permission name
110
     *
111
     * @return bool
112
     */
113
    public function getPermission($category, $perm = 'access')
114
    {
115
        if ($GLOBALS['xoopsUserIsAdmin'] && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
116
            return true;
117
        }
118
119
        $cat_id = \is_object($category) ? $category->getVar('cat_id') : (int)$category;
120
        /** @var PermissionHandler $permHandler */
121
        $permHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Permission');
122
123
        return $permHandler->getPermission('category', $perm, $cat_id);
124
    }
125
126
    /**
127
     * @param Category $category
128
     * @return mixed
129
     */
130
    public function deletePermission(Category $category)
131
    {
132
        /** @var PermissionHandler $permHandler */
133
        $permHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Permission');
134
135
        return $permHandler->deleteByCategory($category->getVar('cat_id'));
136
    }
137
138
    /**
139
     * @param Category $category
140
     * @return mixed
141
     */
142
    public function applyPermissionTemplate(Category $category)
143
    {
144
        /** @var PermissionHandler $permHandler */
145
        $permHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Permission');
146
147
        return $permHandler->setCategoryPermission($category->getVar('cat_id'));
148
    }
149
150
    /**
151
     * @param mixed $object
152
     * @return bool
153
     */
154
    public function synchronization($object = null)
0 ignored issues
show
Unused Code introduced by
The parameter $object 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

154
    public function synchronization(/** @scrutinizer ignore-unused */ $object = null)

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...
155
    {
156
        return true;
157
    }
158
}
159