Passed
Push — main ( d6cdce...b163db )
by Oscar
16:28 queued 15:04
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 2
rs 10
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\TwigExtraBundle\Extensions;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
class Configuration implements ConfigurationInterface
21
{
22 2
    public function getConfigTreeBuilder(): TreeBuilder
23
    {
24 2
        $builder = new TreeBuilder('ocubom_twig_extra');
25 2
        $root = $builder->getRootNode();
26
27
        // Register available extensions
28 2
        foreach (Extensions::getClasses() as $name => $class) {
29 2
            $this->{'addTwig'.ucfirst($name).'ExtensionSection'}(
30 2
                $this->createSectionNode($root, $name, class_exists($class))
31 2
            );
32
        }
33
34
        // Headers listener included on this bundle
35 2
        $this->addHttpHeaderSection($root);
36
37 2
        return $builder;
38
    }
39
40 2
    private function createSectionNode(
41
        NodeDefinition $root,
42
        string $name,
43
        bool $canBeDisabled = true
44
    ): ArrayNodeDefinition {
45 2
        return $root
46 2
            ->children()
47 2
                ->arrayNode($name)
48 2
                    ->info(sprintf(
49 2
                        'Twig %s Extension',
50 2
                        ucfirst($name)
51 2
                    ))
52 2
                    ->{$canBeDisabled ? 'canBeDisabled' : 'canBeEnabled'}();
53
    }
54
55 2
    private function addHttpHeaderSection(NodeDefinition $root): void
56
    {
57 2
        $root
58 2
            ->fixXmlConfig('http_header')
0 ignored issues
show
Bug introduced by
The method fixXmlConfig() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

58
            ->/** @scrutinizer ignore-call */ 
59
              fixXmlConfig('http_header')
Loading history...
59 2
            ->children()
60 2
                ->arrayNode('http_headers')
61 2
                    ->info(implode("\n", [
62 2
                        'HTTP headers that must be set.',
63 2
                        'The listener will only be registered if at least one rule is enabled.',
64 2
                    ]))
65 2
                    ->prototype('array')
66 2
                        ->fixXmlConfig('header')
67 2
                        ->treatFalseLike(['enabled' => false])
68 2
                        ->treatTrueLike(['enabled' => true])
69 2
                        ->treatNullLike(['enabled' => true])
70 2
                        ->children()
71 2
                            ->booleanNode('enabled')
72 2
                                ->info('Apply this rule?')
73 2
                                ->defaultTrue()
74 2
                            ->end()
75 2
                            ->scalarNode('name')
76 2
                                ->info('The header name to be added.')
77 2
                                ->example('X-UA-Compatible')
78 2
                                ->isRequired()
79 2
                                ->cannotBeEmpty()
80 2
                            ->end()
81 2
                            ->scalarNode('pattern')
82 2
                                ->info('A regular expression to extract the header value.')
83 2
                                ->example('@@[\p{Zs}]*<meta\s+(?:http-equiv="X-UA-Compatible"\s+content="([^"]+)"|content="([^"]+)"\s+http-equiv="X-UA-Compatible")\s*>\p{Zs}*\n?@i')
84 2
                                ->isRequired()
85 2
                                ->cannotBeEmpty()
86 2
                            ->end()
87 2
                            ->scalarNode('value')
88 2
                                ->info('The format of the value (printf processed using the matched value).')
89 2
                                ->example('%2$s')
90 2
                                ->isRequired()
91 2
                                ->cannotBeEmpty()
92 2
                            ->end()
93 2
                            ->scalarNode('replace')
94 2
                                ->info('The text that replaces the match in the original (printf processed using the matched value).')
95 2
                                // ->example('')
96 2
                                ->defaultValue('%s')
97 2
                            ->end()
98 2
                            ->arrayNode('formats')
99 2
                                ->info('The response formats when this replacement must be done.')
100 2
                                ->example('["text/html"]')
101 2
                                // ->addDefaultChildrenIfNoneSet()
102 2
                                ->prototype('scalar')
103 2
                                    // ->defaultValue('text/html')
104 2
                                ->end()
105 2
                            ->end()
106 2
                        ->end()
107 2
                    ->end()
108 2
                ->end()
109 2
            ->end();
110
    }
111
112 2
    private function addTwigHtmlExtensionSection(ArrayNodeDefinition $root): void
113
    {
114 2
        $root
115 2
            ->children()
116 2
                ->arrayNode('compression')
117 2
                    ->addDefaultsIfNotSet()
118 2
                    ->children()
119 2
                        ->booleanNode('force')
120 2
                            ->info('Force compression?')
121 2
                            ->defaultFalse()
122 2
                        ->end()
123 2
                        ->enumNode('level')
0 ignored issues
show
Bug introduced by
The method enumNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

123
                        ->/** @scrutinizer ignore-call */ enumNode('level')
Loading history...
124 2
                            ->info('The level of compression to use')
125 2
                            ->defaultValue('smallest')
126 2
                            ->values(['none', 'fastest', 'normal', 'smallest'])
127 2
                        ->end()
128 2
                    ->end()
129 2
                ->end()
130 2
            ->end();
131
    }
132
133 2
    private function addTwigSvgExtensionSection(ArrayNodeDefinition $root): void
134
    {
135 2
        $root
136 2
            ->children()
137 2
                ->arrayNode('search_path')
138 2
                    ->info('The paths where the SVG files will be searched for.')
139 2
                    ->example('["%kernel.project_dir%/assets", "%kernel.project_dir%/node_modules/"]')
140 2
                    ->defaultValue([
141 2
                        '%kernel.project_dir%/assets',
142 2
                        '%kernel.project_dir%/node_modules',
143 2
                    ])
144 2
                    ->prototype('scalar')
145 2
                    ->end()
146 2
                ->end()
1 ignored issue
show
Bug introduced by
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

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

146
                ->/** @scrutinizer ignore-call */ end()
Loading history...
147 2
            ->end();
148
    }
149
}
150