Failed Conditions
Push — master ( df44e5...b25e13 )
by Florent
02:17
created

ConfigurationHelper::updateJoseConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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
     * @param array            $tags
31
     */
32
    public static function addJWSBuilder(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
33
    {
34
        $config = [
35
            self::BUNDLE_ALIAS => [
36
                'jws' => [
37
                    'builders' => [
38
                        $name => [
39
                            'is_public' => $is_public,
40
                            'signature_algorithms' => $signatureAlgorithms,
41
                            'tags' => $tags,
42
                        ],
43
                    ],
44
                ],
45
            ],
46
        ];
47
        self::updateJoseConfiguration($container, $config, 'jws');
48
    }
49
50
    /**
51
     * @param ContainerBuilder $container
52
     * @param string           $name
53
     * @param string[]         $signatureAlgorithms
54
     * @param bool             $is_public
55
     * @param array            $tags
56
     */
57
    public static function addJWSVerifier(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
58
    {
59
        $config = [
60
            self::BUNDLE_ALIAS => [
61
                'jws' => [
62
                    'verifiers' => [
63
                        $name => [
64
                            'is_public' => $is_public,
65
                            'signature_algorithms' => $signatureAlgorithms,
66
                            'tags' => $tags,
67
                        ],
68
                    ],
69
                ],
70
            ],
71
        ];
72
73
        self::updateJoseConfiguration($container, $config, 'jws');
74
    }
75
76
    /**
77
     * @param ContainerBuilder $container
78
     * @param string           $name
79
     * @param string[]         $serializers
80
     * @param bool             $is_public
81
     * @param array            $tags
82
     */
83
    public static function addJWSSerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = [])
84
    {
85
        $config = [
86
            self::BUNDLE_ALIAS => [
87
                'jws' => [
88
                    'serializers' => [
89
                        $name => [
90
                            'is_public' => $is_public,
91
                            'serializers' => $serializers,
92
                            'tags' => $tags,
93
                        ],
94
                    ],
95
                ],
96
            ],
97
        ];
98
99
        self::updateJoseConfiguration($container, $config, 'jws');
100
    }
101
102
    /**
103
     * @param ContainerBuilder $container
104
     * @param string           $name
105
     * @param string[]         $serializers
106
     * @param string[]         $signature_algorithms
107
     * @param string[]         $header_checkers
108
     * @param bool             $is_public
109
     * @param array            $tags
110
     */
111
    public static function addJWSLoader(ContainerBuilder $container, string $name, array $serializers, array $signature_algorithms, array $header_checkers, bool $is_public = true, array $tags = [])
112
    {
113
        $config = [
114
            self::BUNDLE_ALIAS => [
115
                'jws' => [
116
                    'loaders' => [
117
                        $name => [
118
                            'is_public' => $is_public,
119
                            'serializers' => $serializers,
120
                            'signature_algorithms' => $signature_algorithms,
121
                            'header_checkers' => $header_checkers,
122
                            'tags' => $tags,
123
                        ],
124
                    ],
125
                ],
126
            ],
127
        ];
128
129
        self::updateJoseConfiguration($container, $config, 'jws');
130
    }
131
132
    /**
133
     * @param ContainerBuilder $container
134
     * @param string           $name
135
     * @param string[]         $serializers
136
     * @param bool             $is_public
137
     * @param array            $tags
138
     */
139
    public static function addJWESerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = [])
140
    {
141
        $config = [
142
            self::BUNDLE_ALIAS => [
143
                'jwe' => [
144
                    'serializers' => [
145
                        $name => [
146
                            'is_public' => $is_public,
147
                            'serializers' => $serializers,
148
                            'tags' => $tags,
149
                        ],
150
                    ],
151
                ],
152
            ],
153
        ];
154
155
        self::updateJoseConfiguration($container, $config, 'jwe');
156
    }
157
158
    /**
159
     * @param ContainerBuilder $container
160
     * @param string           $name
161
     * @param string[]         $serializers
162
     * @param string[]         $key_encryption_algorithms
163
     * @param string[]         $content_encryption_algorithms
164
     * @param string[]         $compression_methods
165
     * @param string[]         $header_checkers
166
     * @param bool             $is_public
167
     * @param array            $tags
168
     */
169
    public static function addJWELoader(ContainerBuilder $container, string $name, array $serializers, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods, array $header_checkers, bool $is_public = true, array $tags = [])
170
    {
171
        $config = [
172
            self::BUNDLE_ALIAS => [
173
                'jwe' => [
174
                    'loaders' => [
175
                        $name => [
176
                            'is_public' => $is_public,
177
                            'serializers' => $serializers,
178
                            'key_encryption_algorithms' => $key_encryption_algorithms,
179
                            'content_encryption_algorithms' => $content_encryption_algorithms,
180
                            'compression_methods' => $compression_methods,
181
                            'header_checkers' => $header_checkers,
182
                            'tags' => $tags,
183
                        ],
184
                    ],
185
                ],
186
            ],
187
        ];
188
189
        self::updateJoseConfiguration($container, $config, 'jwe');
190
    }
191
192
    /**
193
     * @param ContainerBuilder $container
194
     * @param string           $name
195
     * @param string[]         $claimCheckers
196
     * @param bool             $is_public
197
     * @param array            $tags
198
     */
199
    public static function addClaimChecker(ContainerBuilder $container, string $name, array  $claimCheckers, bool $is_public = true, array $tags = [])
