Completed
Push — master ( 8b6623...b6ba4a )
by Florent
08:35
created

EncrypterSource::createService()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 12
nc 1
nop 3
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\DependencyInjection\Source;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
final class EncrypterSource implements SourceInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getName()
25
    {
26
        return 'encrypters';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function createService($name, array $config, ContainerBuilder $container)
33
    {
34
        $service_id = sprintf('jose.encrypter.%s', $name);
35
        $definition = new Definition('Jose\Encrypter');
36
        $definition->setFactory([
37
            new Reference('jose.factory.service'),
38
            'createEncrypter',
39
        ]);
40
        $definition->setArguments([
41
            $config['key_encryption_algorithms'],
42
            $config['content_encryption_algorithms'],
43
            $config['compression_methods'],
44
            null === $config['logger'] ? null : new Reference($config['logger']),
45
        ]);
46
47
        $container->setDefinition($service_id, $definition);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getNodeDefinition(ArrayNodeDefinition $node)
54
    {
55
        $node
56
            ->children()
57
                ->arrayNode($this->getName())
58
                    ->useAttributeAsKey('name')
59
                    ->prototype('array')
60
                        ->children()
61
                            ->arrayNode('key_encryption_algorithms')
62
                                ->useAttributeAsKey('name')
63
                                ->isRequired()
64
                                ->prototype('scalar')->end()
65
                            ->end()
66
                            ->arrayNode('content_encryption_algorithms')
67
                                ->useAttributeAsKey('name')
68
                                ->isRequired()
69
                                ->prototype('scalar')->end()
70
                            ->end()
71
                            ->arrayNode('compression_methods')
72
                                ->useAttributeAsKey('name')
73
                                ->defaultValue(['DEF'])
74
                                ->prototype('scalar')->end()
75
                            ->end()
76
                            ->scalarNode('logger')
77
                                ->defaultNull()
78
                            ->end()
79
                            ->booleanNode('create_decrypter')
80
                                ->defaultFalse()
81
                            ->end()
82
                        ->end()
83
                    ->end()
84
                ->end()
85
            ->end();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function prepend(ContainerBuilder $container, array $config)
92
    {
93
        if (false === array_key_exists($this->getName(), $config)) {
94
            return;
95
        }
96
97
        foreach ($config[$this->getName()] as $id => $section) {
98
            if (true === $section['create_decrypter']) {
99
                $values = $section;
100
                unset($values['create_decrypter']);
101
                $config['decrypters'] = array_merge(
102
                    array_key_exists('decrypters', $config) ? $config['decrypters'] : [],
103
                    [$id => $values]
104
                );
105
            }
106
        }
107
108
        return $config;
109
    }
110
}
111