Failed Conditions
Push — master ( 7c31d6...876c49 )
by Florent
01:53
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[]         $headerCheckers
98
     * @param bool             $is_public
99
     */
100
    public static function addHeaderChecker(ContainerBuilder $container, string $name, array  $headerCheckers, bool $is_public = true)
101
    {
102
        $config = [
103
            self::BUNDLE_ALIAS => [
104
                'header_checkers' => [
105
                    $name => [
106
                        'is_public' => $is_public,
107
                        'headers' => $headerCheckers,
108
                    ],
109
                ],
110
            ],
111
        ];
112
113
        self::updateJoseConfiguration($container, $config, 'header_checkers');
114
    }
115
116
    /**
117
     * @param ContainerBuilder $container
118
     * @param string           $name
119
     * @param string           $type
120
     * @param array            $parameters
121
     */
122
    public static function addKey(ContainerBuilder $container, string $name, string $type, array  $parameters)
123
    {
124
        $config = [
125
            self::BUNDLE_ALIAS => [
126
                'keys' => [
127
                    $name => [
128
                        $type => $parameters,
129
                    ],
130
                ],
131
            ],
132
        ];
133
134
        self::updateJoseConfiguration($container, $config, 'keys');
135
    }
136
137
    /**
138
     * @param ContainerBuilder $container
139
     * @param string           $name
140
     * @param string           $type
141
     * @param array            $parameters
142
     */
143
    public static function addKeyset(ContainerBuilder $container, string $name, string $type, array  $parameters)
144
    {
145
        $config = [
146
            self::BUNDLE_ALIAS => [
147
                'key_sets' => [
148
                    $name => [
149
                        $type => $parameters,
150
                    ],
151
                ],
152
            ],
153
        ];
154
155
        self::updateJoseConfiguration($container, $config, 'key_sets');
156
    }
157
158
    /**
159
     * @param ContainerBuilder $container
160
     * @param string           $name
161
     * @param array            $keyEncryptionAlgorithm
162
     * @param array            $contentEncryptionAlgorithms
163
     * @param array            $compressionMethods
164
     * @param bool             $is_public
165
     */
166
    public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true)
167
    {
168
        $config = [
169
            self::BUNDLE_ALIAS => [
170
                'jwe_builders' => [
171
                    $name => [
172
                        'is_public' => $is_public,
173
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
174
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
175
                        'compression_methods' => $compressionMethods,
176
                    ],
177
                ],
178
            ],
179
        ];
180
181
        self::updateJoseConfiguration($container, $config, 'jwe_builders');
182
    }
183
184
    /**
185
     * @param ContainerBuilder $container
186
     * @param string           $name
187
     * @param array            $keyEncryptionAlgorithm
188
     * @param array            $contentEncryptionAlgorithms
189
     * @param array            $compressionMethods
190
     * @param array            $headerCheckers
191
     * @param array            $serializers
192
     * @param bool             $is_public
193
     */
194
    public static function addJWELoader(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], array  $headerCheckers = [], array $serializers = ['jwe_compact'], bool $is_public = true)
195
    {
196
        $config = [
197
            self::BUNDLE_ALIAS => [
198
                'jwe_loaders' => [
199
                    $name => [
200
                        'is_public' => $is_public,
201
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
202
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
203
                        'compression_methods' => $compressionMethods,
204
                        'header_checkers' => $headerCheckers,
205
                        'serializers' => $serializers,
206
                    ],
207
                ],
208
            ],
209
        ];
210
211
        self::updateJoseConfiguration($container, $config, 'jwe_loaders');
212
    }
213
214
    /**
215
     * @param ContainerBuilder $container
216
     * @param array            $config
217
     * @param string           $element
218
     */
219
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
220
    {
221
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
222
        if (!isset($jose_config[$element])) {
223
            $jose_config[$element] = [];
224
        }
225
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
226
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
227
    }
228
}
229