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

KeyCollector::collectJWK()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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
    /**
49
     * @param array $data
50
     */
51
    private function collectJWK(array &$data)
52
    {
53
        $data['key']['jwk'] = [];
54
        foreach ($this->jwks as $id => $jwk) {
55
            $data['key']['jwk'][$id] = [
56
                'jwk' => $jwk,
57
                'analyze' => null === $this->jwkAnalyzerManager ? [] : $this->jwkAnalyzerManager->analyze($jwk),
58
            ];
59
        }
60
    }
61
62
    /**
63
     * @param array $data
64
     */
65
    private function collectJWKSet(array &$data)
66
    {
67
        $data['key']['jwkset'] = [];
68
        foreach ($this->jwksets as $id => $jwkset) {
69
            $analyze = [];
70
            if (null !== $this->jwkAnalyzerManager) {
71
                foreach ($jwkset as $kid => $jwk) {
72
                    $analyze[$kid] = $this->jwkAnalyzerManager->analyze($jwk);
73
                }
74
            }
75
            $data['key']['jwkset'][$id] = [
76
                'jwkset' => $jwkset,
77
                'analyze' => $analyze,
78
            ];
79
        }
80
    }
81
82
    /**
83
     * @var JWK[]
84
     */
85
    private $jwks = [];
86
87
    /**
88
     * @param string $id
89
     * @param JWK    $jwk
90
     */
91
    public function addJWK(string $id, JWK $jwk)
92
    {
93
        $this->jwks[$id] = $jwk;
94
    }
95
96
    /**
97
     * @var JWKSet[]
98
     */
99
    private $jwksets = [];
100
101
    /**
102
     * @param string $id
103
     * @param JWKSet $jwkset
104
     */
105
    public function addJWKSet(string $id, JWKSet $jwkset)
106
    {
107
        $this->jwksets[$id] = $jwkset;
108
    }
109
}
110