Passed
Push — master ( c66cd3...78be34 )
by Tim
12:20
created

ContactPerson::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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