Passed
Push — master ( 6af1fb...f5fe55 )
by Michael
02:53 queued 10s
created

CategoriesHandler::getCategoriesCount()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 9.2222
cc 6
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
use Xmf\Module\Helper\Permission;
27
use XoopsModules\Adslight;
28
29
$moduleDirName = \basename(\dirname(__DIR__));
30
31
$permHelper = new Permission();
32
33
/**
34
 * Class CategoriesHandler
35
 */
36
class CategoriesHandler extends \XoopsPersistableObjectHandler
37
{
38
    /**
39
     * @var Helper
40
     */
41
    public $helper;
42
43
    /**
44
     * Constructor
45
     * @param null|\XoopsDatabase                $db
46
     * @param null|\XoopsModules\Adslight\Helper $helper
47
     */
48
49
    public function __construct(\XoopsDatabase $db = null, $helper = null)
50
    {
51
        /** @var \XoopsModules\Adslight\Helper $this- >helper */
52
        $this->helper = $helper;
53
        parent::__construct($db, 'adslight_categories', Categories::class, 'cid', 'title');
54
    }
55
56
    /**
57
     * @param bool $isNew
58
     *
59
     * @return \XoopsObject
60
     */
61
    public function create($isNew = true): \XoopsObject
62
    {
63
        $obj         = parent::create($isNew);
64
        $obj->helper = $this->helper;
65
66
        return $obj;
67
    }
68
    
69
    
70
    //====================================
71
72
    /**
73
     * @param int $pid
74
     *
75
     * @return int
76
     */
77
    public function getCategoriesCount($pid = 0)
78
    {
79
        if (-1 == $pid) {
80
            return $this->getCount();
81
        }
82
        $helper = Helper::getInstance();
83
        $criteria = new \CriteriaCompo();
84
        if (isset($pid) && (-1 != $pid)) {
85
            $criteria->add(new \Criteria('pid', $pid));
86
            if (!$helper->isUserAdmin()) {
87
                $categoriesGranted = $this->helper->getHandler('Permission')->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

87
                $categoriesGranted = $this->helper->getHandler('Permission')->/** @scrutinizer ignore-call */ getGrantedItems('category_read');
Loading history...
88
                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

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

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

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

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