Passed
Push — master ( 0c5017...05ff7d )
by Michael
03:08
created

CategoriesHandler::getCategoriesCount()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 24
rs 8.8333
cc 7
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * Module: Adslight
19
 *
20
 * @category        Module
21
 * @author          XOOPS Development Team <https://xoops.org>
22
 * @copyright       {@link https://xoops.org/ XOOPS Project}
23
 * @license         GPL 2.0 or later
24
 */
25
26
27
28
/**
29
 * Class CategoriesHandler
30
 */
31
class CategoriesHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * @var Helper
35
     */
36
    public $helper;
37
38
    /**
39
     * Constructor
40
     * @param null|\XoopsDatabase                $db
41
     * @param null|Helper $helper
42
     */
43
44
    public function __construct(\XoopsDatabase $db = null, $helper = null)
45
    {
46
        /** @var Helper $this- >helper */
47
        $this->helper = $helper;
48
        parent::__construct($db, 'adslight_categories', Categories::class, 'cid', 'title');
49
    }
50
51
    /**
52
     * @param bool $isNew
53
     *
54
     * @return \XoopsObject
55
     */
56
    public function create($isNew = true): \XoopsObject
57
    {
58
        $obj         = parent::create($isNew);
59
        $obj->helper = $this->helper;
60
61
        return $obj;
62
    }
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
        $permHelper = $this->helper->getHandler('Permission');
79
        $criteria = new \CriteriaCompo();
80
        if (isset($pid) && (-1 != $pid)) {
81
            $criteria->add(new \Criteria('pid', $pid));
82
            if (!$helper->isUserAdmin()) {
83
                $categoriesGranted = $permHelper->getGrantedItems('category_read');
0 ignored issues
show
Bug introduced by
The method getGrantedItems() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

83
                /** @scrutinizer ignore-call */ 
84
                $categoriesGranted = $permHelper->getGrantedItems('category_read');
Loading history...
84
                if (is_array($categoriesGranted) && \count($categoriesGranted) > 0) {
85
                    $criteria->add(new \Criteria('cid', '(' . \implode(',', $categoriesGranted) . ')', 'IN'));
86
                } else {
87
                    return 0;
88
                }
89
                //                if (\is_object($GLOBALS['xoopsUser'])) {
90
                //                    $criteria->add(new \Criteria('moderator', $GLOBALS['xoopsUser']->getVar('uid')), 'OR');
91
                //                }
92
            }
93
        }
94
95
        return $this->getCount($criteria);
96
    }
97
98
    /**
99
     * Get all subcats and put them in an array indexed by parent id
100
     *
101
     * @param array $categories
102
     *
103
     * @return array
104
     */
105
    public function getSubCats($categories): array
106
    {
107
        $helper   = Helper::getInstance();
108
        $criteria = new \CriteriaCompo(new \Criteria('pid', '(' . \implode(',', \array_keys($categories)) . ')', 'IN'));
109
        $ret      = [];
110
        if (!$helper->isUserAdmin()) {
111
            $categoriesGranted = $this->helper->getHandler('Permission')->getGrantedItems('category_read');
112
            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

112
            if (\count(/** @scrutinizer ignore-type */ $categoriesGranted) > 0) {
Loading history...
113
                $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

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