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

AlgorithmCollector::getAlgorithmDetails()   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\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
        $algorithms = $this->algorithmManagerFactory->all();
47
        $data['algorithm']['algorithms'] = [];
48
        $signatureAlgorithms = 0;
49
        $keyEncryptionAlgorithms = 0;
50
        $contentEncryptionAlgorithms = 0;
51
        foreach ($algorithms as $alias => $algorithm) {
52
            $type = $this->getAlgorithmType($algorithm, $signatureAlgorithms, $keyEncryptionAlgorithms, $contentEncryptionAlgorithms);
53
            if (!array_key_exists($type, $data['algorithm']['algorithms'])) {
54
                $data['algorithm']['algorithms'][$type] = [];
55
            }
56
            $data['algorithm']['algorithms'][$type][$alias] = [
57
                'name' => $algorithm->name(),
58
            ];
59
        }
60
61
        $data['algorithm']['types'] = [
62
            'signature' => $signatureAlgorithms,
63
            'key_encryption' => $keyEncryptionAlgorithms,
64
            'content_encryption' => $contentEncryptionAlgorithms,
65
        ];
66
    }
67
68
    /**
69
     * @param Algorithm $algorithm
70
     * @param int       $signatureAlgorithms
71
     * @param int       $keyEncryptionAlgorithms
72
     * @param int       $contentEncryptionAlgorithms
73
     *
74
     * @return string
75
     */
76
    private function getAlgorithmType(Algorithm $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string
77
    {
78
        switch (true) {
79
            case $algorithm instanceof SignatureAlgorithm:
80
                $signatureAlgorithms++;
81
82
                return 'Signature';
83
            case $algorithm instanceof KeyEncryptionAlgorithm:
84
                $keyEncryptionAlgorithms++;
85
86
                return 'Key Encryption';
87
            case $algorithm instanceof ContentEncryptionAlgorithm:
88
                $contentEncryptionAlgorithms++;
89
90
                return 'Content Encryption';
91
            default:
92
                return 'Unknown';
93
        }
94
    }
95
}
96