Passed
Pull Request — master (#26)
by Jaime Pérez
02:00
created

EncryptionAlgorithmFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpectedParent() 0 3 1
A __construct() 0 7 1
A getAlgorithm() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Alg\Encryption;
6
7
use SimpleSAML\XMLSecurity\Alg\AbstractAlgorithmFactory;
8
use SimpleSAML\XMLSecurity\Constants;
9
use SimpleSAML\XMLSecurity\Key\AbstractKey;
10
11
/**
12
 * Factory class to create and configure encryption algorithms.
13
 *
14
 * @package simplesamlphp/xml-security
15
 */
16
final class EncryptionAlgorithmFactory extends AbstractAlgorithmFactory
17
{
18
    /**
19
     * A cache of algorithm implementations indexed by algorithm ID.
20
     *
21
     * @var string[]
22
     */
23
    protected static array $cache = [];
24
25
    /**
26
     * Whether the factory has been initialized or not.
27
     *
28
     * @var bool
29
     */
30
    protected static bool $initialized = false;
31
32
    /**
33
     * An array of blacklisted algorithms.
34
     *
35
     * Defaults to 3DES.
36
     *
37
     * @var string[]
38
     */
39
    protected array $blacklist = [
40
        Constants::BLOCK_ENC_3DES,
41
    ];
42
43
44
    /**
45
     * Build a factory that creates encryption algorithms.
46
     *
47
     * @param array|null $blacklist A list of algorithms forbidden for their use.
48
     */
49
    public function __construct(array $blacklist = null)
50
    {
51
        parent::__construct(
52
            $blacklist,
53
            [
54
                TripleDES::class,
55
                AES::class,
56
            ]
57
        );
58
    }
59
60
61
    /**
62
     * Get the name of the abstract class our algorithm implementations must extend.
63
     *
64
     * @return string
65
     */
66
    protected static function getExpectedParent(): string
67
    {
68
        return EncryptionAlgorithmInterface::class;
69
    }
70
71
72
    /**
73
     * Get a new object implementing the given encryption algorithm.
74
     *
75
     * @param string $algId The identifier of the algorithm desired.
76
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The key to use with the given algorithm.
77
     *
78
     * @return \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface An object implementing the given
79
     * algorithm.
80
     *
81
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If an error occurs, e.g. the given algorithm
82
     * is blacklisted, unknown or the given key is not suitable for it.
83
     */
84
    public function getAlgorithm(string $algId, AbstractKey $key): EncryptionAlgorithmInterface
85
    {
86
        return parent::getAlgorithm($algId, $key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getAlgorithm($algId, $key) returns the type SimpleSAML\XMLSecurity\Alg\AlgorithmInterface which includes types incompatible with the type-hinted return SimpleSAML\XMLSecurity\A...ptionAlgorithmInterface.
Loading history...
87
    }
88
}