Test Failed
Branch master (62528d)
by Zangra
14:14 queued 12:04
created

Customer::toXML()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 21
rs 9.2222
cc 6
nc 32
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order;
5
6
use Bpost\BpostApiClient\Common\XmlHelper;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
8
use DOMDocument;
9
use DOMElement;
10
use SimpleXMLElement;
11
12
/**
13
 * bPost Customer class
14
 *
15
 * @author Tijs Verkoyen <[email protected]>
16
 */
17
class Customer
18
{
19
    public const TAG_NAME = 'customer';
20
21
    private ?string $name = null;
22
    private ?string $company = null;
23
    private ?Address $address = null;
24
    private ?string $emailAddress = null;
25
    private ?string $phoneNumber = null;
26
27
    public function setAddress(Address $address): void
28
    {
29
        $this->address = $address;
30
    }
31
32
    public function getAddress(): ?Address
33
    {
34
        return $this->address;
35
    }
36
37
    public function setCompany(string $company): void
38
    {
39
        $this->company = $company;
40
    }
41
42
    public function getCompany(): ?string
43
    {
44
        return $this->company;
45
    }
46
47
    /**
48
     * @throws BpostInvalidLengthException
49
     */
50
    public function setEmailAddress(string $emailAddress): void
51
    {
52
        $length = 50;
53
        if (mb_strlen($emailAddress) > $length) {
54
            throw new BpostInvalidLengthException('emailAddress', mb_strlen($emailAddress), $length);
55
        }
56
        $this->emailAddress = $emailAddress;
57
    }
58
59
    public function getEmailAddress(): ?string
60
    {
61
        return $this->emailAddress;
62
    }
63
64
    public function setName(string $name): void
65
    {
66
        $this->name = $name;
67
    }
68
69
    public function getName(): ?string
70
    {
71
        return $this->name;
72
    }
73
74
    /**
75
     * @throws BpostInvalidLengthException
76
     */
77
    public function setPhoneNumber(string $phoneNumber): void
78
    {
79
        $length = 20;
80
        if (mb_strlen($phoneNumber) > $length) {
81
            throw new BpostInvalidLengthException('phoneNumber', mb_strlen($phoneNumber), $length);
82
        }
83
        $this->phoneNumber = $phoneNumber;
84
    }
85
86
    public function getPhoneNumber(): ?string
87
    {
88
        return $this->phoneNumber;
89
    }
90
91
    /**
92
     * @throws \DOMException
93
     */
94
    public function toXML(DOMDocument $document, ?string $prefix = null): DOMElement
95
    {
96
        $customer = $document->createElement(XmlHelper::getPrefixedTagName(static::TAG_NAME, $prefix));
97
98
        if ($this->name !== null) {
99
            $customer->appendChild($document->createElement('common:name', $this->name));
100
        }
101
        if ($this->company !== null) {
102
            $customer->appendChild($document->createElement('common:company', $this->company));
103
        }
104
        if ($this->address !== null) {
105
            $customer->appendChild($this->address->toXML($document, 'common'));
106
        }
107
        if ($this->emailAddress !== null) {
108
            $customer->appendChild($document->createElement('common:emailAddress', $this->emailAddress));
109
        }
110
        if ($this->phoneNumber !== null) {
111
            $customer->appendChild($document->createElement('common:phoneNumber', $this->phoneNumber));
112
        }
113
114
        return $customer;
115
    }
116
117
    /**
118
     * @throws BpostInvalidLengthException
119
     */
120
    public static function createFromXMLHelper(SimpleXMLElement $xml, Customer $instance): Customer
121
    {
122
        if (isset($xml->name) && (string) $xml->name !== '') {
123
            $instance->setName((string) $xml->name);
124
        }
125
        if (isset($xml->company) && (string) $xml->company !== '') {
126
            $instance->setCompany((string) $xml->company);
127
        }
128
        if (isset($xml->address)) {
129
            $instance->setAddress(Address::createFromXML($xml->address));
130
        }
131
        if (isset($xml->emailAddress) && (string) $xml->emailAddress !== '') {
132
            $instance->setEmailAddress((string) $xml->emailAddress);
133
        }
134
        if (isset($xml->phoneNumber) && (string) $xml->phoneNumber !== '') {
135
            $instance->setPhoneNumber((string) $xml->phoneNumber);
136
        }
137
138
        return $instance;
139
    }
140
}
141