ContactPerson   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 127
dl 0
loc 339
rs 10
c 0
b 0
f 0
wmc 29

12 Methods

Rating   Name   Duplication   Size   Complexity  
B processArrayContents() 0 70 11
A fromXML() 0 31 2
A getGivenName() 0 3 1
A getEmailAddress() 0 3 1
A __construct() 0 18 1
A getContactType() 0 3 1
A getCompany() 0 3 1
A toArray() 0 26 4
A toXML() 0 24 4
A fromArray() 0 13 1
A getTelephoneNumber() 0 3 1
A getSurName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
11
use SimpleSAML\XML\ArrayizableElementInterface;
12
use SimpleSAML\XML\Attribute as XMLAttribute;
13
use SimpleSAML\XML\Constants as C;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
use SimpleSAML\XML\Exception\TooManyElementsException;
16
use SimpleSAML\XML\ExtendableAttributesTrait;
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use SimpleSAML\XML\XsNamespace as NS;
20
21
use function array_change_key_case;
22
use function array_filter;
23
use function array_key_exists;
24
use function array_keys;
25
use function array_pop;
26
use function count;
27
28
/**
29
 * Class representing SAML 2 ContactPerson.
30
 *
31
 * @package simplesamlphp/saml2
32
 */
33
final class ContactPerson extends AbstractMdElement implements
34
    ArrayizableElementInterface,
35
    SchemaValidatableElementInterface
