Failed Conditions
Push — v7 ( 8da077...12d27f )
by Florent
02:14
created

JWELoader::getNodeDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.0303
c 0
b 0
f 0
cc 1
eloc 45
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\Encryption\DependencyInjection\Source;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
17
use Jose\Component\Encryption\JWELoaderFactory;
18
use Jose\Component\Encryption\JWELoader as JWELoaderService;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * Class JWELoader.
26
 */
27
final class JWELoader implements SourceInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function name(): string
33
    {
34
        return 'jwe_loaders';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function createService(string $name, array $config, ContainerBuilder $container)
41
    {
42
        $service_id = sprintf('jose.jwe_loader.%s', $name);
43
        $definition = new Definition(JWELoaderService::class);
44
        $definition
45
            ->setFactory([new Reference(JWELoaderFactory::class), 'create'])
46
            ->setArguments([
47
                $config['key_encryption_algorithms'],
48
                $config['content_encryption_algorithms'],
49
                $config['compression_methods'],
50
                $config['header_checkers'],
51
                $config['serializers'],
52
            ])
53
            ->setPublic($config['is_public']);
54
55
        $container->setDefinition($service_id, $definition);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getNodeDefinition(ArrayNodeDefinition $node)
62
    {
63
        $node
64
            ->children()
65
                ->arrayNode($this->name())
66
                    ->useAttributeAsKey('name')
67
                    ->prototype('array')
68
                        ->children()
69
                            ->booleanNode('is_public')
70
                                ->info('If true, the service will be public, else private.')
71
                                ->defaultTrue()
72
                            ->end()
73
                            ->arrayNode('key_encryption_algorithms')
74
                                ->info('A list of supported key encryption algorithms.')
75
                                ->useAttributeAsKey('name')
76
                                ->isRequired()
77
                                ->prototype('scalar')->end()
78
                            ->end()
79
                            ->arrayNode('content_encryption_algorithms')
80
                                ->info('A list of supported content encryption algorithms.')
81
                                ->useAttributeAsKey('name')
82
                                ->isRequired()
83
                                ->prototype('scalar')->end()
84
                            ->end()
85
                            ->arrayNode('compression_methods')
86
                                ->info('A list of supported compression methods.')
87
                                ->useAttributeAsKey('name')
88
                                ->defaultValue(['DEF'])
89
                                ->prototype('scalar')->end()
90
                            ->end()
91
                            ->arrayNode('header_checkers')
92
                                ->info('A list of headers to check.')
93
                                ->useAttributeAsKey('name')
94
                                ->isRequired()
95
                                ->prototype('scalar')->end()
96
                            ->end()
97
                            ->arrayNode('serializers')
98
                                ->info('A list of serializers.')
99
                                ->useAttributeAsKey('name')
100
                                ->treatNullLike(['jwe_compact'])
101
                                ->prototype('scalar')->end()
102
                            ->end()
103
                        ->end()
104
                    ->end()
105
                ->end()
106
            ->end();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function prepend(ContainerBuilder $container, array $config): ?array
113
    {
114
        return null;
115
    }
116
}
117