Completed
Push — master ( 51cbcc...2d333a )
by Andrii
07:19
created

AuthManager::setItem()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 3
eloc 6
nc 4
nop 3
crap 3.2098
1
<?php
2
3
/*
4
 * RBAC implementation for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-rbac
7
 * @package   hipanel-rbac
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\rbac;
13
14
use yii\base\InvalidParamException;
15
use yii\rbac\Item;
16
17
/**
18
 * AuthManager class.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class AuthManager extends \yii\rbac\PhpManager
23
{
24
    /**
25
     * Set permission.
26
     * @param string $name
27
     * @param string $description
28
     * @return Item
29
     */
30 1
    public function setPermission($name, $description = null)
31
    {
32 1
        return $this->setItem('permission', $name, $description);
33
    }
34
35
    /**
36
     * Set role.
37
     * @param string $name
38
     * @param string $description
39
     * @return Item
40
     */
41 1
    public function setRole($name, $description = null)
42
    {
43 1
        return $this->setItem('role', $name, $description);
44
    }
45
46
    /**
47
     * Set item by type and name.
48
     * Created if not exists else updates.
49
     * @param string $type
50
     * @param string $name
51
     * @param string $description
52
     * @return Item
53
     */
54 1
    public function setItem($type, $name, $description = null)
55
    {
56 1
        $item = $this->getItem($name) ?: $this->createItem($type, $name);
57 1
        if ($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
            $item->description = $description;
59
        }
60 1
        $this->add($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<yii\rbac\Item>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
62 1
        return $item;
63
    }
64
65
    /**
66
     * Create item by type and name.
67
     * @param string $type
68
     * @param string $name
69
     * @throws InvalidParamException
70
     * @return Item
71
     */
72 1
    public function createItem($type, $name)
73
    {
74 1
        if ('role' === $type) {
75 1
            return $this->createRole($name);
76 1
        } elseif ('permission' === $type) {
77 1
            return $this->createPermission($name);
78
        } else {
79
            throw new InvalidParamException('Creating unsupported item type: ' . $type);
80
        }
81
    }
82
83
    /**
84
     * Set child.
85
     * @param string|Item $parent
86
     * @param string|Item $child
87
     * @return bool
88
     */
89 1
    public function setChild($parent, $child)
90
    {
91 1 View Code Duplication
        if (is_string($parent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92 1
            $name   = $parent;
93 1
            $parent = $this->getItem($parent);
94 1
            if (is_null($parent)) {
95
                throw new InvalidParamException("Unknown parent:$name at setChild");
96
            }
97 1
        }
98 1 View Code Duplication
        if (is_string($child)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99 1
            $name  = $child;
100 1
            $child = $this->getItem($child);
101 1
            if (is_null($child)) {
102
                throw new InvalidParamException("Unknown child:$name at setChild");
103
            }
104 1
        }
105 1
        if (isset($this->children[$parent->name][$child->name])) {
106
            return false;
107 1
        }
108 1
        return $this->addChild($parent, $child);
109
    }
110
111 1
    public function setAssignment($role, $userId)
112
    {
113 1 View Code Duplication
        if (is_string($role)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 1
            $name = $role;
115 1
            $role = $this->getRole($role);
116 1
            if (is_null($role)) {
117
                throw new InvalidParamException("Unknown role:$name at setAssignment");
118
            }
119 1
        }
120 1
        if (isset($this->assignments[$userId][$role->name])) {
121
            return false;
122
        }
123 1
        return $this->assign($role, $userId);
124
    }
125
}
126