MetadataGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 52
ccs 0
cts 27
cp 0
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRoleDescriptions() 0 9 3
A generatePermissionsDescriptions() 0 11 3
A __construct() 0 4 1
A dump() 0 10 1
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 hipanel\rbac\AuthManager;
14
use hipanel\rbac\RbacIniterInterface;
15
use Laminas\Code\Generator\FileGenerator;
16
use Laminas\Code\Generator\ValueGenerator;
17
18
final class MetadataGenerator
19
{
20
    /**
21
     * @var AuthManager
22
     */
23
    private $auth;
24
    /**
25
     * @var RbacIniterInterface
26
     */
27
    private $initer;
28
29
    public function __construct(AuthManager $authManager, RbacIniterInterface $initer)
30
    {
31
        $this->auth = $authManager;
32
        $this->initer = $initer;
33
    }
34
35
    public function dump(string $path): void
36
    {
37
        $metadata = $this->initer->getMetadata();
0 ignored issues
show
Bug introduced by
The method getMetadata() does not exist on hipanel\rbac\RbacIniterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hipanel\rbac\RbacIniterInterface. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-call */ 
38
        $metadata = $this->initer->getMetadata();
Loading history...
38
        $this->generateRoleDescriptions($metadata);
39
        $this->generatePermissionsDescriptions($metadata);
40
41
        $generator = FileGenerator::fromArray([
42
            'body' => 'return ' . (new ValueGenerator($metadata, ValueGenerator::TYPE_ARRAY_SHORT))->generate() . ';',
43
        ]);
44
        file_put_contents($path, $generator->generate(), LOCK_EX);
45
    }
46
47
    private function generateRoleDescriptions(array &$metadata): void
48
    {
49
        $descriptor = new RoleDescriptionGenerator();
50
        foreach ($this->auth->getRoles() as $name => $role) {
51
            if (isset($metadata[$name])) {
52
                continue;
53
            }
54
55
            $metadata[$name] = ['description' => $descriptor($name)];
56
        }
57
    }
58
59
    private function generatePermissionsDescriptions(array &$metadata): void
60
    {
61
        $descriptor = new PermissionDescriptionGenerator();
62
        $permissions = $this->auth->getPermissions();
63
        ksort($permissions);
64
        foreach ($permissions as $name => $permission) {
65
            if (isset($metadata[$name])) {
66
                continue;
67
            }
68
69
            $metadata[$name] = ['description' => $descriptor($name)];
70
        }
71
    }
72
}
73