Failed Conditions
Push — master ( c4fa4a...5cc583 )
by Florent
02:12
created

JWELoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 100
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 4 1
A load() 0 4 1
A createService() 0 19 2
A getNodeDefinition() 0 47 1
A prepend() 0 4 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 load(array $configs, ContainerBuilder $container)
41
    {
42
        $this->createService($configs[$this->name()], $container);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    private function createService(array $config, ContainerBuilder $container)
49
    {
50
        foreach ($config as $name => $itemConfig) {
51
            $service_id = sprintf('jose.jwe_loader.%s', $name);
52
            $definition = new Definition(JWELoaderService::class);
53
            $definition
54
                ->setFactory([new Reference(JWELoaderFactory::class), 'create'])
55
                ->setArguments([
56
                    $itemConfig['key_encryption_algorithms'],
57
                    $itemConfig['content_encryption_algorithms'],
58
                    $itemConfig['compression_methods'],
59
                    $itemConfig['header_checkers'],
60
                    $itemConfig['serializers'],
61
                ])
62
                ->setPublic($itemConfig['is_public']);
63
64
            $container->setDefinition($service_id, $definition);
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getNodeDefinition(ArrayNodeDefinition $node)
72
    {
73
        $node
74
            ->children()
75
                ->arrayNode($this->name())
76
                    ->useAttributeAsKey('name')
77
                    ->prototype('array')
78
                        ->children()
79
                            ->booleanNode('is_public')
80
                                ->info('If true, the service will be public, else private.')
81
                                ->defaultTrue()
82
                            ->end()
83
                            ->arrayNode('key_encryption_algorithms')
84
                                ->info('A list of supported key encryption algorithms.')
85
                                ->useAttributeAsKey('name')
86
                                ->isRequired()
87
                                ->prototype('scalar')->end()
88
                            ->end()
89
                            ->arrayNode('content_encryption_algorithms')
90
                                ->info('A list of supported content encryption algorithms.')
91
                                ->useAttributeAsKey('name')
92
                                ->isRequired()
93
                                ->prototype('scalar')->end()
94
                            ->end()
95
                            ->arrayNode('compression_methods')
96
                                ->info('A list of supported compression methods.')
97
                                ->useAttributeAsKey('name')
98
                                ->defaultValue(['DEF'])
99
                                ->prototype('scalar')->end()
100
                            ->end()
101
                            ->arrayNode('header_checkers')
102
                                ->info('A list of headers to check.')
103
                                ->useAttributeAsKey('name')
104
                                ->isRequired()
105
                                ->prototype('scalar')->end()
106
                            ->end()
107
                            ->arrayNode('serializers')
108
                                ->info('A list of serializers.')
109
                                ->useAttributeAsKey('name')
110
                                ->treatNullLike(['jwe_compact'])
111
                                ->prototype('scalar')->end()
112
                            ->end()
113
                        ->end()
114
                    ->end()
115
                ->end()
116
            ->end();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function prepend(ContainerBuilder $container, array $config): ?array
123
    {
124
        return null;
125
    }
126
}
127