Passed
Push — master ( d37ad5...6a9ed7 )
by De Cramer
10:00 queued 07:52
created

VisibleConfigsResolver   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 1
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAllowedConfigs() 0 8 2
A getAllowedConfigsGenerator() 0 14 5
A getAllAllowedConfigs() 0 18 4
1
<?php
2
3
namespace oliverde8\ComfyBundle\Resolver;
4
5
use oliverde8\AssociativeArraySimplified\AssociativeArray;
6
use oliverde8\ComfyBundle\Manager\ConfigManagerInterface;
7
use oliverde8\ComfyBundle\Model\ConfigInterface;
8
use oliverde8\ComfyBundle\Security\ConfigVoter;
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...izationCheckerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class VisibleConfigsResolver
12
{
13
    protected AuthorizationCheckerInterface $checker;
14
15
    protected ConfigManagerInterface $configManager;
16
17
    /**
18
     * @param AuthorizationCheckerInterface $checker
19
     * @param ConfigManagerInterface $configManager
20
     */
21
    public function __construct(AuthorizationCheckerInterface $checker, ConfigManagerInterface $configManager)
22
    {
23
        $this->checker = $checker;
24
        $this->configManager = $configManager;
25
    }
26
27
    public function getAllowedConfigs(string $configPath, string $action = ConfigVoter::ACTION_VIEW): array
28
    {
29
        $allowedConfigs = [];
30
        foreach ($this->getAllowedConfigsGenerator($configPath, $action) as $config) {
31
            $allowedConfigs[] = $config;
32
        }
33
34
        return $allowedConfigs;
35
    }
36
37
    public function getAllAllowedConfigs(string $action = ConfigVoter::ACTION_VIEW): array
38
    {
39
        $allowedConfigs = new AssociativeArray();
40
41
        $allConfigs = $this->configManager->getAllConfigs();
42
        array_walk_recursive($allConfigs, function ($config, $key) use($allowedConfigs, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

42
        array_walk_recursive($allConfigs, function ($config, /** @scrutinizer ignore-unused */ $key) use($allowedConfigs, $action) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
            if (is_object($config)) {
44
                if ($config->isHidden()) {
45
                    return;
46
                }
47
48
                if ($this->checker->isGranted($action, $config)) {
49
                    $allowedConfigs->set($config->getPath(), $config);
50
                }
51
            }
52
        });
53
54
        return $allowedConfigs->getArray();
55
    }
56
57
    /**
58
     * @return \Generator|ConfigInterface[]
59
     * @throws \oliverde8\ComfyBundle\Exception\UnknownScopeException
60
     */
61
    protected function getAllowedConfigsGenerator(string $configPath, string $action)
62
    {
63
        $configs = $this->configManager->getAllConfigs()->get($configPath, []);
0 ignored issues
show
Unused Code introduced by
The assignment to $configs is dead and can be removed.
Loading history...
64
        if (empty($configPath)) {
65
            $this->configManager->getAllConfigs();
66
        }
67
68
        foreach ($this->configManager->getAllConfigs()->get($configPath, []) as $config) {
69
            if ($config->isHidden()) {
70
                continue;
71
            }
72
73
            if ($this->checker->isGranted($action, $config)) {
74
               yield $config;
75
            }
76
        }
77
    }
78
}
79