Failed Conditions
Push — master ( ea06df...34d59f )
by Florent
10:18
created

JWEDecrypter::createService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 2
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\JWEDecrypterFactory;
18
use Jose\Component\Encryption\JWEDecrypter as JWEDecrypterService;
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 JWEDecrypter.
26
 */
27
final class JWEDecrypter implements SourceInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function name(): string
33
    {
34
        return 'jwe_decrypters';
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_decrypter.%s', $name);
52
            $definition = new Definition(JWEDecrypterService::class);
53
            $definition
54
                ->setFactory([new Reference(JWEDecrypterFactory::class), 'create'])
55
                ->setArguments([
56
                    $itemConfig['key_encryption_algorithms'],
57
                    $itemConfig['content_encryption_algorithms'],
58
                    $itemConfig['compression_methods'],
59
                    $itemConfig['header_checkers'],
60
                ])
61
                ->addTag('jose.jwe_decrypter')
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
                        ->end()
108
                    ->end()
109
                ->end()
110
            ->end();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function prepend(ContainerBuilder $container, array $config): ?array
117
    {
118
        return null;
119
    }
120
}
121