Failed Conditions
Push — master ( d9377e...587e80 )
by Florent
02:05
created

AbstractJWKSetSource::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 4
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\KeyManagement\DependencyInjection\Source\JWKSetSource;
15
16
use Jose\Bundle\KeyManagement\Controller\JWKSetController;
17
use Jose\Bundle\KeyManagement\Controller\JWKSetControllerFactory;
18
use Jose\Bundle\JoseFramework\DependencyInjection\Source\AbstractSource;
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
/**
25
 * Class AbstractJWKSetSource.
26
 */
27
abstract class AbstractJWKSetSource extends AbstractSource implements JWKSetSourceInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function create(ContainerBuilder $container, string $type, string $name, array $config)
33
    {
34
        parent::create($container, $type, $name, $config);
35
36
        if (null !== $config['path']) {
37
            $jwkset_id = sprintf('jose.key_set.%s', $name);
38
            $controller_definition = new Definition(JWKSetController::class);
39
            $controller_definition->setFactory([new Reference(JWKSetControllerFactory::class), 'create']);
40
            $controller_definition->setArguments([new Reference($jwkset_id), $config['max_age']]);
41
            $controller_definition->addTag('jose.key_set.controller', ['path' => $config['path']]);
42
            $controller_id = sprintf('jose.controller.%s', $name);
43
            $container->setDefinition($controller_id, $controller_definition);
44
        }
45
    }
46
47
    /**
48
     * @param NodeDefinition $node
49
     */
50
    public function addConfiguration(NodeDefinition $node)
51
    {
52
        parent::addConfiguration($node);
53
        $node
54
            ->children()
55
                ->scalarNode('path')
56
                    ->info('To share the JWKSet, then set a valid path (e.g. "/jwkset.json").')
57
                    ->defaultNull()
58
                ->end()
59
                ->integerNode('max_age')
60
                    ->info('When share, this value indicates how many seconds the HTTP client should keep the key in cache. Default is 21600 = 6 hours.')
61
                    ->defaultValue(21600)
62
                ->end()
63
            ->end();
64
    }
65
}
66