KeyCollector   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 79
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A collect() 0 5 1
A addJWK() 0 4 1
A addJWKSet() 0 4 1
A collectJWK() 0 11 3
A collectJWKSet() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\Analyzer\KeyAnalyzerManager;
19
use Jose\Component\KeyManagement\Analyzer\KeysetAnalyzerManager;
20
use Jose\Component\KeyManagement\Analyzer\MessageBag;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\VarDumper\Cloner\VarCloner;
24
25
class KeyCollector implements Collector
26
{
27
    /**
28
     * @var null|KeyAnalyzerManager
29
     */
30
    private $jwkAnalyzerManager;
31
32
    /**
33
     * @var null|KeysetAnalyzerManager
34
     */
35
    private $jwksetAnalyzerManager;
36
37
    /**
38
     * @var JWK[]
39
     */
40
    private $jwks = [];
41
42
    /**
43
     * @var JWKSet[]
44
     */
45
    private $jwksets = [];
46
47
    public function __construct(?KeyAnalyzerManager $jwkAnalyzerManager = null, ?KeysetAnalyzerManager $jwksetAnalyzerManager = null)
48
    {
49
        $this->jwkAnalyzerManager = $jwkAnalyzerManager;
50
        $this->jwksetAnalyzerManager = $jwksetAnalyzerManager;
51
    }
52
53
    public function collect(array &$data, Request $request, Response $response, ?\Exception $exception = null): void
54
    {
55
        $this->collectJWK($data);
56
        $this->collectJWKSet($data);
57
    }
58
59
    public function addJWK(string $id, JWK $jwk): void
60
    {
61
        $this->jwks[$id] = $jwk;
62
    }
63
64
    public function addJWKSet(string $id, JWKSet $jwkset): void
65
    {
66
        $this->jwksets[$id] = $jwkset;
67
    }
68
69
    private function collectJWK(array &$data): void
70
    {
71
        $cloner = new VarCloner();
72
        $data['key']['jwk'] = [];
73
        foreach ($this->jwks as $id => $jwk) {
74
            $data['key']['jwk'][$id] = [
75
                'jwk' => $cloner->cloneVar($jwk),
76
                'analyze' => null === $this->jwkAnalyzerManager ? [] : $this->jwkAnalyzerManager->analyze($jwk),
77
            ];
78
        }
79
    }
80
81
    private function collectJWKSet(array &$data): void
82
    {
83
        $cloner = new VarCloner();
84
        $data['key']['jwkset'] = [];
85
        foreach ($this->jwksets as $id => $jwkset) {
86
            $analyze = [];
87
            $analyzeJWKSet = new MessageBag();
88
            if (null !== $this->jwkAnalyzerManager) {
89
                foreach ($jwkset as $kid => $jwk) {
90
                    $analyze[$kid] = $this->jwkAnalyzerManager->analyze($jwk);
91
                }
92
            }
93
            if (null !== $this->jwksetAnalyzerManager) {
94
                $analyzeJWKSet = $this->jwksetAnalyzerManager->analyze($jwkset);
95
            }
96
            $data['key']['jwkset'][$id] = [
97
                'jwkset' => $cloner->cloneVar($jwkset),
98
                'analyze' => $analyze,
99
                'analyze_jwkset' => $analyzeJWKSet,
100
            ];
101
        }
102
    }
103
}
104