Configuration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 60
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 35 1
A createMiddlewareNormalizer() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SymfonyMiddleware\DependencyInjection;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
12
final class Configuration implements ConfigurationInterface
13
{
14
    /**
15
     * @psalm-suppress MixedMethodCall
16
     * @psalm-suppress PossiblyUndefinedMethod
17
     * @psalm-suppress PossiblyNullReference
18
     */
19
    public function getConfigTreeBuilder(): TreeBuilder
20
    {
21
        $tree = new TreeBuilder('symiddleware');
22
23
        /** @var ArrayNodeDefinition $rootNode */
24
        $rootNode = $tree->getRootNode();
25
26
        $rootNode
27
            ->children()
28
                ->arrayNode('global')
29
                    ->scalarPrototype()
30
                        ->isRequired()
31
                        ->beforeNormalization()
32
                            ->always($this->createMiddlewareNormalizer())
33
                        ->end()
34
                    ->end()
35
                ->end()
0 ignored issues
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

35
                ->/** @scrutinizer ignore-call */ end()
Loading history...
36
                ->arrayNode('groups')
37
                    ->arrayPrototype()
38
                        ->children()
39
                            ->scalarNode('if')->end()
40
                            ->arrayNode('middlewares')
41
                                ->scalarPrototype()
42
                                ->isRequired()
43
                                ->beforeNormalization()
44
                                    ->always($this->createMiddlewareNormalizer())
45
                                ->end()
46
                            ->end()
47
                        ->end()
48
                    ->end()
49
                ->end()
50
            ->end()
51
        ;
52
53
        return $tree;
54
    }
55
56
    /**
57
     * @return \Closure
58
     */
59
    private function createMiddlewareNormalizer(): callable
60
    {
61
        return function (mixed $middlewareName): string {
62
            if (!\is_string($middlewareName) || !\is_a($middlewareName, MiddlewareInterface::class, true)) {
63
                throw new \RuntimeException(
64
                    vsprintf('Each middleware must implements the "%s" interface, but "%s" doesn\'t.', [
65
                        MiddlewareInterface::class,
66
                        $middlewareName,
67
                    ])
68
                );
69
            }
70
71
            return $middlewareName;
72
        };
73
    }
74
}
75