Failed Conditions
Push — v7 ( adb166...809898 )
by Florent
02:37
created

ConfigurationHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addJWSBuilder() 0 5 1
A getJWSBuilderConfiguration() 0 13 1
A updateJoseConfiguration() 0 9 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\JoseFramework\Helper;
15
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * This helper will help you to create services configuration.
20
 */
21
final class ConfigurationHelper
22
{
23
    const BUNDLE_ALIAS = 'jose';
24
25
    /**
26
     * @param ContainerBuilder $container
27
     * @param string           $name
28
     * @param string[]         $signatureAlgorithms
29
     * @param bool             $is_public
30
     */
31
    public static function addJWSBuilder(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true)
32
    {
33
        $config = self::getJWSBuilderConfiguration($name, $signatureAlgorithms, $is_public);
34
        self::updateJoseConfiguration($container, $config, 'jws_builders');
35
    }
36
37
    /**
38
     * @param string   $name
39
     * @param string[] $signatureAlgorithms
40
     * @param bool     $is_public
41
     *
42
     * @return array
43
     */
44
    private static function getJWSBuilderConfiguration(string $name, array $signatureAlgorithms, bool $is_public = true)
45
    {
46
        return [
47
            self::BUNDLE_ALIAS => [
48
                'jws_builders' => [
49
                    $name => [
50
                        'is_public'            => $is_public,
51
                        'signature_algorithms' => $signatureAlgorithms,
52
                    ],
53
                ],
54
            ],
55
        ];
56
    }
57
58
    /**
59
     * @param ContainerBuilder $container
60
     * @param array            $config
61
     * @param string           $element
62
     */
63
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
64
    {
65
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
66
        if (!isset($jose_config[$element])) {
67
            $jose_config[$element] = [];
68
        }
69
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
70
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
71
    }
72
}
73