36
{
37
    use ExtendableAttributesTrait;
38
    use ExtendableElementTrait;
39
    use SchemaValidatableElementTrait;
40
41
    /** The namespace-attribute for the xs:anyAttribute element */
42
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
43
44
45
    /**
46
     * The several different contact types as defined per specification
47
     */
48
    public const CONTACT_TYPES = [
49
        'technical',
50
        'support',
51
        'administrative',
52
        'billing',
53
        'other',
54
    ];
55
56
57
    /**
58
     * ContactPerson constructor.
59
     *
60
     * @param string $contactType
61
     * @param \SimpleSAML\SAML2\XML\md\Company|null $company
62
     * @param \SimpleSAML\SAML2\XML\md\GivenName|null $givenName
63
     * @param \SimpleSAML\SAML2\XML\md\SurName|null $surName
64
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
65
     * @param \SimpleSAML\SAML2\XML\md\EmailAddress[] $emailAddress
66
     * @param \SimpleSAML\SAML2\XML\md\TelephoneNumber[] $telephoneNumber
67
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttribute
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list 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...
68
     */
69
    public function __construct(
70
        protected string $contactType,
71
        protected ?Company $company = null,
72
        protected ?GivenName $givenName = null,
73
        protected ?SurName $surName = null,
74
        ?Extensions $extensions = null,
75
        protected array $emailAddress = [],
76
        protected array $telephoneNumber = [],
77
        array $namespacedAttribute = [],
78
    ) {
79
        Assert::oneOf($contactType, self::CONTACT_TYPES);
80
        Assert::maxCount($emailAddress, C::UNBOUNDED_LIMIT);
81
        Assert::allIsInstanceOf($emailAddress, EmailAddress::class);
82
        Assert::maxCount($telephoneNumber, C::UNBOUNDED_LIMIT);
83
        Assert::allIsInstanceOf($telephoneNumber, TelephoneNumber::class);
84
85
        $this->setExtensions($extensions);
86
        $this->setAttributesNS($namespacedAttribute);
87
    }
88
89
90
    /**
91
     * Collect the value of the contactType-property
92
     *
93
     * @return string
94
     */
95
    public function getContactType(): string
96
    {
97
        return $this->contactType;
98
    }
99
100
101
    /**
102
     * Collect the value of the Company-property
103
     *
104
     * @return \SimpleSAML\SAML2\XML\md\Company|null
105
     */
106
    public function getCompany(): ?Company
107
    {
108
        return $this->company;
109
    }
110
111
112
    /**
113
     * Collect the value of the GivenName-property
114
     *
115
     * @return \SimpleSAML\SAML2\XML\md\GivenName|null
116
     */
117
    public function getGivenName(): ?GivenName
118
    {
119
        return $this->givenName;
120
    }
121
122
123
    /**
124
     * Collect the value of the SurName-property
125
     *
126
     * @return \SimpleSAML\SAML2\XML\md\SurName|null
127
     */
128
    public function getSurName(): ?SurName
129
    {
130
        return $this->surName;
131
    }
132
133
134
    /**
135
     * Collect the value of the EmailAddress-property.
136
     *
137
     * @return \SimpleSAML\SAML2\XML\md\EmailAddress[]
138
     */
139
    public function getEmailAddress(): array
140
    {
141
        return $this->emailAddress;
142
    }
143
144
145
    /**
146
     * Collect the value of the TelephoneNumber property
147
     *
148
     * @return \SimpleSAML\SAML2\XML\md\TelephoneNumber[]
149
     */
150
    public function getTelephoneNumber(): array
151
    {
152
        return $this->telephoneNumber;
153
    }
154
155
156
    /**
157
     * Initialize a ContactPerson element.
158
     *
159
     * @param \DOMElement $xml The XML element we should load.
160
     * @return static
161
     *
162
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
163
     *   if the qualified name of the supplied element is wrong
164
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
165
     *   if the supplied element is missing one of the mandatory attributes
166
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
167
     *   if too many child-elements of a type are specified
168
     */
169
    public static function fromXML(DOMElement $xml): static
170
    {
171
        Assert::same($xml->localName, 'ContactPerson', InvalidDOMElementException::class);
172
        Assert::same($xml->namespaceURI, ContactPerson::NS, InvalidDOMElementException::class);
173
174
        $contactType = self::getAttribute($xml, 'contactType');
175
176
        $company = Company::getChildrenOfClass($xml);
177
        Assert::maxCount($company, 1, 'More than one Company in md:ContactPerson');
178
179
        $givenName = GivenName::getChildrenOfClass($xml);
180
        Assert::maxCount($givenName, 1, 'More than one GivenName in md:ContactPerson');
181
182
        $surName = SurName::getChildrenOfClass($xml);
183
        Assert::maxCount($surName, 1, 'More than one SurName in md:ContactPerson');
184
185
        $email = EmailAddress::getChildrenOfClass($xml);
186
        $telephone = TelephoneNumber::getChildrenOfClass($xml);
187
188
        $extensions = Extensions::getChildrenOfClass($xml);
189
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
190
191
        return new static(
192
            $contactType,
193
            array_pop($company),
194
            array_pop($givenName),
195
            array_pop($surName),
196
            (count($extensions) === 1) ? $extensions[0] : null,
197
            $email,
198
            $telephone,
199
            self::getAttributesNSFromXML($xml),
200
        );
201
    }
202
203
204
    /**
205
     * Convert this ContactPerson to XML.
206
     *
207
     * @param \DOMElement|null $parent The element we should add this contact to.
208
     *
209
     * @return \DOMElement The new ContactPerson-element.
210
     */
211
    public function toXML(?DOMElement $parent = null): DOMElement
212
    {
213
        $e = $this->instantiateParentElement($parent);
214
215
        $e->setAttribute('contactType', $this->getContactType());
216
217
        foreach ($this->getAttributesNS() as $attr) {
218
            $attr->toXML($e);
219
        }
220
221
        $this->getExtensions()?->toXML($e);
222
        $this->getCompany()?->toXML($e);
223
        $this->getGivenName()?->toXML($e);
224
        $this->getSurName()?->toXML($e);
225
226
        foreach ($this->getEmailAddress() as $mail) {
227
            $mail->toXML($e);
228
        }
229
230
        foreach ($this->getTelephoneNumber() as $telephone) {
231
            $telephone->toXML($e);
232
        }
233
234
        return $e;
235
    }
236
237
238
    /**
239
     * Create a class from an array
240
     *
241
     * @param array $data
242
     * @return static
243
     */
244
    public static function fromArray(array $data): static
245
    {
246
        $data = self::processArrayContents($data);
247
248
        return new static(
249
            $data['contactType'],
250
            $data['Company'] ?? null,
251
            $data['GivenName'] ?? null,
252
            $data['SurName'] ?? null,
253
            $data['Extensions'] ?? null,
254
            $data['EmailAddress'] ?? [],
255
            $data['TelephoneNumber'] ?? [],
256
            $data['attributes'] ?? [],
257
        );
258
    }
259
260
261
    /**
262
     * Validates an array representation of this object and returns the same array with
263
     * rationalized keys (casing) and parsed sub-elements.
264
     *
265
     * @param array $data
266
     * @return array $data
267
     */
268
    private static function processArrayContents(array $data): array
269
    {
270
        $data = array_change_key_case($data, CASE_LOWER);
271
272
        // Make sure the array keys are known for this kind of object
273
        Assert::allOneOf(
274
            array_keys($data),
275
            [
276
                'contacttype',
277
                'company',
278
                'givenname',
279
                'surname',
280
                'emailaddress',
281
                'telephonenumber',
282
                'extensions',
283
                'attributes',
284
            ],
285
            ArrayValidationException::class,
286
        );
287
288
        Assert::keyExists($data, 'contacttype', ArrayValidationException::class);
289
        Assert::string($data['contacttype'], ArrayValidationException::class);
290
291
        $retval = ['contactType' => $data['contacttype']];
292
293
        if (array_key_exists('company', $data)) {
294
            Assert::string($data['company'], ArrayValidationException::class);
295
            $retval['Company'] = new Company($data['company']);
296
        }
297
298
        if (array_key_exists('givenname', $data)) {
299
            Assert::string($data['givenname'], ArrayValidationException::class);
300
            $retval['GivenName'] = new GivenName($data['givenname']);
301
        }
302
303
        if (array_key_exists('surname', $data)) {
304
            Assert::string($data['surname'], ArrayValidationException::class);
305
            $retval['SurName'] = new SurName($data['surname']);
306
        }
307
308
        if (array_key_exists('emailaddress', $data)) {
309
            Assert::isArray($data['emailaddress'], ArrayValidationException::class);
310
            Assert::allString($data['emailaddress'], ArrayValidationException::class);
311
            foreach ($data['emailaddress'] as $email) {
312
                $retval['EmailAddress'][] = new EmailAddress($email);
313
            }
314
        }
315
316
        if (array_key_exists('telephonenumber', $data)) {
317
            Assert::isArray($data['telephonenumber'], ArrayValidationException::class);
318
            Assert::allString($data['telephonenumber'], ArrayValidationException::class);
319
            foreach ($data['telephonenumber'] as $telephone) {
320
                $retval['TelephoneNumber'][] = new TelephoneNumber($telephone);
321
            }
322
        }
323
324
        if (array_key_exists('extensions', $data)) {
325
            Assert::isArray($data['extensions'], ArrayValidationException::class);
326
            $retval['Extensions'] = new Extensions($data['extensions']);
327
        }
328
329
        if (array_key_exists('attributes', $data)) {
330
            Assert::isArray($data['attributes'], ArrayValidationException::class);
331
            Assert::allIsArray($data['attributes'], ArrayValidationException::class);
332
            foreach ($data['attributes'] as $i => $attr) {
333
                $retval['attributes'][] = XMLAttribute::fromArray($attr);
334
            }
335
        }
336
337
        return $retval;
338
    }
339
340
341
    /**
342
     * Create an array from this class
343
     *
344
     * @return array
345
     */
346
    public function toArray(): array
347
    {
348
        $data = [
349
            'ContactType' => $this->getContactType(),
350
            'Company' => $this->getCompany()?->getContent(),
351
            'GivenName' => $this->getGivenName()?->getContent(),
352
            'SurName' => $this->getSurName()?->getContent(),
353
            'EmailAddress' => [],
354
            'TelephoneNumber' => [],
355
            'Extensions' => $this->Extensions?->getList(),
0 ignored issues
show
Bug introduced by
The method getList() does not exist on SimpleSAML\XML\AbstractElement. It seems like you code against a sub-type of SimpleSAML\XML\AbstractElement such as SimpleSAML\SAML2\XML\md\Extensions or SimpleSAML\SAML2\XML\samlp\Extensions. ( Ignorable by Annotation )

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

355
            'Extensions' => $this->Extensions?->/** @scrutinizer ignore-call */ getList(),
Loading history...
356
            'attributes' => [],
357
        ];
358
359
        foreach ($this->getEmailAddress() as $mail) {
360
            $data['EmailAddress'] = array_merge($data['EmailAddress'], $mail->toArray());
361
        }
362
363
        foreach ($this->getTelephoneNumber() as $telephone) {
364
            $data['TelephoneNumber'] = array_merge($data['TelephoneNumber'], $telephone->toArray());
365
        }
366
367
        foreach ($this->getAttributesNS() as $attr) {
368
            $data['attributes'][] = $attr->toArray();
369
        }
370
371
        return array_filter($data);
372
    }
373
}
374