Completed
Push — master ( a6e833...2ffd36 )
by Florent
02:08
created

createEncrypterServiceConfiguration()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 2
nop 3
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')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end()
46
                            ->arrayNode('key_encryption_algorithms')->defaultValue([])->prototype('scalar')->end()->end()
47
                            ->arrayNode('content_encryption_algorithms')->defaultValue([])->prototype('scalar')->end()->end()
48
                            ->arrayNode('compression_methods')->defaultValue(['DEF'])->prototype('scalar')->end()->end()
49
                            ->scalarNode('logger')->defaultNull()->end()
50
                        ->end()
51
                    ->end()
52
                ->end()
53
            ->end();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function prepend(ContainerBuilder $container, array $config)
60
    {
61
        if (false === array_key_exists($this->getName(), $config)) {
62
            return;
63
        }
64
65
        foreach ($config[$this->getName()] as $id=>$section) {
66
            $config = $this->createServiceConfiguration($config, $id, $section);
67
        }
68
69
        return $config;
70
    }
71
72
    /**
73
     * @param array  $config
74
     * @param string $id
75
     * @param array  $section
76
     *
77
     * @return array
78
     */
79
    private function createServiceConfiguration(array $config, $id, array $section)
80
    {
81
        $config = $this->createSignerServiceConfiguration($config, $id, $section);
82
        $config = $this->createEncrypterServiceConfiguration($config, $id, $section);
83
        $config = $this->createJWTCreatorServiceConfiguration($config, $id, $section);
84
85
        return $config;
86
    }
87
88
    /**
89
     * @param array  $config
90
     * @param string $id
91
     * @param array  $section
92
     *
93
     * @return array
94
     */
95
    private function createSignerServiceConfiguration(array $config, $id, array $section)
96
    {
97
        $config['signers'] = array_merge(
98
            array_key_exists('signers', $config) ? $config['signers'] : [],
99
            [$id => [
100
                'algorithms' => $section['signature_algorithms'],
101
                'logger' => array_key_exists('logger', $section) ? $section['logger'] : null,
102
            ]]
103
        );
104
105
        return $config;
106
    }
107
108
    /**
109
     * @param array  $config
110
     * @param string $id
111
     * @param array  $section
112
     *
113
     * @return array
114
     */
115
    private function createEncrypterServiceConfiguration(array $config, $id, array $section)
116
    {
117
        if (false === $this->isEncryptionSupportEnabled($section)) {
118
            return $config;
119
        }
120
        $config['encrypters'] = array_merge(
121
            array_key_exists('encrypters', $config) ? $config['encrypters'] : [],
122
            [$id => [
123
                'key_encryption_algorithms' => $section['key_encryption_algorithms'],
124
                'content_encryption_algorithms' => $section['content_encryption_algorithms'],
125
                'compression_methods' => $section['compression_methods'],
126
                'logger' => array_key_exists('logger', $section) ? $section['logger'] : null,
127
            ]]
128
        );
129
130
        return $config;
131
    }
132
133
    /**
134
     * @param array  $config
135
     * @param string $id
136
     * @param array  $section
137
     *
138
     * @return array
139
     */
140
    private function createJWTCreatorServiceConfiguration(array $config, $id, array $section)
141
    {
142
        $service = [
143
            'signer' => sprintf('jose.signer.%s', $id),
144
        ];
145
        if (true === $this->isEncryptionSupportEnabled($section)) {
146
            $service['encrypter'] = sprintf('jose.encrypter.%s', $id);
147
        }
148
        $config['jwt_creators'] = array_merge(
149
            array_key_exists('jwt_creators', $config) ? $config['jwt_creators'] : [],
150
            [$id => $service]
151
        );
152
153
        return $config;
154
    }
155
156
    /**
157
     * @param array $section
158
     *
159
     * @return bool
160
     */
161
    private function isEncryptionSupportEnabled(array $section)
162
    {
163
        if (true === empty($section['key_encryption_algorithms']) && true === empty($section['content_encryption_algorithms'])) {
164
            return false;
165
        }
166
167
        if (true === empty($section['key_encryption_algorithms']) || true === empty($section['content_encryption_algorithms'])) {
168
            throw new \LogicException('Both key encryption algorithms and content encryption algorithms must be set to enable the encryption support.');
169
        }
170
171
        return true;
172
    }
173
}
174