Completed
Push — master ( 484ff8...780e48 )
by Jaime Pérez
15s queued 12s
created

EncryptionAlgorithmFactory::getAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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
}