Failed Conditions
Push — v7 ( 8da077...12d27f )
by Florent
02:14
created

ConfigurationHelper::addJWELoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 = [
34
            self::BUNDLE_ALIAS => [
35
                'jws_builders' => [
36
                    $name => [
37
                        'is_public' => $is_public,
38
                        'signature_algorithms' => $signatureAlgorithms,
39
                    ],
40
                ],
41
            ],
42
        ];
43
        self::updateJoseConfiguration($container, $config, 'jws_builders');
44
    }
45
46
    /**
47
     * @param ContainerBuilder $container
48
     * @param string           $name
49
     * @param string[]         $signatureAlgorithms
50
     * @param string[]         $headerCheckers
51
     * @param string[]         $serializers
52
     * @param bool             $is_public
53
     */
54
    public static function addJWSLoader(ContainerBuilder $container, string $name, array $signatureAlgorithms, array  $headerCheckers, array $serializers = ['jws_compact'], bool $is_public = true)
55
    {
56
        $config = [
57
            self::BUNDLE_ALIAS => [
58
                'jws_loaders' => [
59
                    $name => [
60
                        'is_public' => $is_public,
61
                        'signature_algorithms' => $signatureAlgorithms,
62
                        'header_checkers' => $headerCheckers,
63
                        'serializers' => $serializers,
64
                    ],
65
                ],
66
            ],
67
        ];
68
69
        self::updateJoseConfiguration($container, $config, 'jws_loaders');
70
    }
71
72
    /**
73
     * @param ContainerBuilder $container
74
     * @param string           $name
75
     * @param string[]         $claimCheckers
76
     * @param bool             $is_public
77
     */
78
    public static function addClaimChecker(ContainerBuilder $container, string $name, array  $claimCheckers, bool $is_public = true)
79
    {
80
        $config = [
81
            self::BUNDLE_ALIAS => [
82
                'claim_checkers' => [
83
                    $name => [
84
                        'is_public' => $is_public,
85
                        'claims' => $claimCheckers,
86
                    ],
87
                ],
88
            ],
89
        ];
90
91
        self::updateJoseConfiguration($container, $config, 'claim_checkers');
92
    }
93
94
    /**
95
     * @param ContainerBuilder $container
96
     * @param string           $name
97
     * @param string           $type
98
     * @param array            $parameters
99
     */
100
    public static function addKey(ContainerBuilder $container, string $name, string $type, array  $parameters)
101
    {
102
        $config = [
103
            self::BUNDLE_ALIAS => [
104
                'keys' => [
105
                    $name => [
106
                        $type => $parameters,
107
                    ],
108
                ],
109
            ],
110
        ];
111
112
        self::updateJoseConfiguration($container, $config, 'keys');
113
    }
114
115
    /**
116
     * @param ContainerBuilder $container
117
     * @param string           $name
118
     * @param string           $type
119
     * @param array            $parameters
120
     */
121
    public static function addKeyset(ContainerBuilder $container, string $name, string $type, array  $parameters)
122
    {
123
        $config = [
124
            self::BUNDLE_ALIAS => [
125
                'key_sets' => [
126
                    $name => [
127
                        $type => $parameters,
128
                    ],
129
                ],
130
            ],
131
        ];
132
133
        self::updateJoseConfiguration($container, $config, 'key_sets');
134
    }
135
136
    /**
137
     * @param ContainerBuilder $container
138
     * @param string           $name
139
     * @param array            $keyEncryptionAlgorithm
140
     * @param array            $contentEncryptionAlgorithms
141
     * @param array            $compressionMethods
142
     * @param bool             $is_public
143
     */
144
    public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true)
145
    {
146
        $config = [
147
            self::BUNDLE_ALIAS => [
148
                'jwe_builders' => [
149
                    $name => [
150
                        'is_public' => $is_public,
151
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
152
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
153
                        'compression_methods' => $compressionMethods,
154
                    ],
155
                ],
156
            ],
157
        ];
158
159
        self::updateJoseConfiguration($container, $config, 'jwe_builders');
160
    }
161
162
    /**
163
     * @param ContainerBuilder $container
164
     * @param string           $name
165
     * @param array            $keyEncryptionAlgorithm
166
     * @param array            $contentEncryptionAlgorithms
167
     * @param array            $compressionMethods
168
     * @param array            $headerCheckers
169
     * @param array            $serializers
170
     * @param bool             $is_public
171
     */
172
    public static function addJWELoader(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], array  $headerCheckers = [], array $serializers = ['jws_compact'], bool $is_public = true)
173
    {
174
        $config = [
175
            self::BUNDLE_ALIAS => [
176
                'jwe_loaders' => [
177
                    $name => [
178
                        'is_public' => $is_public,
179
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
180
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
181
                        'compression_methods' => $compressionMethods,
182
                        'header_checkers' => $headerCheckers,
183
                        'serializers' => $serializers,
184
                    ],
185
                ],
186
            ],
187
        ];
188
189
        self::updateJoseConfiguration($container, $config, 'jwe_loaders');
190
    }
191
192
    /**
193
     * @param ContainerBuilder $container
194
     * @param array            $config
195
     * @param string           $element
196
     */
197
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
198
    {
199
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
200
        if (!isset($jose_config[$element])) {
201
            $jose_config[$element] = [];
202
        }
203
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
204
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
205
    }
206
}
207