JWESerializer::getNodeDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\JoseFramework\DependencyInjection\Source\Encryption;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
17
use Jose\Component\Encryption\Serializer\JWESerializerManager;
18
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
19
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
class JWESerializer implements Source
25
{
26
    public function name(): string
27
    {
28
        return 'serializers';
29
    }
30
31
    public function load(array $configs, ContainerBuilder $container): void
32
    {
33
        foreach ($configs[$this->name()] as $name => $itemConfig) {
34
            $service_id = sprintf('jose.jwe_serializer.%s', $name);
35
            $definition = new Definition(JWESerializerManager::class);
36
            $definition
37
                ->setFactory([new Reference(JWESerializerManagerFactory::class), 'create'])
38
                ->setArguments([$itemConfig['serializers']])
39
                ->addTag('jose.jwe_serializer_manager')
40
                ->setPublic($itemConfig['is_public'])
41
            ;
42
            foreach ($itemConfig['tags'] as $id => $attributes) {
43
                $definition->addTag($id, $attributes);
44
            }
45
            $container->setDefinition($service_id, $definition);
46
        }
47
    }
48
49
    public function getNodeDefinition(NodeDefinition $node): void
50
    {
51
        $node->children()
52
            ->arrayNode($this->name())
53
            ->treatFalseLike([])
54
            ->treatNullLike([])
55
            ->useAttributeAsKey('name')
56
            ->arrayPrototype()
57
            ->children()
58
            ->booleanNode('is_public')
59
            ->info('If true, the service will be public, else private.')
60
            ->defaultTrue()
61
            ->end()
62
            ->arrayNode('serializers')
63
            ->info('A list of JWE serializers aliases.')
64
            ->isRequired()
65
            ->scalarPrototype()->end()
66
            ->treatNullLike([])
67
            ->treatFalseLike([])
68
            ->requiresAtLeastOneElement()
69
            ->end()
70
            ->arrayNode('tags')
71
            ->info('A list of tags to be associated to the service.')
72
            ->useAttributeAsKey('name')
73
            ->treatNullLike([])
74
            ->treatFalseLike([])
75
            ->variablePrototype()->end()
76
            ->end()
77
            ->end()
78
            ->end()
79
            ->end()
80
            ->end()
81
        ;
82
    }
83
84
    public function prepend(ContainerBuilder $container, array $config): array
85
    {
86
        return [];
87
    }
88
}
89