Completed
Push — master ( de1564...663938 )
by Florent
10:19
created

SignerSource::getNodeDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 19
nc 1
nop 1
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
        ]);
43
44
        $container->setDefinition($service_id, $definition);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getNodeDefinition(ArrayNodeDefinition $node)
51
    {
52
        $node
53
            ->children()
54
                ->arrayNode($this->getName())
55
                    ->useAttributeAsKey('name')
56
                    ->prototype('array')
57
                        ->children()
58
                            ->arrayNode('algorithms')
59
                                ->useAttributeAsKey('name')
60
                                ->isRequired()
61
                                ->prototype('scalar')->end()
62
                            ->end()
63
                            ->booleanNode('create_verifier')
64
                                ->defaultFalse()
65
                            ->end()
66
                        ->end()
67
                    ->end()
68
                ->end()
69
            ->end();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function prepend(ContainerBuilder $container, array $config)
76
    {
77
        if (false === array_key_exists($this->getName(), $config)) {
78
            return;
79
        }
80
81
        foreach ($config[$this->getName()] as $id => $section) {
82
            if (true === $section['create_verifier']) {
83
                $values = $section;
84
                unset($values['create_verifier']);
85
                $config['verifiers'] = array_merge(
86
                    array_key_exists('verifiers', $config) ? $config['verifiers'] : [],
87
                    [$id => $values]
88
                );
89
            }
90
        }
91
92
        return $config;
93
    }
94
}
95