Passed
Pull Request — master (#315)
by Thijs
11:19
created

IdentityProvider::getKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Configuration;
6
7
use RuntimeException;
8
9
use function array_filter;
10
use function array_pop;
11
use function count;
12
use function sprintf;
13
14
/**
15
 * Basic configuration wrapper
16
 */
17
final class IdentityProvider extends ArrayAdapter implements CertificateProvider, DecryptionProvider, EntityIdProvider
18
{
19
    /**
20
     * @return array|\Traversable|null
21
     */
22
    public function getKeys()
23
    {
24
        return $this->get('keys');
25
    }
26
27
28
    /**
29
     * @return string|null
30
     */
31
    public function getCertificateData(): ?string
32
    {
33
        return $this->get('certificateData');
34
    }
35
36
37
    /**
38
     * @return string|null
39
     */
40
    public function getCertificateFile(): ?string
41
    {
42
        return $this->get('certificateFile');
43
    }
44
45
46
    /**
47
     * @return array|mixed|\Traversable|null
48
     */
49
    public function getCertificateFingerprints()
50
    {
51
        return $this->get('certificateFingerprints');
52
    }
53
54
55
    /**
56
     * @return bool|null
57
     */
58
    public function isAssertionEncryptionRequired(): ?bool
59
    {
60
        return $this->get('assertionEncryptionEnabled');
61
    }
62
63
64
    /**
65
     * @return string|null
66
     */
67
    public function getSharedKey(): ?string
68
    {
69
        return $this->get('sharedKey');
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param bool $required
75
     * @return mixed|null
76
     */
77
    public function getPrivateKey(string $name, bool $required = null)
78
    {
79
        if ($required === null) {
80
            $required = false;
81
        }
82
        $privateKeys = $this->get('privateKeys');
83
        $key = array_filter($privateKeys, function (PrivateKey $key) use ($name) {
0 ignored issues
show
Bug introduced by
It seems like $privateKeys can also be of type null; however, parameter $array of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $key = array_filter(/** @scrutinizer ignore-type */ $privateKeys, function (PrivateKey $key) use ($name) {
Loading history...
84
            return $key->getName() === $name;
85
        });
86
87
        $keyCount = count($key);
88
        if ($keyCount !== 1 && $required) {
89
            throw new RuntimeException(sprintf(
90
                'Attempted to get privateKey by name "%s", found "%d" keys, where only one was expected. Please '
91
                . 'verify that your configuration is correct',
92
                $name,
93
                $keyCount
94
            ));
95
        }
96
97
        if (!$keyCount) {
98
            return null;
99
        }
100
101
        return array_pop($key);
102
    }
103
104
105
    /**
106
     * @return array|null
107
     */
108
    public function getBlacklistedAlgorithms(): ?array
109
    {
110
        return $this->get('blacklistedEncryptionAlgorithms');
111
    }
112
113
114
    /**
115
     * @return string|null
116
     */
117
    public function getEntityId(): ?string
118
    {
119
        return $this->get('entityId');
120
    }
121
}
122