MetadataGenerator::dump()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 7
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-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