Failed Conditions
Push — master ( c4fa4a...5cc583 )
by Florent
02:12
created

JWSBuilder::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\Signature\DependencyInjection\Source;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceInterface;
17
use Jose\Component\Signature\JWSBuilderFactory;
18
use Jose\Component\Signature\JWSBuilder as JWSBuilderService;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * Class JWSBuilder.
26
 */
27
final class JWSBuilder implements SourceInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function name(): string
33
    {
34
        return 'jws_builders';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function load(array $configs, ContainerBuilder $container)
41
    {
42
        $this->createService($configs[$this->name()], $container);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    private function createService(array $config, ContainerBuilder $container)
49
    {
50
        foreach ($config as $name => $itemConfig) {
51
            $service_id = sprintf('jose.jws_builder.%s', $name);
52
            $definition = new Definition(JWSBuilderService::class);
53
            $definition
54
                ->setFactory([new Reference(JWSBuilderFactory::class), 'create'])
55
                ->setArguments([$itemConfig['signature_algorithms']])
56
                ->setPublic($itemConfig['is_public']);
57
58
            $container->setDefinition($service_id, $definition);
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getNodeDefinition(ArrayNodeDefinition $node)
66
    {
67
        $node
68
            ->children()
69
                ->arrayNode($this->name())
70
                    ->useAttributeAsKey('name')
71
                    ->prototype('array')
72
                        ->children()
73
                            ->booleanNode('is_public')
74
                                ->info('If true, the service will be public, else private.')
75
                                ->defaultTrue()
76
                            ->end()
77
                            ->arrayNode('signature_algorithms')
78
                                ->useAttributeAsKey('name')
79
                                ->isRequired()
80
                                ->prototype('scalar')->end()
81
                            ->end()
82
                        ->end()
83
                    ->end()
84
                ->end()
85
            ->end();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function prepend(ContainerBuilder $container, array $config): ?array
92
    {
93
        return null;
94
    }
95
}
96