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

AlgorithmCollector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 122
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collect() 0 4 1
A name() 0 4 1
A getAlgorithmDetails() 0 4 1
A countSignatureAlgorithms() 0 4 1
A countKeyEncryptionAlgorithms() 0 4 1
A countContentEncryptionAlgorithms() 0 4 1
A collectSupportedAlgorithms() 0 23 3
A getAlgorithmType() 0 19 4
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\Algorithm;
17
use Jose\Component\Core\AlgorithmManagerFactory;
18
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
19
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithm;
20
use Jose\Component\Signature\Algorithm\SignatureAlgorithm;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
final class AlgorithmCollector implements Collector
25
{
26
    /**
27
     * @var AlgorithmManagerFactory
28
     */
29
    private $algorithmManagerFactory;
30
31
    /**
32
     * AlgorithmCollector constructor.
33
     *
34
     * @param AlgorithmManagerFactory $algorithmManagerFactory
35
     */
36
    public function __construct(AlgorithmManagerFactory $algorithmManagerFactory)
37
    {
38
        $this->algorithmManagerFactory = $algorithmManagerFactory;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function collect(array &$data, Request $request, Response $response, \Exception $exception = null)
45
    {
46
        $this->collectSupportedAlgorithms($data);
47
    }
48
49
    public function name(): string
50
    {
51
        return 'algorithm';
52
    }
53
54
    /**
55
     * @param array $data
56
     *
57
     * @return array
58
     */
59
    public function getAlgorithmDetails(array $data): array
60
    {
61
        return $data['algorithms'];
62
    }
63
64
    /**
65
     * @param array $data
66
     *
67
     * @return int
68
     */
69
    public function countSignatureAlgorithms(array $data): int
70
    {
71
        return $data['types']['signature'];
72
    }
73
74
    /**
75
     * @param array $data
76
     *
77
     * @return int
78
     */
79
    public function countKeyEncryptionAlgorithms(array $data): int
80
    {
81
        return $data['types']['key_encryption'];
82
    }
83
84
    /**
85
     * @param array $data
86
     *
87
     * @return int
88
     */
89
    public function countContentEncryptionAlgorithms(array $data): int
90
    {
91
        return $data['types']['content_encryption'];
92
    }
93
94
    private function collectSupportedAlgorithms(array &$data)
95
    {
96
        $algorithms = $this->algorithmManagerFactory->all();
97
        $data['algorithms'] = [];
98
        $signatureAlgorithms = 0;
99
        $keyEncryptionAlgorithms = 0;
100
        $contentEncryptionAlgorithms = 0;
101
        foreach ($algorithms as $alias => $algorithm) {
102
            $type = $this->getAlgorithmType($algorithm, $signatureAlgorithms, $keyEncryptionAlgorithms, $contentEncryptionAlgorithms);
103
            if (!array_key_exists($type, $data['algorithms'])) {
104
                $data['algorithms'][$type] = [];
105
            }
106
            $data['algorithms'][$type][$alias] = [
107
                'name' => $algorithm->name(),
108
            ];
109
        }
110
111
        $data['types'] = [
112
            'signature' => $signatureAlgorithms,
113
            'key_encryption' => $keyEncryptionAlgorithms,
114
            'content_encryption' => $contentEncryptionAlgorithms,
115
        ];
116
    }
117
118
    /**
119
     * @param Algorithm $algorithm
120
     * @param int       $signatureAlgorithms
121
     * @param int       $keyEncryptionAlgorithms
122
     * @param int       $contentEncryptionAlgorithms
123
     *
124
     * @return string
125
     */
126
    private function getAlgorithmType(Algorithm $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string
127
    {
128
        switch (true) {
129
            case $algorithm instanceof SignatureAlgorithm:
130
                $signatureAlgorithms++;
131
132
                return 'Signature';
133
            case $algorithm instanceof KeyEncryptionAlgorithm:
134
                $keyEncryptionAlgorithms++;
135
136
                return 'Key Encryption';
137
            case $algorithm instanceof ContentEncryptionAlgorithm:
138
                $contentEncryptionAlgorithms++;
139
140
                return 'Content Encryption';
141
            default:
142
                return 'Unknown';
143
        }
144
    }
145
}
146