Passed
Push — master ( 3e5b55...ab2589 )
by Andrii
03:20
created

PlantUML::isDeny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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 yii\rbac\PhpManager;
14
use yii\rbac\Item;
15
16
class PlantUML
17
{
18
    /**
19
     * @var PhpManager
20
     */
21
    protected $auth;
22
23
    public function __construct($auth)
24
    {
25
        $this->auth = $auth;
26
    }
27
28
    public function build()
29
    {
30
        $path = dirname(__DIR__, 2) . '/docs/test.md';
0 ignored issues
show
Unused Code introduced by
The assignment to $path is dead and can be removed.
Loading history...
31
32
        $header = "@startuml";
33
        $footer = "@enduml";
34
        $legend = '
35
legend right
36
        |<#FFDDDD>  <b>E</b>  |  <b>Role</b> |
37
        |<#DDFFDD>  <b>P</b>  |  <b>Permission</b>  |
38
endlegend
39
';
40
41
        $defs = '
42
!define Role(name,desc) component "desc" as name << (R,#EEEEEE) >> #FFDDDD
43
!define Permission(name,desc) () name as "desc" #DDFFDD
44
hide methods
45
hide stereotypes
46
';
47
48
        $items = [];
49
        $links = [];
50
        $hidds = [];
51
52
        $permissions = $this->auth->getPermissions();
53
        foreach ($permissions as $name => $perm) {
54
            if ($this->isDeny($name)) {
55
                continue;
56
            }
57
58
            $id = $this->toId($name);
59
            $items[] = "Permission($id, \"$name\")";
60
61
            if (!empty($old_id)) {
62
                $hidds[] = "$old_id -[hidden]-> $id";
63
            }
64
            $old_id = $id;
65
        }
66
67
        $items[] = '';
68
69
        foreach ($this->auth->getRoles() as $parent => $role) {
70
            $parent_id = $this->toId($parent);
71
72
            $items[] = "Role($parent_id, \"$parent\")";
73
            foreach ($this->auth->getChildren($parent) as $child => $item) {
74
                $child_id = $this->toId($child);
75
                $links[] = "$child_id -> $parent_id";
76
            }
77
        }
78
79
        $items = $this->joinClientOnly($items);
80
        $links = $this->joinClientOnly($links);
81
        $hidds = $this->joinClientOnly($hidds);
82
        $res = implode("\n", [$header, $legend, $defs, $items, '', $links, '', $hidds, '', $footer, '']);
83
84
        return $res;
85
    }
86
87
    private function isDeny(string $name): bool
88
    {
89
        return strncmp($name, 'deny', 4) === 0;
90
    }
91
92
    private function toId(string $name): string
93
    {
94
        return strtr($name, ':.-', '___');
95
    }
96
97
    private function joinClientOnly($arr)
98
    {
99
        $res = [];
100
        foreach ($arr as $str) {
101
            if (strpos($str, 'client') !== false) {
102
                $res[] = $str;
103
            }
104
        }
105
106
        return implode("\n", $res);
107
    }
108
}
109