PermissionsHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A buildPermissionsYaml() 0 14 3
A getPermissions() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Util;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Yaml\Parser;
16
17
class PermissionsHandler implements PermissionsHandlerInterface
18
{
19
    /** @var string */
20
    protected $definitionsPath;
21
22
    /** @var array */
23
    protected $modules;
24
25
    /** @var Finder */
26
    protected $finder;
27
28
    /** @var Parser */
29
    protected $parser;
30
31
    /** @var array */
32
    protected $permissions;
33
34 5
    public function __construct($definitionsPath)
35
    {
36 5
        $this->definitionsPath = $definitionsPath;
37 5
        $this->finder = Finder::create()->files()->name('*.yaml')->in($this->definitionsPath);
38 5
        $this->parser = new Parser();
39
40 5
        $this->buildPermissionsYaml();
41 5
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 5
    public function getPermissions()
47
    {
48 5
        return $this->permissions;
49
    }
50
51
    /**
52
     * Builds the list of currently active modules with their permissions.
53
     */
54 5
    protected function buildPermissionsYaml()
55
    {
56 5
        $permissions = [];
57
58 5
        foreach ($this->finder as $definitionsFile) {
59 5
            $module = $this->parser->parseFile($definitionsFile->getPathname());
60 5
            $moduleKey = $definitionsFile->getBasename('.yaml');
61
62 5
            foreach ($module['permissions'] as $key => $permission) {
63 5
                $permissions[$key] = $permission + ['provider' => $moduleKey];
64
            }
65
        }
66
67 5
        $this->permissions = $permissions;
68 5
    }
69
}
70