Failed Conditions
Push — master ( e08481...7ad838 )
by Florent
03:52 queued 01:57
created

NestedTokenLoader::load()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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\JoseFramework\DependencyInjection\Source\Encryption;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
17
use Jose\Component\Encryption\NestedTokenLoaderFactory;
18
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
class NestedTokenLoader implements Source
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function name(): string
29
    {
30
        return 'loaders';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function load(array $configs, ContainerBuilder $container)
37
    {
38
        foreach ($configs[$this->name()] as $name => $itemConfig) {
39
            $service_id = sprintf('jose.nested_token_loader.%s', $name);
40
            $definition = new Definition(self::class);
41
            $definition
42
                ->setFactory([new Reference(NestedTokenLoaderFactory::class), 'create'])
43
                ->setArguments([
44
                    $itemConfig['jwe_serializers'],
45
                    $itemConfig['key_encryption_algorithms'],
46
                    $itemConfig['content_encryption_algorithms'],
47
                    $itemConfig['compression_methods'],
48
                    $itemConfig['jwe_header_checkers'],
49
                    $itemConfig['jws_serializers'],
50
                    $itemConfig['signature_algorithms'],
51
                    $itemConfig['jws_header_checkers'],
52
                ])
53
                ->addTag('jose.nested_token_loader')
54
                ->setPublic($itemConfig['is_public']);
55
            foreach ($itemConfig['tags'] as $id => $attributes) {
56
                $definition->addTag($id, $attributes);
57
            }
58
            $container->setDefinition($service_id, $definition);
59
        }
60
    }
61
62
    public function getNodeDefinition(NodeDefinition $node)
63
    {
64
        $node->children()
65
            ->arrayNode($this->name())
66
                ->treatNullLike([])
67
                ->treatFalseLike([])
68
                ->useAttributeAsKey('name')
69
                ->arrayPrototype()
70
                    ->children()
71
                        ->booleanNode('is_public')
72
                            ->info('If true, the service will be public, else private.')
73
                            ->defaultTrue()
74
                        ->end()
75
                        ->arrayNode('signature_algorithms')
76
                            ->info('A list of signature algorithm aliases.')
77
                            ->useAttributeAsKey('name')
78
                            ->isRequired()
79
                            ->scalarPrototype()->end()
80
                        ->end()
81
                        ->arrayNode('key_encryption_algorithms')
82
                            ->info('A list of key encryption algorithm aliases.')
83
                            ->useAttributeAsKey('name')
84
                            ->isRequired()
85
                            ->scalarPrototype()->end()
86
                        ->end()
87
                        ->arrayNode('content_encryption_algorithms')
88
                            ->info('A list of key encryption algorithm aliases.')
89
                            ->useAttributeAsKey('name')
90
                            ->isRequired()
91
                            ->scalarPrototype()->end()
92
                        ->end()
93
                        ->arrayNode('compression_methods')
94
                            ->info('A list of compression method aliases.')
95
                            ->useAttributeAsKey('name')
96
                            ->defaultValue(['DEF'])
97
                            ->scalarPrototype()->end()
98
                        ->end()
99
                        ->arrayNode('jws_serializers')
100
                            ->info('A list of JWS serializer aliases.')
101
                            ->useAttributeAsKey('name')
102
                            ->treatNullLike([])
103
                            ->treatFalseLike([])
104
                            ->isRequired()
105
                            ->requiresAtLeastOneElement()
106
                            ->scalarPrototype()->end()
107
                        ->end()
108
                        ->arrayNode('jwe_serializers')
109
                            ->info('A list of JWE serializer aliases.')
110
                            ->useAttributeAsKey('name')
111
                            ->treatNullLike([])
112
                            ->treatFalseLike([])
113
                            ->isRequired()
114
                            ->requiresAtLeastOneElement()
115
                            ->scalarPrototype()->end()
116
                        ->end()
117
                        ->arrayNode('jws_header_checkers')
118
                            ->info('A list of header checker aliases.')
119
                            ->useAttributeAsKey('name')
120
                            ->treatNullLike([])
121
                            ->treatFalseLike([])
122
                            ->scalarPrototype()->end()
123
                        ->end()
124
                        ->arrayNode('jwe_header_checkers')
125
                            ->info('A list of header checker aliases.')
126
                            ->useAttributeAsKey('name')
127
                            ->treatNullLike([])
128
                            ->treatFalseLike([])
129
                            ->scalarPrototype()->end()
130
                        ->end()
131
                        ->arrayNode('tags')
132
                            ->info('A list of tags to be associated to the service.')
133
                            ->useAttributeAsKey('name')
134
                            ->treatNullLike([])
135
                            ->treatFalseLike([])
136
                            ->variablePrototype()->end()
137
                        ->end()
138
                    ->end()
139
                ->end()
140
            ->end();
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function prepend(ContainerBuilder $container, array $config): array
147
    {
148
        return [];
149
    }
150
}
151