NameID::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 6
nop 5
dl 0
loc 44
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use SimpleSAML\SAML2\Assert\Assert;
8
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
11
use SimpleSAML\SAML2\Type\SAMLStringValue;
12
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
16
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
17
18
use function array_change_key_case;
19
use function array_filter;
20
use function array_key_exists;
21
use function array_keys;
22
23
/**
24
 * Class representing the saml:NameID element.
25
 *
26
 * @package simplesamlphp/saml2
27
 */
28
final class NameID extends NameIDType implements
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\NameIDType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    EncryptableElementInterface,
30
    SchemaValidatableElementInterface
31
{
32
    use EncryptableElementTrait;
33
    use SchemaValidatableElementTrait;
34
35
36
    /**
37
     * Initialize a saml:NameID
38
     *
39
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue $value
40
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
41
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
42
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
43
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPProvidedID
44
     */
45
    public function __construct(
46
        SAMLStringValue $value,
47
        ?SAMLStringValue $NameQualifier = null,
48
        ?SAMLStringValue $SPNameQualifier = null,
49
        ?SAMLAnyURIValue $Format = null,
50
        ?SAMLStringValue $SPProvidedID = null,
51
    ) {
52
        if ($Format !== null) {
53
            switch ($Format->getValue()) {
54
                case C::NAMEID_EMAIL_ADDRESS:
55
                    Assert::email(
56
                        $value->getValue(),
57
                        "The content %s of the NameID was not in the format specified by the Format attribute",
58
                    );
59
                    break;
60
                case C::NAMEID_ENTITY:
61
                    /* 8.3.6: the NameQualifier, SPNameQualifier, and SPProvidedID attributes MUST be omitted. */
62
                    Assert::null($NameQualifier, "Entity Identifier included a disallowed NameQualifier attribute.");
63
                    Assert::null(
64
                        $SPNameQualifier,
65
                        "Entity Identifier included a disallowed SPNameQualifier attribute.",
66
                    );
67
                    Assert::null($SPProvidedID, "Entity Identifier included a disallowed SPProvidedID attribute.");
68
                    break;
69
                case C::NAMEID_PERSISTENT:
70
                    /* 8.3.7: Persistent name identifier values MUST NOT exceed a length of 256 characters. */
71
                    Assert::maxLength(
72
                        $value->getValue(),
73
                        256,
74
                        "Persistent name identifier values MUST NOT exceed a length of 256 characters.",
75
                    );
76
                    break;
77
                case C::NAMEID_TRANSIENT:
78
                    /* 8.3.8: Transient name identifier values MUST NOT exceed a length of 256 characters. */
79
                    Assert::maxLength(
80
                        $value->getValue(),
81
                        256,
82
                        "Transient name identifier values MUST NOT exceed a length of 256 characters.",
83
                    );
84
                    break;
85
            }
86
        }
87
88
        parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
89
    }
90
91
92
    /**
93
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend
94
     */
95
    public function getEncryptionBackend(): ?EncryptionBackend
96
    {
97
        // return the encryption backend you want to use,
98
        // or null if you are fine with the default
99
        return null;
100
    }
101
102
103
    /**
104
     * Create a class from an array
105
     *
106
     * @param array{
107
     *   'value': string,
108
     *   'NameQualifier'?: string,
109
     *   'SPNameQualifier'?: string,
110
     *   'Format'?: string,
111
     *   'SPProvidedID'?: string,
112
     * } $data
113
     */
114
    public static function fromArray(array $data): static
115
    {
116
        $data = self::processArrayContents($data);
117
118
        return new static(
119
            SAMLStringValue::fromString($data['value']),
120
            $data['NameQualifier'] ? SAMLStringValue::fromString($data['NameQualifier']) : null,
121
            $data['SPNameQualifier'] ? SAMLStringValue::fromString($data['SPNameQualifier']) : null,
122
            $data['Format'] ? SAMLAnyURIValue::fromString($data['Format']) : null,
123
            $data['SPProvidedID'] ? SAMLStringValue::fromString($data['SPProvidedID']) : null,
124
        );
125
    }
126
127
128
    /**
129
     * Validates an array representation of this object and returns the same array with
130
     * rationalized keys (casing) and parsed sub-elements.
131
     *
132
     * @param array{
133
     *   'value': string,
134
     *   'NameQualifier'?: string,
135
     *   'SPNameQualifier'?: string,
136
     *   'Format'?: string,
137
     *   'SPProvidedID'?: string,
138
     * } $data
139
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
140
     *   'value': string,
141
     *   'NameQualifier'?: string,
142
     *   'SPNameQualifier'?: string,
143
     *   'Format'?: string,
144
     *   'SPProvidedID'?: string,
145
     * }
146
     */
147
    private static function processArrayContents(array $data): array
148
    {
149
        $data = array_change_key_case($data, CASE_LOWER);
150
151
        // Make sure the array keys are known for this kind of object
152
        Assert::allOneOf(
153
            array_keys($data),
154
            [
155
                'value',
156
                'format',
157
                'namequalifier',
158
                'spnamequalifier',
159
                'spprovidedid',
160
            ],
161
            ArrayValidationException::class,
162
        );
163
164
        Assert::keyExists($data, 'value', ArrayValidationException::class);
165
        Assert::string($data['value'], ArrayValidationException::class);
166
        $retval = ['value' => $data['value']];
167
168
        if (array_key_exists('format', $data)) {
169
            Assert::string($data['format'], ArrayValidationException::class);
170
            $retval['Format'] = $data['format'];
171
        }
172
173
        if (array_key_exists('namequalifier', $data)) {
174
            Assert::string($data['namequalifier'], ArrayValidationException::class);
175
            $retval['NameQualifier'] = $data['namequalifier'];
176
        }
177
178
        if (array_key_exists('spnamequalifier', $data)) {
179
            Assert::string($data['spnamequalifier'], ArrayValidationException::class);
180
            $retval['SPNameQualifier'] = $data['spnamequalifier'];
181
        }
182
183
        if (array_key_exists('spprovidedid', $data)) {
184
            Assert::string($data['spprovidedid'], ArrayValidationException::class);
185
            $retval['SPProvidedID'] = $data['spprovidedid'];
186
        }
187
188
        return $retval;
189
    }
190
191
192
    /**
193
     * Create an array from this class
194
     *
195
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
196
     *   'value': string,
197
     *   'NameQualifier'?: string,
198
     *   'SPNameQualifier'?: string,
199
     *   'Format'?: string,
200
     *   'SPProvidedID'?: string,
201
     * }
202
     */
203
    public function toArray(): array
204
    {
205
        $data = [
206
            'value' => $this->getContent()->getValue(),
207
            'Format' => $this->getFormat()?->getValue(),
208
            'NameQualifier' => $this->getNameQualifier()?->getValue(),
209
            'SPNameQualifier' => $this->getSPNameQualifier()?->getValue(),
210
            'SPProvidedID' => $this->getSPProvidedID()?->getValue(),
211
        ];
212
213
        return array_filter($data);
214
    }
215
}
216