Completed
Push — master ( 2b6d49...c13cc5 )
by Florent
02:37
created

AlgorithmManagerFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 89
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createAlgorithmManager() 0 11 2
A isAlgorithmSupported() 0 4 1
A getAlgorithmClass() 0 7 2
B getSupportedAlgorithms() 0 41 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Factory;
13
14
use Jose\Algorithm\JWAManager;
15
16
final class AlgorithmManagerFactory
17
{
18
    /**
19
     * @param string[] $algorithms
20
     *
21
     * @return \Jose\Algorithm\JWAManagerInterface
22
     */
23
    public static function createAlgorithmManager(array $algorithms)
24
    {
25
        $jwa_manager = new JWAManager();
26
27
        foreach ($algorithms as $algorithm) {
28
            $class = self::getAlgorithmClass($algorithm);
29
            $jwa_manager->addAlgorithm(new $class());
30
        }
31
32
        return $jwa_manager;
33
    }
34
35
    /**
36
     * @param string $algorithm
37
     *
38
     * @return bool
39
     */
40
    private static function isAlgorithmSupported($algorithm)
41
    {
42
        return array_key_exists($algorithm, self::getSupportedAlgorithms());
43
    }
44
45
    /**
46
     * @param string $algorithm
47
     *
48
     * @throws \InvalidArgumentException
49
     *
50
     * @return string
51
     */
52
    private static function getAlgorithmClass($algorithm)
53
    {
54
        if (self::isAlgorithmSupported($algorithm)) {
55
            return self::getSupportedAlgorithms()[$algorithm];
56
        }
57
        throw new \InvalidArgumentException(sprintf('Algorithm "%s" is not supported.', $algorithm));
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    private static function getSupportedAlgorithms()
64
    {
65
        return [
66
            'HS256'              => '\Jose\Algorithm\Signature\HS256',
67
            'HS384'              => '\Jose\Algorithm\Signature\HS384',
68
            'HS512'              => '\Jose\Algorithm\Signature\HS512',
69
            'ES256'              => '\Jose\Algorithm\Signature\ES256',
70
            'ES384'              => '\Jose\Algorithm\Signature\ES384',
71
            'ES512'              => '\Jose\Algorithm\Signature\ES512',
72
            'none'               => '\Jose\Algorithm\Signature\None',
73
            'RS256'              => '\Jose\Algorithm\Signature\RS256',
74
            'RS384'              => '\Jose\Algorithm\Signature\RS384',
75
            'RS512'              => '\Jose\Algorithm\Signature\RS512',
76
            'PS256'              => '\Jose\Algorithm\Signature\PS256',
77
            'PS384'              => '\Jose\Algorithm\Signature\PS384',
78
            'PS512'              => '\Jose\Algorithm\Signature\PS512',
79
            'A128GCM'            => '\Jose\Algorithm\ContentEncryption\A128GCM',
80
            'A192GCM'            => '\Jose\Algorithm\ContentEncryption\A192GCM',
81
            'A256GCM'            => '\Jose\Algorithm\ContentEncryption\A256GCM',
82
            'A128CBC-HS256'      => '\Jose\Algorithm\ContentEncryption\A128CBCHS256',
83
            'A192CBC-HS384'      => '\Jose\Algorithm\ContentEncryption\A192CBCHS384',
84
            'A256CBC-HS512'      => '\Jose\Algorithm\ContentEncryption\A256CBCHS512',
85
            'A128KW'             => '\Jose\Algorithm\KeyEncryption\A128KW',
86
            'A192KW'             => '\Jose\Algorithm\KeyEncryption\A192KW',
87
            'A256KW'             => '\Jose\Algorithm\KeyEncryption\A256KW',
88
            'A128GCMKW'          => '\Jose\Algorithm\KeyEncryption\A128GCMKW',
89
            'A192GCMKW'          => '\Jose\Algorithm\KeyEncryption\A192GCMKW',
90
            'A256GCMKW'          => '\Jose\Algorithm\KeyEncryption\A256GCMKW',
91
            'dir'                => '\Jose\Algorithm\KeyEncryption\Dir',
92
            'ECDH-ES'            => '\Jose\Algorithm\KeyEncryption\ECDHES',
93
            'ECDH-ES+A128KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA128KW',
94
            'ECDH-ES+A192KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA192KW',
95
            'ECDH-ES+A256KW'     => '\Jose\Algorithm\KeyEncryption\ECDHESA256KW',
96
            'PBES2-HS256+A128KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS256A128KW',
97
            'PBES2-HS384+A192KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS384A192KW',
98
            'PBES2-HS512+A256KW' => '\Jose\Algorithm\KeyEncryption\PBES2HS512A256KW',
99
            'RSA1_5'             => '\Jose\Algorithm\KeyEncryption\RSA15',
100
            'RSA-OAEP'           => '\Jose\Algorithm\KeyEncryption\RSAOAEP',
101
            'RSA-OAEP-256'       => '\Jose\Algorithm\KeyEncryption\RSAOAEP256',
102
        ];
103
    }
104
}
105