Test Failed
Push — master ( c4c2e8...dd2174 )
by Elijah
03:37
created

PermissionsHandler::buildPermissionsYaml()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 0
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
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
    public function __construct($definitionsPath)
32
    {
33
        $this->definitionsPath = $definitionsPath;
34
        $this->finder = Finder::create()->files()->name('*.yaml')->in($this->definitionsPath);
35
        $this->parser = new Parser();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getPermissions()
42
    {
43
        return $this->buildPermissionsYaml();
44
    }
45
46
    /**
47
     * Returns the list of currently active modules with their permissions.
48
     *
49
     * @return array
50
     */
51
    protected function buildPermissionsYaml()
52
    {
53
        $permissions = [];
54
55
        foreach ($this->finder as $definitionsFile) {
56
            $module = $this->parser->parseFile($definitionsFile->getPathname());
57
            $moduleKey = $definitionsFile->getBasename('.yaml');
58
            $module['title'] = !empty($module['title']) ? $module['title'] : $moduleKey;
59
60
            foreach ($module['permissions'] as $key => $permission) {
61
                $permissions[$key] = [
62
                    'title' => $permission['title'],
63
                    'provider' => $moduleKey,
64
                ];
65
            }
66
        }
67
68
        return $permissions;
69
    }
70
}
71