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

SignerSource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A createService() 0 15 2
B getNodeDefinition() 0 24 1
B prepend() 0 19 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 SignerSource implements SourceInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getName()
25
    {
26
        return 'signers';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function createService($name, array $config, ContainerBuilder $container)
33
    {
34
        $service_id = sprintf('jose.signer.%s', $name);
35
        $definition = new Definition('Jose\Signer');
36
        $definition->setFactory([
37
            new Reference('jose.factory.service'),
38
            'createSigner',
39
        ]);
40
        $definition->setArguments([
41
            $config['algorithms'],
42
            null === $config['logger'] ? null : new Reference($config['logger']),
43
        ]);
44
45
        $container->setDefinition($service_id, $definition);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getNodeDefinition(ArrayNodeDefinition $node)
52
    {
53
        $node
54
            ->children()
55
                ->arrayNode($this->getName())
56
                    ->useAttributeAsKey('name')
57
                    ->prototype('array')
58
                        ->children()
59
                            ->arrayNode('algorithms')
60
                                ->useAttributeAsKey('name')
61
                                ->isRequired()
62
                                ->prototype('scalar')->end()
63
                            ->end()
64
                            ->scalarNode('logger')
65
                                ->defaultNull()
66
                            ->end()
67
                            ->booleanNode('create_verifier')
68
                                ->defaultFalse()
69
                            ->end()
70
                        ->end()
71
                    ->end()
72
                ->end()
73
            ->end();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function prepend(ContainerBuilder $container, array $config)
80
    {
81
        if (false === array_key_exists($this->getName(), $config)) {
82
            return;
83
        }
84
85
        foreach ($config[$this->getName()] as $id => $section) {
86
            if (true === $section['create_verifier']) {
87
                $values = $section;
88
                unset($values['create_verifier']);
89
                $config['verifiers'] = array_merge(
90
                    array_key_exists('verifiers', $config) ? $config['verifiers'] : [],
91
                    [$id => $values]
92
                );
93
            }
94
        }
95
96
        return $config;
97
    }
98
}
99