Completed
Push — master ( 8b6623...b6ba4a )
by Florent
08:35
created

EasyJWTCreatorSource   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 2 Features 1
Metric Value
wmc 22
c 3
b 2
f 1
lcom 1
cbo 2
dl 0
loc 174
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A createService() 0 3 1
B getNodeDefinition() 0 35 1
A prepend() 0 12 3
A createServiceConfiguration() 0 8 1
A createSignerServiceConfiguration() 0 12 3
A createEncrypterServiceConfiguration() 0 17 4
A createJWTCreatorServiceConfiguration() 0 15 3
B isEncryptionSupportEnabled() 0 12 5
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\DependencyInjection\Source;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
final class EasyJWTCreatorSource implements SourceInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getName()
23
    {
24
        return 'easy_jwt_creator';
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createService($name, array $config, ContainerBuilder $container)
31
    {
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getNodeDefinition(ArrayNodeDefinition $node)
38
    {
39
        $node
40
            ->children()
41
                ->arrayNode('easy_jwt_creator')
42
                    ->useAttributeAsKey('name')
43
                    ->prototype('array')
44
                        ->children()
45
                            ->arrayNode('signature_algorithms')
46
                                ->useAttributeAsKey('name')
47
                                ->isRequired()
48
                                ->cannotBeEmpty()
49
                                ->prototype('scalar')->end()
50
                            ->end()
51
                            ->arrayNode('key_encryption_algorithms')
52
                                ->useAttributeAsKey('name')
53
                                ->defaultValue([])
54
                                ->prototype('scalar')->end()
55
                            ->end()
56
                            ->arrayNode('content_encryption_algorithms')
57
                                ->useAttributeAsKey('name')
58
                                ->defaultValue([])
59
                                ->prototype('scalar')->end()
60
                            ->end()
61
                            ->arrayNode('compression_methods')
62
                                ->useAttributeAsKey('name')
63
                                ->defaultValue(['DEF'])
64
                                ->prototype('scalar')->end()
65
                            ->end()
66
                            ->scalarNode('logger')->defaultNull()->end()
67
                        ->end()
68
                    ->end()
69
                ->end()
70
            ->end();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function prepend(ContainerBuilder $container, array $config)
77
    {
78
        if (false === array_key_exists($this->getName(), $config)) {
79
            return;
80
        }
81
82
        foreach ($config[$this->getName()] as $id => $section) {
83
            $config = $this->createServiceConfiguration($config, $id, $section);
84
        }
85
86
        return $config;
87
    }
88
89
    /**
90
     * @param array  $config
91
     * @param string $id
92
     * @param array  $section
93
     *
94
     * @return array
95
     */
96
    private function createServiceConfiguration(array $config, $id, array $section)
97
    {
98
        $config = $this->createSignerServiceConfiguration($config, $id, $section);
99
        $config = $this->createEncrypterServiceConfiguration($config, $id, $section);
100
        $config = $this->createJWTCreatorServiceConfiguration($config, $id, $section);
101
102
        return $config;
103
    }
104
105
    /**
106
     * @param array  $config
107
     * @param string $id
108
     * @param array  $section
109
     *
110
     * @return array
111
     */
112
    private function createSignerServiceConfiguration(array $config, $id, array $section)
113
    {
114
        $config['signers'] = array_merge(
115
            array_key_exists('signers', $config) ? $config['signers'] : [],
116
            [$id => [
117
                'algorithms' => $section['signature_algorithms'],
118
                'logger'     => array_key_exists('logger', $section) ? $section['logger'] : null,
119
            ]]
120
        );
121
122
        return $config;
123
    }
124
125
    /**
126
     * @param array  $config
127
     * @param string $id
128
     * @param array  $section
129
     *
130
     * @return array
131
     */
132
    private function createEncrypterServiceConfiguration(array $config, $id, array $section)
133
    {
134
        if (false === $this->isEncryptionSupportEnabled($section)) {
135
            return $config;
136
        }
137
        $config['encrypters'] = array_merge(
138
            array_key_exists('encrypters', $config) ? $config['encrypters'] : [],
139
            [$id => [
140
                'key_encryption_algorithms'     => $section['key_encryption_algorithms'],
141
                'content_encryption_algorithms' => $section['content_encryption_algorithms'],
142
                'compression_methods'           => $section['compression_methods'],
143
                'logger'                        => array_key_exists('logger', $section) ? $section['logger'] : null,
144
            ]]
145
        );
146
147
        return $config;
148
    }
149
150
    /**
151
     * @param array  $config
152
     * @param string $id
153
     * @param array  $section
154
     *
155
     * @return array
156
     */
157
    private function createJWTCreatorServiceConfiguration(array $config, $id, array $section)
158
    {
159
        $service = [
160
            'signer' => sprintf('jose.signer.%s', $id),
161
        ];
162
        if (true === $this->isEncryptionSupportEnabled($section)) {
163
            $service['encrypter'] = sprintf('jose.encrypter.%s', $id);
164
        }
165
        $config['jwt_creators'] = array_merge(
166
            array_key_exists('jwt_creators', $config) ? $config['jwt_creators'] : [],
167
            [$id => $service]
168
        );
169
170
        return $config;
171
    }
172
173
    /**
174
     * @param array $section
175
     *
176
     * @return bool
177
     */
178
    private function isEncryptionSupportEnabled(array $section)
179
    {
180
        if (true === empty($section['key_encryption_algorithms']) && true === empty($section['content_encryption_algorithms'])) {
181
            return false;
182
        }
183
184
        if (true === empty($section['key_encryption_algorithms']) || true === empty($section['content_encryption_algorithms'])) {
185
            throw new \LogicException('Both key encryption algorithms and content encryption algorithms must be set to enable the encryption support.');
186
        }
187
188
        return true;
189
    }
190
}
191