200
    {
201
        $config = [
202
            self::BUNDLE_ALIAS => [
203
                'checkers' => [
204
                    'claims' => [
205
                        $name => [
206
                            'is_public' => $is_public,
207
                            'claims' => $claimCheckers,
208
                            'tags' => $tags,
209
                        ],
210
                    ],
211
                ],
212
            ],
213
        ];
214
215
        self::updateJoseConfiguration($container, $config, 'checkers');
216
    }
217
218
    /**
219
     * @param ContainerBuilder $container
220
     * @param string           $name
221
     * @param string[]         $headerCheckers
222
     * @param bool             $is_public
223
     * @param array            $tags
224
     */
225
    public static function addHeaderChecker(ContainerBuilder $container, string $name, array  $headerCheckers, bool $is_public = true, array $tags = [])
226
    {
227
        $config = [
228
            self::BUNDLE_ALIAS => [
229
                'checkers' => [
230
                    'headers' => [
231
                        $name => [
232
                            'is_public' => $is_public,
233
                            'headers' => $headerCheckers,
234
                            'tags' => $tags,
235
                        ],
236
                    ],
237
                ],
238
            ],
239
        ];
240
241
        self::updateJoseConfiguration($container, $config, 'checkers');
242
    }
243
244
    /**
245
     * @param ContainerBuilder $container
246
     * @param string           $name
247
     * @param string           $type
248
     * @param array            $parameters
249
     * @param array            $tags
250
     */
251
    public static function addKey(ContainerBuilder $container, string $name, string $type, array  $parameters, array $tags = [])
252
    {
253
        $parameters['tags'] = $tags;
254
        $config = [
255
            self::BUNDLE_ALIAS => [
256
                'keys' => [
257
                    $name => [
258
                        $type => $parameters,
259
                    ],
260
                ],
261
            ],
262
        ];
263
264
        self::updateJoseConfiguration($container, $config, 'keys');
265
    }
266
267
    /**
268
     * @param ContainerBuilder $container
269
     * @param string           $name
270
     * @param string           $type
271
     * @param array            $parameters
272
     * @param array            $tags
273
     */
274
    public static function addKeyset(ContainerBuilder $container, string $name, string $type, array  $parameters, array $tags = [])
275
    {
276
        $parameters['tags'] = $tags;
277
        $config = [
278
            self::BUNDLE_ALIAS => [
279
                'key_sets' => [
280
                    $name => [
281
                        $type => $parameters,
282
                    ],
283
                ],
284
            ],
285
        ];
286
287
        self::updateJoseConfiguration($container, $config, 'key_sets');
288
    }
289
290
    /**
291
     * @param ContainerBuilder $container
292
     * @param string           $name
293
     * @param array            $parameters
294
     * @param array            $tags
295
     */
296
    public static function addKeyUri(ContainerBuilder $container, string $name, array $parameters, array $tags = [])
297
    {
298
        $parameters['tags'] = $tags;
299
        $config = [
300
            self::BUNDLE_ALIAS => [
301
                'jwk_uris' => [
302
                    $name => $parameters,
303
                ],
304
            ],
305
        ];
306
307
        self::updateJoseConfiguration($container, $config, 'jwk_uris');
308
    }
309
310
    /**
311
     * @param ContainerBuilder $container
312
     * @param string           $name
313
     * @param array            $keyEncryptionAlgorithm
314
     * @param array            $contentEncryptionAlgorithms
315
     * @param array            $compressionMethods
316
     * @param bool             $is_public
317
     * @param array            $tags
318
     */
319
    public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = [])
320
    {
321
        $config = [
322
            self::BUNDLE_ALIAS => [
323
                'jwe' => [
324
                    'builders' => [
325
                        $name => [
326
                            'is_public' => $is_public,
327
                            'key_encryption_algorithms' => $keyEncryptionAlgorithm,
328
                            'content_encryption_algorithms' => $contentEncryptionAlgorithms,
329
                            'compression_methods' => $compressionMethods,
330
                            'tags' => $tags,
331
                        ],
332
                    ],
333
                ],
334
            ],
335
        ];
336
337
        self::updateJoseConfiguration($container, $config, 'jwe');
338
    }
339
340
    /**
341
     * @param ContainerBuilder $container
342
     * @param string           $name
343
     * @param array            $keyEncryptionAlgorithm
344
     * @param array            $contentEncryptionAlgorithms
345
     * @param array            $compressionMethods
346
     * @param bool             $is_public
347
     * @param array            $tags
348
     */
349
    public static function addJWEDecrypter(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = [])
350
    {
351
        $config = [
352
            self::BUNDLE_ALIAS => [
353
                'jwe' => [
354
                    'decrypters' => [
355
                        $name => [
356
                            'is_public' => $is_public,
357
                            'key_encryption_algorithms' => $keyEncryptionAlgorithm,
358
                            'content_encryption_algorithms' => $contentEncryptionAlgorithms,
359
                            'compression_methods' => $compressionMethods,
360
                            'tags' => $tags,
361
                        ],
362
                    ],
363
                ],
364
            ],
365
        ];
366
367
        self::updateJoseConfiguration($container, $config, 'jwe');
368
    }
369
370
    /**
371
     * @param ContainerBuilder $container
372
     * @param array            $config
373
     * @param string           $element
374
     */
375
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
376
    {
377
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
378
        if (!isset($jose_config[$element])) {
379
            $jose_config[$element] = [];
380
        }
381
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
382
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
383
    }
384
}
385