Passed
Pull Request — master (#12)
by Saulius
06:00
created

SaulsComponentsExtension::loadBuiltInWidgets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
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 Exception;
16
use InvalidArgumentException;
17
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;
18
use Sauls\Component\Widget\View\ViewInterface;
19
use Sauls\Component\Widget\WidgetInterface;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\Config\Loader\LoaderInterface;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Extension\Extension;
24
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
25
26
use function dirname;
27
use function Sauls\Component\Helper\array_get_value;
28
29
class SaulsComponentsExtension extends Extension
30
{
31 5
    /**
32
     * @throws Exception
33
     * @throws PropertyNotAccessibleException
34 5
     * @throws InvalidArgumentException When provided tag is not defined in this extension
35 5
     */
36
    public function load(array $configs, ContainerBuilder $container)
37 5
    {
38
        $configuration = $this->getConfiguration($configs, $container);
39 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

39
        $config = $this->processConfiguration(/** @scrutinizer ignore-type */ $configuration, $configs);
Loading history...
40 5
41 5
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config'));
42 5
43
        $this->loadHelpersConfiguration($config, $container, $loader);
44
        $this->loadWidgetsConfiguration($config, $container, $loader);
45
        $this->loadComponentsConfiguration($config, $container, $loader);
46
        $this->loadBuiltInWidgets($config, $container, $loader);
47
    }
48 5
49
    /**
50 5
     * @throws Exception
51 1
     * @throws PropertyNotAccessibleException
52
     */
53
    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

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

116
    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...
117
    {
118
        if ($container->has('cache.app')) {
119
            $loader->load('builtin_widgets.yaml');
120
        }
121
    }
122
}
123