ContactPerson::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 8
dl 0
loc 18
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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

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