Failed Conditions
Push — master ( df44e5...b25e13 )
by Florent
02:17
created

JWELoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 15
nc 2
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\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 Source
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function name(): string
33
    {
34
        return 'loaders';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function load(array $configs, ContainerBuilder $container)
41
    {
42
        foreach ($configs[$this->name()] as $name => $itemConfig) {
43
            $service_id = sprintf('jose.jwe_loader.%s', $name);
44
            $definition = new Definition(JWELoaderService::class);
45
            $definition
46
                ->setFactory([new Reference(JWELoaderFactory::class), 'create'])
47
                ->setArguments([
48
                    $itemConfig['serializers'],
49
                    $itemConfig['key_encryption_algorithms'],
50
                    $itemConfig['content_encryption_algorithms'],
51
                    $itemConfig['compression_methods'],
52
                    $itemConfig['header_checkers'],
53
                ])
54
                ->addTag('jose.jwe_loader')
55
                ->setPublic($itemConfig['is_public']);
56
57
            $container->setDefinition($service_id, $definition);
58
        }
59
    }
60
61
    public function getNodeDefinition(ArrayNodeDefinition $node)
62
    {
63
        $node
64
            ->children()
65
                ->arrayNode($this->name())
66
                    ->requiresAtLeastOneElement()
67
                    ->useAttributeAsKey('name')
68
                    ->prototype('array')
69
                        ->children()
70
                            ->booleanNode('is_public')
71
                                ->info('If true, the service will be public, else private.')
72
                                ->defaultTrue()
73
                            ->end()
74
                            ->arrayNode('key_encryption_algorithms')
75
                                ->info('A list of key encryption algorithm aliases.')
76
                                ->useAttributeAsKey('name')
77
                                ->isRequired()
78
                                ->prototype('scalar')->end()
79
                            ->end()
80
                            ->arrayNode('content_encryption_algorithms')
81
                                ->info('A list of key encryption algorithm aliases.')
82
                                ->useAttributeAsKey('name')
83
                                ->isRequired()
84
                                ->prototype('scalar')->end()
85
                            ->end()
86
                            ->arrayNode('compression_methods')
87
                                ->info('A list of compression method aliases.')
88
                                ->useAttributeAsKey('name')
89
                                ->isRequired()
90
                                ->prototype('scalar')->end()
91
                            ->end()
92
                            ->arrayNode('serializers')
93
                                ->info('A list of signature serializer aliases.')
94
                                ->useAttributeAsKey('name')
95
                                ->requiresAtLeastOneElement()
96
                                ->prototype('scalar')->end()
97
                            ->end()
98
                            ->arrayNode('header_checkers')
99
                                ->info('A list of header checker aliases.')
100
                                ->useAttributeAsKey('name')
101
                                ->treatNullLike([])
102
                                ->treatFalseLike([])
103
                                ->prototype('scalar')->end()
104
                            ->end()
105
                        ->end()
106
                    ->end()
107
                ->end()
108
            ->end();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function prepend(ContainerBuilder $container, array $config): array
115
    {
116
        return [];
117
    }
118
}
119