PermissionCategoryHandler::getValidItems()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2.0 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
 */
13
14
use XoopsModules\Newbb\{
15
    Category
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Newbb\Category. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
};
17
18
//defined("NEWBB_HANDLER_PERMISSION") || require_once __DIR__  .'/permission.php';
19
//define("NEWBB_HANDLER_PERMISSION_CATEGORY", 1);
20
21
/**
22
 * Class PermissionCategoryHandler
23
 */
24
class PermissionCategoryHandler extends PermissionHandler
25
{
26
    /**
27
     * @param \XoopsDatabase|null $db
28
     */
29
    public function __construct(\XoopsDatabase $db = null)
30
    {
31
        //        $this->PermissionHandler($db);
32
        parent::__construct($db);
33
    }
34
35
    /**
36
     * @param int $mid
37
     * @param int $id
38
     * @return array
39
     */
40
    public function getValidItems(int $mid, int $id = 0): array
0 ignored issues
show
Unused Code introduced by
The parameter $id 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

40
    public function getValidItems(int $mid, /** @scrutinizer ignore-unused */ int $id = 0): array

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...
41
    {
42
        $full_items = [];
43
        if (empty($mid)) {
44
            return $full_items;
45
        }
46
47
        $full_items[] = "'category_access'";
48
49
        return $full_items;
50
    }
51
52
    /**
53
     * @param int $cat_id
54
     * @return bool
55
     */
56
    public function deleteByCategory(int $cat_id): bool
57
    {
58
        $cat_id = (int)$cat_id;
59
        if (empty($cat_id)) {
60
            return false;
61
        }
62
        /** @var \XoopsGroupPermHandler $grouppermHandler */
63
        $grouppermHandler = \xoops_getHandler('groupperm');
64
        $criteria         = new \CriteriaCompo(new \Criteria('gperm_modid', $GLOBALS['xoopsModule']->getVar('mid')));
65
        $criteria->add(new \Criteria('gperm_name', 'category_access'));
66
        $criteria->add(new \Criteria('gperm_itemid', $cat_id));
67
68
        return $grouppermHandler->deleteAll($criteria);
69
    }
70
71
    /**
72
     * @param int $category
73
     * @param array  $groups
74
     * @return bool
75
     */
76
    public function setCategoryPermission(int $category, array $groups = []): bool
77
    {
78
        if (\is_object($GLOBALS['xoopsModule']) && 'newbb' === $GLOBALS['xoopsModule']->getVar('dirname')) {
79
            $mid = $GLOBALS['xoopsModule']->getVar('mid');
80
        } else {
81
            /** @var \XoopsModuleHandler $moduleHandler */
82
            $moduleHandler = \xoops_getHandler('module');
83
            $newbb         = $moduleHandler->getByDirname('newbb');
84
            $mid           = $newbb->getVar('mid');
85
        }
86
        if (empty($groups)) {
87
            /** @var \XoopsMemberHandler $memberHandler */
88
            $memberHandler = \xoops_getHandler('member');
89
            $glist         = $memberHandler->getGroupList();
90
            $groups        = \array_keys($glist);
91
        }
92
        $ids     = $this->getGroupIds('category_access', $category, $mid);
93
        $ids_add = \array_diff($groups, $ids);
94
        $ids_rmv = \array_diff($ids, $groups);
95
        foreach ($ids_add as $group) {
96
            $this->addRight('category_access', $category, $group, $mid);
97
        }
98
        foreach ($ids_rmv as $group) {
99
            $this->deleteRight('category_access', $category, $group, $mid);
100
        }
101
102
        return true;
103
    }
104
}
105