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

KeyCollector::getJWKs()   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\Core\JWK;
17
use Jose\Component\Core\JWKSet;
18
use Jose\Component\KeyManagement\KeyAnalyzer\KeyAnalyzerManager;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
final class KeyCollector implements Collector
23
{
24
    /**
25
     * @var KeyAnalyzerManager|null
26
     */
27
    private $jwkAnalyzerManager;
28
29
    /**
30
     * KeyCollector constructor.
31
     *
32
     * @param KeyAnalyzerManager|null $jwkAnalyzerManager
33
     */
34
    public function __construct(?KeyAnalyzerManager $jwkAnalyzerManager = null)
35
    {
36
        $this->jwkAnalyzerManager = $jwkAnalyzerManager;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function collect(array &$data, Request $request, Response $response, \Exception $exception = null)
43
    {
44
        $this->collectJWK($data);
45
        $this->collectJWKSet($data);
46
    }
47
48
    public function name(): string
49
    {
50
        return 'key';
51
    }
52
53
    /**
54
     * @param array $data
55
     *
56
     * @return array
57
     */
58
    public function getJWKs(array $data): array
59
    {
60
        return $data['jwk'];
61
    }
62
63
    /**
64
     * @param array $data
65
     *
66
     * @return array
67
     */
68
    public function getJWKSets(array $data): array
69
    {
70
        return $data['jwkset'];
71
    }
72
73
    /**
74
     * @param array $data
75
     */
76
    private function collectJWK(array &$data)
77
    {
78
        $data['jwk'] = [];
79
        foreach ($this->jwks as $id => $jwk) {
80
            $data['jwk'][$id] = [
81
                'jwk' => $jwk,
82
                'analyze' => null === $this->jwkAnalyzerManager ? [] : $this->jwkAnalyzerManager->analyze($jwk),
83
            ];
84
        }
85
    }
86
87
    /**
88
     * @param array $data
89
     */
90
    private function collectJWKSet(array &$data)
91
    {
92
        $data['jwkset'] = [];
93
        foreach ($this->jwksets as $id => $jwkset) {
94
            $analyze = [];
95
            if (null !== $this->jwkAnalyzerManager) {
96
                foreach ($jwkset as $kid => $jwk) {
97
                    $analyze[$kid] = $this->jwkAnalyzerManager->analyze($jwk);
98
                }
99
            }
100
            $data['jwkset'][$id] = [
101
                'jwkset' => $jwkset,
102
                'analyze' => $analyze,
103
            ];
104
        }
105
    }
106
107
    /**
108
     * @var JWK[]
109
     */
110
    private $jwks = [];
111
112
    /**
113
     * @param string $id
114
     * @param JWK    $jwk
115
     */
116
    public function addJWK(string $id, JWK $jwk)
117
    {
118
        $this->jwks[$id] = $jwk;
119
    }
120
121
    /**
122
     * @var JWKSet[]
123
     */
124
    private $jwksets = [];
125
126
    /**
127
     * @param string $id
128
     * @param JWKSet $jwkset
129
     */
130
    public function addJWKSet(string $id, JWKSet $jwkset)
131
    {
132
        $this->jwksets[$id] = $jwkset;
133
    }
134
}
135