Failed Conditions
Push — master ( 5537f8...d96005 )
by Florent
02:00
created

ConfigurationHelper::addJWSVerifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 5
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 bool             $is_public
52
     */
53
    public static function addJWSVerifier(ContainerBuilder $container, string $name, array $signatureAlgorithms, array  $headerCheckers, bool $is_public = true)
54
    {
55
        $config = [
56
            self::BUNDLE_ALIAS => [
57
                'jws_verifiers' => [
58
                    $name => [
59
                        'is_public' => $is_public,
60
                        'signature_algorithms' => $signatureAlgorithms,
61
                        'header_checkers' => $headerCheckers,
62
                    ],
63
                ],
64
            ],
65
        ];
66
67
        self::updateJoseConfiguration($container, $config, 'jws_verifiers');
68
    }
69
70
    /**
71
     * @param ContainerBuilder $container
72
     * @param string           $name
73
     * @param string[]         $claimCheckers
74
     * @param bool             $is_public
75
     */
76
    public static function addClaimChecker(ContainerBuilder $container, string $name, array  $claimCheckers, bool $is_public = true)
77
    {
78
        $config = [
79
            self::BUNDLE_ALIAS => [
80
                'claim_checkers' => [
81
                    $name => [
82
                        'is_public' => $is_public,
83
                        'claims' => $claimCheckers,
84
                    ],
85
                ],
86
            ],
87
        ];
88
89
        self::updateJoseConfiguration($container, $config, 'claim_checkers');
90
    }
91
92
    /**
93
     * @param ContainerBuilder $container
94
     * @param string           $name
95
     * @param string[]         $headerCheckers
96
     * @param bool             $is_public
97
     */
98
    public static function addHeaderChecker(ContainerBuilder $container, string $name, array  $headerCheckers, bool $is_public = true)
99
    {
100
        $config = [
101
            self::BUNDLE_ALIAS => [
102
                'header_checkers' => [
103
                    $name => [
104
                        'is_public' => $is_public,
105
                        'headers' => $headerCheckers,
106
                    ],
107
                ],
108
            ],
109
        ];
110
111
        self::updateJoseConfiguration($container, $config, 'header_checkers');
112
    }
113
114
    /**
115
     * @param ContainerBuilder $container
116
     * @param string           $name
117
     * @param string           $type
118
     * @param array            $parameters
119
     */
120
    public static function addKey(ContainerBuilder $container, string $name, string $type, array  $parameters)
121
    {
122
        $config = [
123
            self::BUNDLE_ALIAS => [
124
                'keys' => [
125
                    $name => [
126
                        $type => $parameters,
127
                    ],
128
                ],
129
            ],
130
        ];
131
132
        self::updateJoseConfiguration($container, $config, 'keys');
133
    }
134
135
    /**
136
     * @param ContainerBuilder $container
137
     * @param string           $name
138
     * @param string           $type
139
     * @param array            $parameters
140
     */
141
    public static function addKeyset(ContainerBuilder $container, string $name, string $type, array  $parameters)
142
    {
143
        $config = [
144
            self::BUNDLE_ALIAS => [
145
                'key_sets' => [
146
                    $name => [
147
                        $type => $parameters,
148
                    ],
149
                ],
150
            ],
151
        ];
152
153
        self::updateJoseConfiguration($container, $config, 'key_sets');
154
    }
155
156
    /**
157
     * @param ContainerBuilder $container
158
     * @param string           $name
159
     * @param array            $keyEncryptionAlgorithm
160
     * @param array            $contentEncryptionAlgorithms
161
     * @param array            $compressionMethods
162
     * @param bool             $is_public
163
     */
164
    public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true)
165
    {
166
        $config = [
167
            self::BUNDLE_ALIAS => [
168
                'jwe_builders' => [
169
                    $name => [
170
                        'is_public' => $is_public,
171
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
172
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
173
                        'compression_methods' => $compressionMethods,
174
                    ],
175
                ],
176
            ],
177
        ];
178
179
        self::updateJoseConfiguration($container, $config, 'jwe_builders');
180
    }
181
182
    /**
183
     * @param ContainerBuilder $container
184
     * @param string           $name
185
     * @param array            $keyEncryptionAlgorithm
186
     * @param array            $contentEncryptionAlgorithms
187
     * @param array            $compressionMethods
188
     * @param array            $headerCheckers
189
     * @param bool             $is_public
190
     */
191
    public static function addJWEDecrypter(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], array  $headerCheckers = [], bool $is_public = true)
192
    {
193
        $config = [
194
            self::BUNDLE_ALIAS => [
195
                'jwe_decrypters' => [
196
                    $name => [
197
                        'is_public' => $is_public,
198
                        'key_encryption_algorithms' => $keyEncryptionAlgorithm,
199
                        'content_encryption_algorithms' => $contentEncryptionAlgorithms,
200
                        'compression_methods' => $compressionMethods,
201
                        'header_checkers' => $headerCheckers,
202
                    ],
203
                ],
204
            ],
205
        ];
206
207
        self::updateJoseConfiguration($container, $config, 'jwe_decrypters');
208
    }
209
210
    /**
211
     * @param ContainerBuilder $container
212
     * @param array            $config
213
     * @param string           $element
214
     */
215
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
216
    {
217
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
218
        if (!isset($jose_config[$element])) {
219
            $jose_config[$element] = [];
220
        }
221
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
222
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
223
    }
224
}
225