PlantUML   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 91
ccs 0
cts 43
cp 0
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 57 6
A toId() 0 3 1
A __construct() 0 3 1
A isDeny() 0 3 1
A joinClientOnly() 0 10 3
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-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac\console;
12
13
use yii\rbac\PhpManager;
14
15
class PlantUML
16
{
17
    /**
18
     * @var PhpManager
19
     */
20
    protected $auth;
21
22
    public function __construct($auth)
23
    {
24
        $this->auth = $auth;
25
    }
26
27
    public function build()
28
    {
29
        $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...
30
31
        $header = '@startuml';
32
        $footer = '@enduml';
33
        $legend = '
34
legend right
35
        |<#FFDDDD>  <b>E</b>  |  <b>Role</b> |
36
        |<#DDFFDD>  <b>P</b>  |  <b>Permission</b>  |
37
endlegend
38
';
39
40
        $defs = '
41
!define Role(name,desc) component "desc" as name << (R,#EEEEEE) >> #FFDDDD
42
!define Permission(name,desc) () name as "desc" #DDFFDD
43
hide methods
44
hide stereotypes
45
';
46
47
        $items = [];
48
        $links = [];
49
        $hidds = [];
50
51
        $permissions = $this->auth->getPermissions();
52
        foreach ($permissions as $name => $perm) {
53
            if ($this->isDeny($name)) {
54
                continue;
55
            }
56
57
            $id = $this->toId($name);
58
            $items[] = "Permission($id, \"$name\")";
59
60
            if (!empty($old_id)) {
61
                $hidds[] = "$old_id -[hidden]-> $id";
62
            }
63
            $old_id = $id;
64
        }
65
66
        $items[] = '';
67
68
        foreach ($this->auth->getRoles() as $parent => $role) {
69
            $parent_id = $this->toId($parent);
70
71
            $items[] = "Role($parent_id, \"$parent\")";
72
            foreach ($this->auth->getChildren($parent) as $child => $item) {
73
                $child_id = $this->toId($child);
74
                $links[] = "$child_id -> $parent_id";
75
            }
76
        }
77
78
        $items = $this->joinClientOnly($items);
79
        $links = $this->joinClientOnly($links);
80
        $hidds = $this->joinClientOnly($hidds);
81
        $res = implode("\n", [$header, $legend, $defs, $items, '', $links, '', $hidds, '', $footer, '']);
82
83
        return $res;
84
    }
85
86
    private function isDeny(string $name): bool
87
    {
88
        return strncmp($name, 'deny', 4) === 0;
89
    }
90
91
    private function toId(string $name): string
92
    {
93
        return strtr($name, ':.-', '___');
94
    }
95
96
    private function joinClientOnly($arr)
97
    {
98
        $res = [];
99
        foreach ($arr as $str) {
100
            if (strpos($str, 'client') !== false) {
101
                $res[] = $str;
102
            }
103
        }
104
105
        return implode("\n", $res);
106
    }
107
}
108