Completed
Pull Request — master (#12)
by Saulius
06:17 queued 03:28
created

loadHelpersConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of the sauls/components-bundle package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Bundle\Components\DependencyInjection;
14
15
use Sauls\Component\Widget\View\ViewInterface;
16
use Sauls\Component\Widget\WidgetInterface;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\Config\Loader\LoaderInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Extension\Extension;
21
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22
23
use function Sauls\Component\Helper\array_get_value;
24
25
class SaulsComponentsExtension extends Extension
26
{
27
    /**
28
     * @throws \Exception
29
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
30
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
31
     */
32 5
    public function load(array $configs, ContainerBuilder $container)
33
    {
34 5
        $configuration = $this->getConfiguration($configs, $container);
35 5
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of Symfony\Component\Depend...:processConfiguration() does only seem to accept Symfony\Component\Config...\ConfigurationInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        $config = $this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $configs);
Loading history...
36
37 5
        $loader = new YamlFileLoader($container, new FileLocator(\dirname(__DIR__) . '/Resources/config'));
38
39 5
        $this->loadHelpersConfiguration($config, $container, $loader);
40 5
        $this->loadWidgetsConfiguration($config, $container, $loader);
41 5
        $this->loadComponentsConfiguration($config, $container, $loader);
42 5
        $this->loadBuiltInWidgets($config, $container, $loader);
43 5
    }
44
45
    /**
46
     * @throws \Exception
47
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
48
     */
49 5
    private function loadHelpersConfiguration(array $configs, ContainerBuilder $container, LoaderInterface $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container 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

49
    private function loadHelpersConfiguration(array $configs, /** @scrutinizer ignore-unused */ ContainerBuilder $container, LoaderInterface $loader)

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...
50
    {
51 5
        if ($this->componentIsNotEnabled('helpers', $configs)) {
52 1
            return;
53
        }
54
55 4
        $loader->load('helpers.yaml');
56 4
    }
57
58
    /**
59
     * @throws \Exception
60
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
61
     */
62 5
    private function componentIsNotEnabled(string $componentName, array $configs): bool
63
    {
64 5
        return false === array_get_value($configs, $componentName, false);
65
    }
66
67
    /**
68
     * @throws \Exception
69
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
70
     */
71 5
    private function loadWidgetsConfiguration(array $configs, ContainerBuilder $container, LoaderInterface $loader)
72
    {
73 5
        if ($this->componentIsNotEnabled('widgets', $configs)) {
74 1
            return;
75
        }
76
77 4
        $loader->load('widgets.yaml');
78
79
        $container
80 4
            ->registerForAutoconfiguration(WidgetInterface::class)
81 4
            ->addTag('sauls_widget.widget')
82 4
            ->setPublic(true);
83
84
        $container
85 4
            ->registerForAutoconfiguration(ViewInterface::class)
86 4
            ->addTag('sauls_widget.view')
87 4
            ->setPublic(true);
88 4
    }
89
90 5
    private function loadComponentsConfiguration(array $configs, ContainerBuilder $container, LoaderInterface $loader)
91
    {
92 5
        $this->loadSecurityAccessComponent($configs, $container, $loader);
93 5
    }
94
95 5
    private function loadSecurityAccessComponent(array $configs, ContainerBuilder $container, LoaderInterface $loader)
96
    {
97 5
        if ($this->componentIsNotEnabled('components.access', $configs)) {
98 4
            return;
99
        }
100
101 1
        $loader->load('component/security/access.yaml');
102
103 1
        $container->setParameter(
104 1
            'sauls_components.component.access.options',
105 1
            array_get_value($configs, 'components.access')
106
        );
107 1
    }
108
109 5
    private function loadBuiltInWidgets(array $config, ContainerBuilder $container, YamlFileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $config 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

109
    private function loadBuiltInWidgets(/** @scrutinizer ignore-unused */ array $config, ContainerBuilder $container, YamlFileLoader $loader)

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...
110
    {
111 5
        if ($container->has('cache.app')) {
112
            $loader->load('builtin_widgets.yaml');
113
        }
114 5
    }
115
}
116