At247::createFromXML()   F
last analyzed

Complexity

Conditions 21
Paths 1024

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 50
rs 0
cc 21
nc 1024
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order\Box;
5
6
use Bpost\BpostApiClient\Bpost\Order\Box\National\Unregistered;
7
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
8
use Bpost\BpostApiClient\Bpost\Order\ParcelsDepotAddress;
9
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Product;
10
use Bpost\BpostApiClient\Common\XmlHelper;
11
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
12
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
13
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
14
use DOMDocument;
15
use DOMElement;
16
use SimpleXMLElement;
17
18
/**
19
 * bPost At247 class
20
 *
21
 * @author    Tijs Verkoyen <[email protected]>
22
 *
23
 * @version   3.0.0
24
 *
25
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
26
 * @license   BSD License
27
 */
28
class At247 extends National
29
{
30
    private ?string $parcelsDepotId = null;
31
    private ?string $parcelsDepotName = null;
32
    private ?ParcelsDepotAddress $parcelsDepotAddress = null;
33
34
    protected ?string $product = Product::PRODUCT_NAME_BPACK_24H_PRO;
35
36
    private ?string $memberId = null;
37
    private ?Unregistered $unregistered = null;
38
    private ?string $receiverName = null;
39
    private ?string $receiverCompany = null;
40
    protected ?string $requestedDeliveryDate = null;
41
42
    public function setMemberId(?string $memberId): void
43
    {
44
        $this->memberId = $memberId;
45
    }
46
47
    public function getMemberId(): ?string
48
    {
49
        return $this->memberId;
50
    }
51
52
    public function setParcelsDepotAddress(?ParcelsDepotAddress $parcelsDepotAddress): void
53
    {
54
        $this->parcelsDepotAddress = $parcelsDepotAddress;
55
    }
56
57
    public function getParcelsDepotAddress(): ?ParcelsDepotAddress
58
    {
59
        return $this->parcelsDepotAddress;
60
    }
61
62
    public function setParcelsDepotId(?string $parcelsDepotId): void
63
    {
64
        $this->parcelsDepotId = $parcelsDepotId;
65
    }
66
67
    public function getParcelsDepotId(): ?string
68
    {
69
        return $this->parcelsDepotId;
70
    }
71
72
    public function setParcelsDepotName(?string $parcelsDepotName): void
73
    {
74
        $this->parcelsDepotName = $parcelsDepotName;
75
    }
76
77
    public function getParcelsDepotName(): ?string
78
    {
79
        return $this->parcelsDepotName;
80
    }
81
82
    public function getUnregistered(): ?Unregistered
83
    {
84
        return $this->unregistered;
85
    }
86
87
    public function setUnregistered(?Unregistered $unregistered): void
88
    {
89
        $this->unregistered = $unregistered;
90
    }
91
92
    /**
93
     * @throws BpostInvalidValueException
94
     */
95
    public function setProduct(string $product): void
96
    {
97
        if (!in_array($product, self::getPossibleProductValues(), true)) {
98
            throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues());
99
        }
100
        parent::setProduct($product);
101
    }
102
103
    public static function getPossibleProductValues(): array
104
    {
105
        return [
106
            Product::PRODUCT_NAME_BPACK_24H_PRO,
107
            Product::PRODUCT_NAME_BPACK_24_7,
108
        ];
109
    }
110
111
    public function setReceiverCompany(?string $receiverCompany): void
112
    {
113
        $this->receiverCompany = $receiverCompany;
114
    }
115
116
    public function getReceiverCompany(): ?string
117
    {
118
        return $this->receiverCompany;
119
    }
120
121
    public function setReceiverName(?string $receiverName): void
122
    {
123
        $this->receiverName = $receiverName;
124
    }
125
126
    public function getReceiverName(): ?string
127
    {
128
        return $this->receiverName;
129
    }
130
131
    public function getRequestedDeliveryDate(): ?string
132
    {
133
        return $this->requestedDeliveryDate;
134
    }
135
136
    public function setRequestedDeliveryDate(?string $requestedDeliveryDate): void
137
    {
138
        $this->requestedDeliveryDate = $requestedDeliveryDate;
139
    }
140
141
    /**
142
     * @throws \DOMException
143
     */
144
    public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement
145
    {
146
        $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix));
147
        $boxElement = parent::toXML($document, null, 'at24-7');
148
        $nationalElement->appendChild($boxElement);
149
150
        if ($this->parcelsDepotId !== null) {
151
            $boxElement->appendChild($document->createElement('parcelsDepotId', $this->parcelsDepotId));
152
        }
