SignerSource::getNodeDefinition()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
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
        $definition->setPublic($config['is_public']);
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
                            ->booleanNode('is_public')
60
                                ->info('If true, the service will be public, else private.')
61
                                ->defaultTrue()
62
                            ->end()
63
                            ->arrayNode('algorithms')
64
                                ->useAttributeAsKey('name')
65
                                ->isRequired()
66
                                ->prototype('scalar')->end()
67
                            ->end()
68
                            ->booleanNode('create_verifier')
69
                                ->defaultFalse()
70
                            ->end()
71
                        ->end()
72
                    ->end()
73
                ->end()
74
            ->end();
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function prepend(ContainerBuilder $container, array $config)
81
    {
82
        if (false === array_key_exists($this->getName(), $config)) {
83
            return;
84
        }
85
86
        foreach ($config[$this->getName()] as $id => $section) {
87
            if (true === $section['create_verifier']) {
88
                $values = $section;
89
                unset($values['create_verifier']);
90
                $config['verifiers'] = array_merge(
91
                    array_key_exists('verifiers', $config) ? $config['verifiers'] : [],
92
                    [$id => $values]
93
                );
94
            }
95
        }
96
97
        return $config;
98
    }
99
}
100