Perm::getHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace XoopsModules\Extcal;
4
5
/**
6
 * Class Perm.
7
 */
8
class Perm
9
{
10
    /**
11
     * @return Perm
12
     */
13
    public static function getHandler()
14
    {
15
        static $permHandler;
16
        if (!isset($permHandler)) {
17
            $permHandler = new self();
18
        }
19
20
        return $permHandler;
21
    }
22
23
    /**
24
     * @param $user
25
     *
26
     * @return string
27
     */
28
    public function getUserGroup($user)
29
    {
30
        if ($user instanceof \XoopsUser) {
31
            return $user->getGroups();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user->getGroups() returns the type array which is incompatible with the documented return type string.
Loading history...
32
        }
33
34
        return XOOPS_GROUP_ANONYMOUS;
35
    }
36
37
    /**
38
     * @param           $user
39
     * @param           $perm
40
     *
41
     * @return bool
42
     */
43
    public function getAuthorizedCat($user, $perm)
44
    {
45
        static $authorizedCat;
46
        $userId = $user ? $user->getVar('uid') : 0;
47
        if (!isset($authorizedCat[$perm][$userId])) {
48
            /** @var \XoopsGroupPermHandler $grouppermHandler */
49
            $groupPermHandler = \xoops_getHandler('groupperm');
50
            /** @var \XoopsModuleHandler $moduleHandler */
51
            $moduleHandler = \xoops_getHandler('module');
52
            $module        = $moduleHandler->getByDirname('extcal');
53
            if (!$module) {
54
                return false;
55
            }
56
            $authorizedCat[$perm][$userId] = $groupPermHandler->getItemIds($perm, $this->getUserGroup($user), $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getItemIds() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $authorizedCat[$perm][$userId] = $groupPermHandler->getItemIds($perm, $this->getUserGroup($user), $module->getVar('mid'));
Loading history...
57
        }
58
59
        return $authorizedCat[$perm][$userId];
60
    }
61
62
    /**
63
     * @param $user
64
     * @param $perm
65
     * @param $catId
66
     *
67
     * @return bool
68
     */
69
    public function isAllowed($user, $perm, $catId)
70
    {
71
        $autorizedCat = $this->getAuthorizedCat($user, $perm);
72
73
        return \in_array($catId, $autorizedCat);
0 ignored issues
show
Bug introduced by
$autorizedCat of type boolean is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

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

73
        return \in_array($catId, /** @scrutinizer ignore-type */ $autorizedCat);
Loading history...
74
    }
75
}
76