153
        if ($this->parcelsDepotName !== null) {
154
            $boxElement->appendChild($document->createElement('parcelsDepotName', $this->parcelsDepotName));
155
        }
156
        if ($this->parcelsDepotAddress !== null) {
157
            $boxElement->appendChild($this->parcelsDepotAddress->toXML($document));
158
        }
159
        if ($this->memberId !== null) {
160
            $boxElement->appendChild($document->createElement('memberId', $this->memberId));
161
        }else if ($this->unregistered !== null) {
162
            $boxElement->appendChild($this->unregistered->toXML($document));
163
        }
164
165
        if ($this->receiverName !== null) {
166
            $boxElement->appendChild($document->createElement('receiverName', $this->receiverName));
167
        }
168
        if ($this->receiverCompany !== null) {
169
            $boxElement->appendChild($document->createElement('receiverCompany', $this->receiverCompany));
170
        }
171
172
        $this->addToXmlRequestedDeliveryDate($document, $boxElement, $prefix);
173
174
        return $nationalElement;
175
    }
176
177
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, ?string $prefix): void
178
    {
179
        $date = $this->requestedDeliveryDate;
180
        if ($date !== null && $date !== '') {
181
            $typeElement->appendChild(
182
                $document->createElement(XmlHelper::getPrefixedTagName('requestedDeliveryDate', $prefix), $date)
183
            );
184
        }
185
    }
186
187
    /**
188
     * @throws BpostInvalidValueException
189
     * @throws BpostNotImplementedException
190
     * @throws BpostInvalidLengthException
191
     */
192
    public static function createFromXML(SimpleXMLElement $xml, ?National $self = null): At247
193
    {
194
        $at247 = new At247();
195
196
        if (isset($xml->{'at24-7'}->product) && (string)$xml->{'at24-7'}->product !== '') {
197
            $at247->setProduct((string)$xml->{'at24-7'}->product);
198
        }
199
200
        if (isset($xml->{'at24-7'}->options)) {
201
            foreach ($xml->{'at24-7'}->options as $optionData) {
202
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
203
204
                if ($optionData->getName() === Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED) {
205
                    $option = Messaging::createFromXML($optionData);
206
                } else {
207
                    $option = self::getOptionFromOptionData($optionData);
208
                }
209
210
                $at247->addOption($option);
211
            }
212
        }
213
214
        if (isset($xml->{'at24-7'}->weight) && (string)$xml->{'at24-7'}->weight !== '') {
215
            $at247->setWeight((int)$xml->{'at24-7'}->weight);
216
        }
217
        if (isset($xml->{'at24-7'}->memberId) && (string)$xml->{'at24-7'}->memberId !== '') {
218
            $at247->setMemberId((string)$xml->{'at24-7'}->memberId);
219
        }
220
        if (isset($xml->{'at24-7'}->receiverName) && (string)$xml->{'at24-7'}->receiverName !== '') {
221
            $at247->setReceiverName((string)$xml->{'at24-7'}->receiverName);
222
        }
223
        if (isset($xml->{'at24-7'}->receiverCompany) && (string)$xml->{'at24-7'}->receiverCompany !== '') {
224
            $at247->setReceiverCompany((string)$xml->{'at24-7'}->receiverCompany);
225
        }
226
        if (isset($xml->{'at24-7'}->parcelsDepotId) && (string)$xml->{'at24-7'}->parcelsDepotId !== '') {
227
            $at247->setParcelsDepotId((string)$xml->{'at24-7'}->parcelsDepotId);
228
        }
229
        if (isset($xml->{'at24-7'}->parcelsDepotName) && (string)$xml->{'at24-7'}->parcelsDepotName !== '') {
230
            $at247->setParcelsDepotName((string)$xml->{'at24-7'}->parcelsDepotName);
231
        }
232
        if (isset($xml->{'at24-7'}->parcelsDepotAddress)) {
233
            $parcelsDepotAddressData = $xml->{'at24-7'}->parcelsDepotAddress
234
                ->children('http://schema.post.be/shm/deepintegration/v3/common');
235
            $at247->setParcelsDepotAddress(ParcelsDepotAddress::createFromXML($parcelsDepotAddressData));
236
        }
237
        if (isset($xml->{'at24-7'}->requestedDeliveryDate) && (string)$xml->{'at24-7'}->requestedDeliveryDate !== '') {
238
            $at247->setRequestedDeliveryDate((string)$xml->{'at24-7'}->requestedDeliveryDate);
239
        }
240
241
        return $at247;
242
    }
243
}
244