Failed Conditions
Push — master ( 2c044d...7f3b43 )
by Florent
02:21
created

JWECollector::getJWEDecrypters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\DataCollector;
15
16
use Jose\Component\Encryption\Compression\CompressionMethodManagerFactory;
17
use Jose\Component\Encryption\JWEBuilder;
18
use Jose\Component\Encryption\JWEDecrypter;
19
use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
final class JWECollector implements Collector
24
{
25
    /**
26
     * @var JWESerializerManagerFactory|null
27
     */
28
    private $jweSerializerManagerFactory;
29
30
    /**
31
     * @var CompressionMethodManagerFactory|null
32
     */
33
    private $compressionMethodManagerFactory;
34
35
    /**
36
     * JWECollector constructor.
37
     *
38
     * @param CompressionMethodManagerFactory|null $compressionMethodManagerFactory
39
     * @param JWESerializerManagerFactory|null $jweSerializerManagerFactory
40
     */
41
    public function __construct(?CompressionMethodManagerFactory $compressionMethodManagerFactory = null, ?JWESerializerManagerFactory $jweSerializerManagerFactory = null)
42
    {
43
        $this->compressionMethodManagerFactory = $compressionMethodManagerFactory;
44
        $this->jweSerializerManagerFactory = $jweSerializerManagerFactory;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function collect(array &$data, Request $request, Response $response, \Exception $exception = null)
51
    {
52
        $this->collectSupportedCompressionMethods($data);
53
        $this->collectSupportedJWESerializations($data);
54
        $this->collectSupportedJWEBuilders($data);
55
        $this->collectSupportedJWEDecrypters($data);
56
    }
57
58
    public function name(): string
59
    {
60
        return 'jwe';
61
    }
62
63
    /**
64
     * @param array $data
65
     *
66
     * @return array
67
     */
68
    public function getCompressionMethodDetails(array $data): array
69
    {
70
        return $data['compression_methods'];
71
    }
72
73
    /**
74
     * @param array $data
75
     *
76
     * @return array
77
     */
78
    public function getJWESerializationDetails(array $data): array
79
    {
80
        return $data['jwe_serialization'];
81
    }
82
83
    /**
84
     * @param array $data
85
     *
86
     * @return array
87
     */
88
    public function getJWEBuilders(array $data): array
89
    {
90
        return $data['jwe_builders'];
91
    }
92
93
    /**
94
     * @param array $data
95
     *
96
     * @return array
97
     */
98
    public function getJWEDecrypters(array $data): array
99
    {
100
        return $data['jwe_decrypters'];
101
    }
102
103
    /**
104
     * @param array $data
105
     */
106
    private function collectSupportedCompressionMethods(array &$data)
107
    {
108
        $data['compression_methods'] = [];
109
        if (null === $this->compressionMethodManagerFactory) {
110
            return;
111
        }
112
        $compressionMethods = $this->compressionMethodManagerFactory->all();
113
        foreach ($compressionMethods as $alias => $compressionMethod) {
114
            $data['compression_methods'][$alias] = $compressionMethod->name();
115
        }
116
    }
117
118
    /**
119
     * @param array $data
120
     */
121
    private function collectSupportedJWESerializations(array &$data)
122
    {
123
        $data['jwe_serialization'] = [];
124
        if (null === $this->jweSerializerManagerFactory) {
125
            return;
126
        }
127
        $serializers = $this->jweSerializerManagerFactory->all();
128
        foreach ($serializers as $serializer) {
129
            $data['jwe_serialization'][$serializer->name()] = $serializer->displayName();
130
        }
131
    }
132
133
    /**
134
     * @param array $data
135
     */
136
    private function collectSupportedJWEBuilders(array &$data)
137
    {
138
        $data['jwe_builders'] = [];
139
        foreach ($this->jweBuilders as $id => $jweBuilder) {
140
            $data['jwe_builders'][$id] = [
141
                'key_encryption_algorithms' => $jweBuilder->getKeyEncryptionAlgorithmManager()->list(),
142
                'content_encryption_algorithms' => $jweBuilder->getContentEncryptionAlgorithmManager()->list(),
143
                'compression_methods' => $jweBuilder->getCompressionMethodManager()->list(),
144
            ];
145
        }
146
    }
147
148
    /**
149
     * @param array $data
150
     */
151
    private function collectSupportedJWEDecrypters(array &$data)
152
    {
153
        $data['jwe_decrypters'] = [];
154
        foreach ($this->jweDecrypters as $id => $jweDecrypter) {
155
            $data['jwe_decrypters'][$id] = [
156
                'key_encryption_algorithms' => $jweDecrypter->getKeyEncryptionAlgorithmManager()->list(),
157
                'content_encryption_algorithms' => $jweDecrypter->getContentEncryptionAlgorithmManager()->list(),
158
                'compression_methods' => $jweDecrypter->getCompressionMethodManager()->list(),
159
            ];
160
        }
161
    }
162
163
    /**
164
     * @var JWEBuilder[]
165
     */
166
    private $jweBuilders = [];
167
168
    /**
169
     * @param string     $id
170
     * @param JWEBuilder $jweBuilder
171
     */
172
    public function addJWEBuilder(string $id, JWEBuilder $jweBuilder)
173
    {
174
        $this->jweBuilders[$id] = $jweBuilder;
175
    }
176
177
    /**
178
     * @var JWEDecrypter[]
179
     */
180
    private $jweDecrypters = [];
181
182
    /**
183
     * @param string       $id
184
     * @param JWEDecrypter $jweDecrypter
185
     */
186
    public function addJWEDecrypter(string $id, JWEDecrypter $jweDecrypter)
187
    {
188
        $this->jweDecrypters[$id] = $jweDecrypter;
189
    }
190
}
191