Completed
Push — master ( a6e833...2ffd36 )
by Florent
02:08
created

EncrypterSource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A createService() 0 17 2
A getNodeDefinition() 0 18 1
B prepend() 0 20 5
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
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getNodeDefinition(ArrayNodeDefinition $node)
56
    {
57
        $node
58
            ->children()
59
                ->arrayNode($this->getName())
60
                    ->useAttributeAsKey('name')
61
                    ->prototype('array')
62
                        ->children()
63
                            ->arrayNode('key_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
64
                            ->arrayNode('content_encryption_algorithms')->isRequired()->prototype('scalar')->end()->end()
65
                            ->arrayNode('compression_methods')->defaultValue(['DEF'])->prototype('scalar')->end()->end()
66
                            ->scalarNode('logger')->defaultNull()->end()
67
                            ->booleanNode('create_decrypter')->defaultFalse()->end()
68
                        ->end()
69
                    ->end()
70
                ->end()
71
            ->end();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function prepend(ContainerBuilder $container, array $config)
78
    {
79
        if (false === array_key_exists($this->getName(), $config)) {
80
            return;
81
        }
82
83
        foreach ($config[$this->getName()] as $id=>$section) {
84
            if (true === $section['create_decrypter']) {
85
86
                $values = $section;
87
                unset($values['create_decrypter']);
88
                $config['decrypters'] = array_merge(
89
                    array_key_exists('decrypters', $config) ? $config['decrypters'] : [],
90
                    [$id => $values]
91
                );
92
            }
93
        }
94
95
        return $config;
96
    }
97
}
98