Passed
Branch feature/php83 (cb5cde)
by Tim
14:20
created

Key   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Certificate;
6
7
use ArrayAccess;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Certificate\Exception\InvalidKeyUsageException;
10
use SimpleSAML\SAML2\Exception\InvalidArgumentException;
11
12
use function array_key_exists;
13
use function is_string;
14
15
/**
16
 * Simple DTO wrapper for (X509) keys. Implements ArrayAccess
17
 * for easier backwards compatibility.
18
 */
19
class Key implements ArrayAccess
20
{
21
    // Possible key usages
22
    public const string USAGE_SIGNING = 'signing';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 22 at column 24
Loading history...
23
24
    public const string USAGE_ENCRYPTION = 'encryption';
25
26
27
    /** @var array */
28
    protected array $keyData = [];
29
30
31
    /**
32
     * @param array $keyData
33
     */
34
    public function __construct(array $keyData)
35
    {
36
        // forcing usage of offsetSet
37
        foreach ($keyData as $property => $value) {
38
            $this->offsetSet($property, $value);
39
        }
40
    }
41
42
43
    /**
44
     * Whether or not the key is configured to be used for usage given
45
     *
46
     * @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
47
     */
48
    public function canBeUsedFor(string $usage): bool
49
    {
50
        Assert::oneOf(
51
            $usage,
52
            [self::USAGE_ENCRYPTION, self::USAGE_SIGNING],
53
            'Invalid key usage given: "%s", usages "%2$s" allowed',
54
            InvalidKeyUsageException::class,
55
        );
56
57
        return isset($this->keyData[$usage]) && $this->keyData[$usage];
58
    }
59
60
61
    /**
62
     * @param mixed $offset
63
     *
64
     * @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
65
     */
66
    public function offsetExists(mixed $offset): bool
67
    {
68
        if (!is_string($offset)) {
69
            throw InvalidArgumentException::invalidType('string', $offset);
70
        }
71
        return array_key_exists($offset, $this->keyData);
72
    }
73
74
75
    /**
76
     * @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
77
     */
78
    public function offsetGet($offset): mixed
79
    {
80
        if (!is_string($offset)) {
81
            throw InvalidArgumentException::invalidType('string', $offset);
82
        }
83
        return $this->keyData[$offset];
84
    }
85
86
87
    /**
88
     * @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
89
     */
90
    public function offsetSet(mixed $offset, mixed $value): void
91
    {
92
        if (!is_string($offset)) {
93
            throw InvalidArgumentException::invalidType('string', $offset);
94
        }
95
        $this->keyData[$offset] = $value;
96
    }
97
98
99
    /**
100
     * @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
101
     */
102
    public function offsetUnset(mixed $offset): void
103
    {
104
        if (!is_string($offset)) {
105
            throw InvalidArgumentException::invalidType('string', $offset);
106
        }
107
        unset($this->keyData[$offset]);
108
    }
109
}
110