AbstractContainer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 5
b 0
f 0
dl 0
loc 114
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerExtensionHandler() 0 5 1
A getExtensionHandler() 0 9 2
A registerElementHandler() 0 5 1
A getBlacklistedEncryptionAlgorithms() 0 3 1
A getElementHandler() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\Compat;
6
7
use Psr\Clock\ClockInterface;
8
use Psr\Log\LoggerInterface;
9
use SimpleSAML\SAML11\Assert\Assert;
10
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
11
use SimpleSAML\XML\{AbstractElement, ElementInterface};
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
14
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
15
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
16
17
use function array_key_exists;
18
use function strval;
19
20
abstract class AbstractContainer
21
{
22
    /** @var array */
23
    protected array $registry = [];
24
25
    /** @var array */
26
    protected array $extRegistry = [];
27
28
    /** @var array|null */
29
    protected ?array $blacklistedEncryptionAlgorithms = [
30
        EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
31
        KeyTransportAlgorithmFactory::DEFAULT_BLACKLIST,
32
        SignatureAlgorithmFactory::DEFAULT_BLACKLIST,
33
    ];
34
35
36
    /**
37
     * Get the list of algorithms that are blacklisted for any encryption operation.
38
     *
39
     * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null if we want to use the
40
     * defaults.
41
     */
42
    public function getBlacklistedEncryptionAlgorithms(): ?array
43
    {
44
        return $this->blacklistedEncryptionAlgorithms;
45
    }
46
47
48
    /**
49
     * Register a class that can handle a given element.
50
     *
51
     * @param string $class The class name of a class extending AbstractElement
52
     * @psalm-param class-string $class
53
     */
54
    public function registerElementHandler(string $class): void
55
    {
56
        Assert::subclassOf($class, AbstractElement::class);
57
        $key = '{' . strval($class::NS) . '}' . AbstractElement::getClassName($class);
58
        $this->registry[$key] = $class;
59
    }
60
61
62
    /**
63
     * Register a class that can handle given extension points of the standard.
64
     *
65
     * @param string $class The class name of a class extending AbstractElement or implementing ExtensionPointInterface.
66
     * @psalm-param class-string $class
67
     */
68
    public function registerExtensionHandler(string $class): void
69
    {
70
        Assert::subclassOf($class, ExtensionPointInterface::class);
71
        $key = '{' . $class::getXsiTypeNamespaceURI() . '}' . $class::getXsiTypeName();
72
        $this->extRegistry[$key] = $class;
73
    }
74
75
76
    /**
77
     * Search for a class that implements an element in the given $namespace.
78
     *
79
     * Such classes must have been registered previously by calling registerExtensionHandler(), and they must
80
     * extend \SimpleSAML\XML\AbstractElement.
81
     *
82
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $qName The qualified name of the element.
83
     *
84
     * @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractElement and
85
     * implementing support for the given element, or null if no such class has been registered before.
86
     * @psalm-return class-string|null
87
     */
88
    public function getElementHandler(QNameValue $qName): ?string
89
    {
90
        $key = '{' . strval($qName->getNameSpaceURI()) . '}' . strval($qName->getLocalName());
91
        if (array_key_exists($key, $this->registry) === true) {
92
            Assert::implementsInterface($this->registry[$key], ElementInterface::class);
93
            return $this->registry[$key];
94
        }
95
96
        return null;
97
    }
98
99
100
    /**
101
     * Search for a class that implements a custom element type.
102
     *
103
     * Such classes must have been registered previously by calling registerExtensionHandler(), and they must
104
     * implement \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface.
105
     *
106
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $qName The qualified name of the extension.
107
     * @return string|null The fully-qualified name of a class implementing
108
     *  \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface or null if no such class has been registered before.
109
     * @psalm-return class-string|null
110
     */
111
    public function getExtensionHandler(QNameValue $qName): ?string
112
    {
113
        $key = '{' . strval($qName->getNameSpaceURI()) . '}' . strval($qName->getLocalName());
114
        if (array_key_exists($key, $this->extRegistry) === true) {
115
            Assert::implementsInterface($this->extRegistry[$key], ExtensionPointInterface::class);
116
            return $this->extRegistry[$key];
117
        }
118
119
        return null;
120
    }
121
122
123
    /**
124
     * Get a PSR-3 compatible logger.
125
     * @return \Psr\Log\LoggerInterface
126
     */
127
    abstract public function getLogger(): LoggerInterface;
128
129
130
    /**
131
     * Get the system clock, using UTC for a timezone
132
     */
133
    abstract public function getClock(): ClockInterface;
134
}
135