Failed Conditions
Push — v7 ( 4dd04a...0b17f0 )
by Florent
01:58
created

JoseCollector::countKeyEncryptionAlgorithms()   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\Core\AlgorithmInterface;
17
use Jose\Component\Core\AlgorithmManagerFactory;
18
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithmInterface;
19
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithmInterface;
20
use Jose\Component\Encryption\Compression\CompressionMethodManagerFactory;
21
use Jose\Component\Signature\Algorithm\SignatureAlgorithmInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
25
26
final class JoseCollector extends DataCollector
27
{
28
    /**
29
     * @var AlgorithmManagerFactory
30
     */
31
    private $algorithmManagerFactory;
32
33
    /**
34
     * @var CompressionMethodManagerFactory
35
     */
36
    private $compressionMethodManagerFactory;
37
38
    /**
39
     * JoseCollector constructor.
40
     *
41
     * @param AlgorithmManagerFactory $algorithmManagerFactory
42
     * @param CompressionMethodManagerFactory $compressionMethodManagerFactory
43
     */
44
    public function __construct(AlgorithmManagerFactory $algorithmManagerFactory, CompressionMethodManagerFactory $compressionMethodManagerFactory)
45
    {
46
        $this->algorithmManagerFactory = $algorithmManagerFactory;
47
        $this->compressionMethodManagerFactory = $compressionMethodManagerFactory;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function collect(Request $request, Response $response, \Exception $exception = null)
54
    {
55
        $aliases = $this->algorithmManagerFactory->aliases();
56
        $algorithmManager = $this->algorithmManagerFactory->create($aliases);
57
        $this->data = ['algorithms' => []];
58
        $signatureAlgorithms = 0;
59
        $keyEncryptionAlgorithms = 0;
60
        $contentEncryptionAlgorithms = 0;
61
        foreach ($algorithmManager->list() as $alias) {
62
            $algorithm = $algorithmManager->get($alias);
63
            $type = $this->getAlgorithmType($algorithm, $signatureAlgorithms, $keyEncryptionAlgorithms, $contentEncryptionAlgorithms);
64
            if (!array_key_exists($type, $this->data['algorithms'])) {
65
                $this->data['algorithms'][$type] = [];
66
            }
67
            $this->data['algorithms'][$type][$alias] = [
68
                'name' => $algorithm->name(),
69
            ];
70
        }
71
72
        $this->data['types'] = [
73
            'signature' => $signatureAlgorithms,
74
            'key_encryption' => $keyEncryptionAlgorithms,
75
            'content_encryption' => $contentEncryptionAlgorithms,
76
        ];
77
78
79
        $aliases = $this->compressionMethodManagerFactory->aliases();
0 ignored issues
show
Bug introduced by
The method aliases() does not seem to exist on object<Jose\Component\En...onMethodManagerFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
        $cmm = $this->compressionMethodManagerFactory->create($aliases);
81
        dump($cmm);
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getAlgorithmDetails(): array
88
    {
89
        return $this->data['algorithms'];
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function countSignatureAlgorithms(): int
96
    {
97
        return $this->data['types']['signature'];
98
    }
99
100
    /**
101
     * @return int
102
     */
103
    public function countKeyEncryptionAlgorithms(): int
104
    {
105
        return $this->data['types']['key_encryption'];
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function countContentEncryptionAlgorithms(): int
112
    {
113
        return $this->data['types']['content_encryption'];
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getName()
120
    {
121
        return 'jose_collector';
122
    }
123
124
    private function getAlgorithmType(AlgorithmInterface $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string
125
    {
126
        switch (true) {
127
            case $algorithm instanceof SignatureAlgorithmInterface:
128
                $signatureAlgorithms++;
129
130
                return 'Signature';
131
            case $algorithm instanceof KeyEncryptionAlgorithmInterface:
132
                $keyEncryptionAlgorithms++;
133
134
                return 'Key Encryption';
135
            case $algorithm instanceof ContentEncryptionAlgorithmInterface:
136
                $contentEncryptionAlgorithms++;
137
138
                return 'Content Encryption';
139
            default:
140
                return 'Unknown';
141
        }
142
    }
143
}
144