Failed Conditions
Push — master ( a96c48...74e563 )
by Florent
02:28
created

JWECollector::name()   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 0
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
    /**
59
     * @param array $data
60
     */
61
    private function collectSupportedCompressionMethods(array &$data)
62
    {
63
        $data['jwe']['compression_methods'] = [];
64
        if (null === $this->compressionMethodManagerFactory) {
65
            return;
66
        }
67
        $compressionMethods = $this->compressionMethodManagerFactory->all();
68
        foreach ($compressionMethods as $alias => $compressionMethod) {
69
            $data['jwe']['compression_methods'][$alias] = $compressionMethod->name();
70
        }
71
    }
72
73
    /**
74
     * @param array $data
75
     */
76
    private function collectSupportedJWESerializations(array &$data)
77
    {
78
        $data['jwe']['jwe_serialization'] = [];
79
        if (null === $this->jweSerializerManagerFactory) {
80
            return;
81
        }
82
        $serializers = $this->jweSerializerManagerFactory->all();
83
        foreach ($serializers as $serializer) {
84
            $data['jwe']['jwe_serialization'][$serializer->name()] = $serializer->displayName();
85
        }
86
    }
87
88
    /**
89
     * @param array $data
90
     */
91
    private function collectSupportedJWEBuilders(array &$data)
92
    {
93
        $data['jwe']['jwe_builders'] = [];
94
        foreach ($this->jweBuilders as $id => $jweBuilder) {
95
            $data['jwe']['jwe_builders'][$id] = [
96
                'key_encryption_algorithms' => $jweBuilder->getKeyEncryptionAlgorithmManager()->list(),
97
                'content_encryption_algorithms' => $jweBuilder->getContentEncryptionAlgorithmManager()->list(),
98
                'compression_methods' => $jweBuilder->getCompressionMethodManager()->list(),
99
            ];
100
        }
101
    }
102
103
    /**
104
     * @param array $data
105
     */
106
    private function collectSupportedJWEDecrypters(array &$data)
107
    {
108
        $data['jwe']['jwe_decrypters'] = [];
109
        foreach ($this->jweDecrypters as $id => $jweDecrypter) {
110
            $data['jwe']['jwe_decrypters'][$id] = [
111
                'key_encryption_algorithms' => $jweDecrypter->getKeyEncryptionAlgorithmManager()->list(),
112
                'content_encryption_algorithms' => $jweDecrypter->getContentEncryptionAlgorithmManager()->list(),
113
                'compression_methods' => $jweDecrypter->getCompressionMethodManager()->list(),
114
            ];
115
        }
116
    }
117
118
    /**
119
     * @var JWEBuilder[]
120
     */
121
    private $jweBuilders = [];
122
123
    /**
124
     * @param string     $id
125
     * @param JWEBuilder $jweBuilder
126
     */
127
    public function addJWEBuilder(string $id, JWEBuilder $jweBuilder)
128
    {
129
        $this->jweBuilders[$id] = $jweBuilder;
130
    }
131
132
    /**
133
     * @var JWEDecrypter[]
134
     */
135
    private $jweDecrypters = [];
136
137
    /**
138
     * @param string       $id
139
     * @param JWEDecrypter $jweDecrypter
140
     */
141
    public function addJWEDecrypter(string $id, JWEDecrypter $jweDecrypter)
142
    {
143
        $this->jweDecrypters[$id] = $jweDecrypter;
144
    }
145
}
146