Passed
Push — master ( e3ab8d...47c9b1 )
by Andrii
02:20
created

RbacController::getApp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * RBAC implementation for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-rbac
6
 * @package   hipanel-rbac
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac\console;
12
13
use hipanel\rbac\RbacIniterInterface;
14
use yii\base\Module;
15
16
/**
17
 * Class RbacController.
18
 *
19
 * Usage: `hidev rbac/init`
20
 */
21
class RbacController extends \yii\console\Controller
22
{
23
    /**
24
     * @var RbacIniterInterface
25
     */
26
    protected $initer;
27
28
    public function __construct($id, Module $module, RbacIniterInterface $initer, $config = [])
29
    {
30
        parent::__construct($id, $module, $config);
31
32
        $this->initer = $initer;
33
    }
34
35
    public $defaultAction = 'show';
36
37
    public function actionInit()
38
    {
39
        $auth = $this->getApp()->get('authManager');
40
        $this->initer->init($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\RbacIniterInterface::init() does only seem to accept hipanel\rbac\AuthManager, 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

40
        $this->initer->init(/** @scrutinizer ignore-type */ $auth);
Loading history...
41
    }
42
43
    public function actionReinit()
44
    {
45
        $auth = $this->getApp()->get('authManager');
46
        $this->initer->reinit($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\RbacIniterInterface::reinit() does only seem to accept hipanel\rbac\AuthManager, 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

46
        $this->initer->reinit(/** @scrutinizer ignore-type */ $auth);
Loading history...
47
    }
48
49
    public function actionShow()
50
    {
51
        $auth = $this->getApp()->get('authManager');
52
53
        echo "Permissions:\n";
54
        $permissions = $auth->getPermissions();
55
        ksort($permissions);
56
        foreach ($permissions as $name => $perm) {
57
            echo "   $perm->name $perm->description\n";
58
        }
59
60
        echo "Roles:\n";
61
        foreach ($auth->getRoles() as $name => $role) {
62
            $children = implode(',', array_keys($auth->getChildren($name)));
63
            printf("   %-12s %s\n", "$role->name:", $children);
64
        }
65
66
        echo "Assignments:\n";
67
        foreach ($auth->getAllAssignments() as $userId => $roles) {
68
            $roles = implode(',', array_keys($roles));
69
            printf("   %-12s %s\n", "$userId:", $roles);
70
        }
71
    }
72
73
    protected function getApp()
74
    {
75
        return class_exists('Yii') ? \Yii::$app : \yii\helpers\Yii::getApp();
0 ignored issues
show
Bug introduced by
The type yii\helpers\Yii was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
    }
77
}
78