Passed
Push — main ( 274d1b...8a24c2 )
by Oscar
03:25
created

OcubomTwigExtraExtension::loadSvg()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 65
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 6.0005

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 36
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 65
ccs 39
cts 40
cp 0.975
crap 6.0005
rs 8.7217

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of ocubom/twig-extra-bundle
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
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 Ocubom\TwigExtraBundle\DependencyInjection;
13
14
use Ocubom\Twig\Extension\HtmlAttributesRuntime;
15
use Ocubom\Twig\Extension\HtmlCompressRuntime;
16
use Ocubom\Twig\Extension\HtmlExtension;
17
use Ocubom\Twig\Extension\Svg\Finder;
18
use Ocubom\Twig\Extension\Svg\FinderInterface;
19
use Ocubom\Twig\Extension\Svg\Library\FontAwesome\Finder as FontAwesomeFinder;
20
use Ocubom\Twig\Extension\Svg\Library\FontAwesomeRuntime;
21
use Ocubom\Twig\Extension\SvgExtension;
22
use Ocubom\Twig\Extension\SvgRuntime;
23
use Ocubom\TwigExtraBundle\Extensions;
24
use Ocubom\TwigExtraBundle\Listener\AddHttpHeadersListener;
25
use Ocubom\TwigExtraBundle\Twig\WebpackEncoreExtension;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
1 ignored issue
show
Bug introduced by
The type Symfony\Component\Depend...ection\ContainerBuilder 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...
28
use Symfony\Component\DependencyInjection\Extension\Extension;
29
use Symfony\Component\DependencyInjection\Reference;
30
31
class OcubomTwigExtraExtension extends Extension
32
{
33 5
    public function load(array $configs, ContainerBuilder $container): void
34
    {
35 5
        $configuration = $this->getConfiguration($configs, $container);
36
        assert($configuration instanceof ConfigurationInterface);
37 5
        $config = $this->processConfiguration($configuration, $configs);
38
39 5
        foreach (array_keys(Extensions::getClasses()) as $name) {
40 5
            if ($this->isConfigEnabled($container, $config[$name])) {
41 5
                $call = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
42 5
                $this->{'load'.$call}($container, $config[$name]);
43
            }
44
        }
45
46 5
        $this->loadHttpHeaders($container, $config['http_headers']);
47
    }
48
49 5
    private function loadHttpHeaders(ContainerBuilder $container, array $config): void
50
    {
51
        // Filter enabled header rules
52 5
        $headers = array_filter($config, function (array $header): bool {
53 1
            return $header['enabled'] ? true : false;
54 5
        });
55
56
        // Only register listener if some rule is defined
57 5
        if (count($headers) > 0) {
58 1
            $container->register('ocubom_twig_extra.http_headers_listener', AddHttpHeadersListener::class)
59 1
                ->setArguments(array_values($headers))
60 1
                ->addTag('kernel.event_subscriber');
61
        }
62
    }
63
64 4
    private function loadHtml(ContainerBuilder $container, array $config): void
65
    {
66 4
        $container->register('ocubom_twig_extra.twig_html_extension', HtmlExtension::class)
67 4
            ->addTag('twig.extension');
68
69 4
        $container->register('ocubom_twig_extra.twig_html_attributes_runtime', HtmlAttributesRuntime::class)
70 4
            ->addTag('twig.runtime');
71
72 4
        $container->register('ocubom_twig_extra.twig_html_compress_runtime', HtmlCompressRuntime::class)
73 4
            ->setArguments([
74 4
                $config['compression']['force'],
75 4
                $config['compression']['level'],
76 4
            ])
77 4
            ->addTag('twig.runtime');
78
    }
79
80 1
    private function loadSvg(ContainerBuilder $container, array $config): void
81
    {
82 1
        if (empty($config['finders'])) {
83
            return;
84
        }
85
86 1
        $container
87 1
            ->register('ocubom_twig_extra.twig_svg_extension', SvgExtension::class)
88 1
            ->addTag('twig.extension');
89
90 1
        foreach ($config['finders'] as $name => $paths) {
91 1
            $hash = sha1(serialize($paths));
92 1
            $key = ".ocubom_twig_extra.svg.finder.{$hash}";
93
94
            // Register finder if not exists
95 1
            if (!$container->has($key)) {
96 1
                $container
97 1
                    ->register($key, Finder::class)
98 1
                    ->setArguments($paths)
99 1
                    ->setPublic(false);
100
            }
101
102
            // Create a hidden alias
103 1
            $container->setAlias(".ocubom_twig_extra.svg.{$name}_finder.inner", $key);
104
        }
105
106
        // Register default runtime
107 1
        if ($container->has('.ocubom_twig_extra.svg.default_finder.inner')) {
108
            // Register runtime
109 1
            $container
110 1
                ->register('ocubom_twig_extra.twig_svg_runtime', SvgRuntime::class)
111 1
                ->setArguments([
112 1
                    new Reference('ocubom_twig_extra.svg.default_finder'),
113 1
                    new Reference('logger'),
114 1
                ])
115 1
                ->addTag('twig.runtime');
116
117
            // Create default finder (just an alias)
118 1
            $container->setAlias('ocubom_twig_extra.svg.default_finder', '.ocubom_twig_extra.svg.default_finder.inner');
119
120
            // Create class aliases
121 1
            $container->setAlias(FinderInterface::class, 'ocubom_twig_extra.svg.default_finder');
122
        }
123
124
        // Register fontawesome runtime
125 1
        if ($container->has('.ocubom_twig_extra.svg.fontawesome_finder.inner')) {
126
            // Register runtime
127 1
            $container
128 1
                ->register('ocubom_twig_extra.twig_fontawesome_runtime', FontAwesomeRuntime::class)
129 1
                ->setArguments([
130 1
                    new Reference('ocubom_twig_extra.svg.fontawesome_finder'),
131 1
                    new Reference('logger'),
132 1
                ])
133 1
                ->addTag('twig.runtime');
134
135
            // Create fontawesome finder
136 1
            $container
137 1
                ->register('ocubom_twig_extra.svg.fontawesome_finder', FontAwesomeFinder::class)
138 1
                ->setArguments([
139 1
                    new Reference('.ocubom_twig_extra.svg.fontawesome_finder.inner'),
140 1
                    new Reference('logger'),
141 1
                ]);
142
143
            // Create class aliases
144 1
            $container->setAlias(FontAwesomeFinder::class, 'ocubom_twig_extra.svg.fontawesome_finder');
145
        }
146
    }
147
148 4
    private function loadWebpackEncore(ContainerBuilder $container, array $config): void
149
    {
150 4
        $container->register('ocubom_twig_extra.twig_webpack_encore_extension', WebpackEncoreExtension::class)
151 4
            ->setArguments([
152 4
                new Reference('webpack_encore.entrypoint_lookup_collection'),
153 4
                $config['output_paths'],
154 4
            ])
155 4
            ->addTag('twig.extension');
156
    }
157
}
158