CategoriesHandler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 14
eloc 44
c 5
b 0
f 0
dl 0
loc 105
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubCats() 0 26 5
B getCategoriesCount() 0 25 7
A create() 0 6 1
A __construct() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Adslight;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * Module: Adslight
17
 *
18
 * @category        Module
19
 * @author          XOOPS Development Team <https://xoops.org>
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 */
23
24
/**
25
 * Class CategoriesHandler
26
 */
27
class CategoriesHandler extends \XoopsPersistableObjectHandler
28
{
29
    private const TABLE      = 'adslight_categories';
30
    private const ENTITY     = Categories::class;
31
    private const ENTITYNAME = 'Categories';
32
    private const KEYNAME    = 'cid';
33
    private const IDENTIFIER = 'title';
34
    /**
35
     * @var Helper
36
     */
37
    public $helper;
38
39
    /**
40
     * Constructor
41
     * @param null|\XoopsDatabase                $db
42
     * @param null|Helper $helper
43
     */
44
    public function __construct(\XoopsDatabase $db = null, $helper = null)
45
    {
46
        /** @var Helper $this- >helper */
47
        $this->helper = $helper;
48
        $this->db     = $db;
49
        parent::__construct($db, static::TABLE, static::ENTITY, static::KEYNAME, static::IDENTIFIER);
50
    }
51
52
    /**
53
     * @param bool $isNew
54
     *
55
     * @return \XoopsObject
56
     */
57
    public function create($isNew = true)
58
    {
59
        $obj         = parent::create($isNew);
60
        $obj->helper = $this->helper;
61
62
        return $obj;
63
    }
64
65
    //====================================
66
67
    /**
68
     * @param int $pid
69
     *
70
     * @return int
71
     */
72
    public function getCategoriesCount($pid = 0): int
73
    {
74
        if (-1 == $pid) {
75
            return $this->getCount();
76
        }
77
        $helper = Helper::getInstance();
78
        /** @var \Xmf\Module\Helper\Permission $permHelper */
79
        $permHelper = $this->helper->getHandler('Permission');
80
        $criteria   = new \CriteriaCompo();
81
        if (isset($pid) && (-1 != $pid)) {
82
            $criteria->add(new \Criteria('pid', $pid));
83
            if (!$helper->isUserAdmin()) {
84
                $categoriesGranted = $permHelper->getGrantedItems('category_read');
0 ignored issues
show
Bug introduced by
The method getGrantedItems() does not exist on Xmf\Module\Helper\Permission. ( Ignorable by Annotation )

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

84
                /** @scrutinizer ignore-call */ 
85
                $categoriesGranted = $permHelper->getGrantedItems('category_read');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
                if (\is_array($categoriesGranted) && \count($categoriesGranted) > 0) {
86
                    $criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
87
                } else {
88
                    return 0;
89
                }
90
                //                if (\is_object($GLOBALS['xoopsUser'])) {
91
                //                    $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
92
                //                }
93
            }
94
        }
95
96
        return $this->getCount($criteria);
97
    }
98
99
    /**
100
     * Get all subcats and put them in an array indexed by parent id
101
     *
102
     * @param array $categories
103
     *
104
     * @return array
105
     */
106
    public function getSubCats($categories): array
107
    {
108
        $helper   = Helper::getInstance();
109
        $criteria = new \CriteriaCompo(new \Criteria('pid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN'));
110
        $ret      = [];
111
        if (!$helper->isUserAdmin()) {
112
            $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
113
            if (\count($categoriesGranted) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $categoriesGranted can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, 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

113
            if (\count(/** @scrutinizer ignore-type */ $categoriesGranted) > 0) {
Loading history...
114
                $criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
0 ignored issues
show
Bug introduced by
It seems like $categoriesGranted can also be of type null; however, parameter $pieces of implode() does only seem to accept array, 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

114
                $criteria->add(new \Criteria('cid', '(' . \implode(',', /** @scrutinizer ignore-type */ $categoriesGranted) . ')', 'IN'));
Loading history...
115
            } else {
116
                return $ret;
117
            }
118
119
            if (\is_object($GLOBALS['xoopsUser'])) {
120
                $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
121
            }
122
        }
123
        $criteria->setSort('weight');
124
        $criteria->order = 'ASC'; // patch for XOOPS <= 2.5.10, does not set order correctly using setOrder() method
125
        $subcats         = $this->getObjects($criteria, true);
126
        /** @var Categories $subcat */
127
        foreach ($subcats as $subcat) {
128
            $ret[$subcat->getVar('pid')][$subcat->getVar('cid')] = $subcat;
129
        }
130
131
        return $ret;
132
    }
133
}
134