AtBpost::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 26
rs 9.1111
cc 6
nc 32
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order\Box;
5
6
use Bpost\BpostApiClient\Bpost\Order\Box\National\ShopHandlingInstruction;
7
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
8
use Bpost\BpostApiClient\Bpost\Order\PugoAddress;
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 AtBpost 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 AtBpost extends National
29
{
30
    protected ?string $product = Product::PRODUCT_NAME_BPACK_AT_BPOST;
31
    private ?string $pugoId = null;
32
    private ?string $pugoName = null;
33
    private ?PugoAddress $pugoAddress = null;
34
    private ?string $receiverName = null;
35
    private ?string $receiverCompany = null;
36
    protected ?string $requestedDeliveryDate = null;
37
    private ?ShopHandlingInstruction $shopHandlingInstruction = null;
38
39
    /**
40
     * @throws BpostInvalidValueException
41
     */
42
    public function setProduct(string $product): void
43
    {
44
        if (!in_array($product, self::getPossibleProductValues(), true)) {
45
            throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues());
46
        }
47
        parent::setProduct($product);
48
    }
49
50
    public static function getPossibleProductValues(): array
51
    {
52
        return [
53
            Product::PRODUCT_NAME_BPACK_AT_BPOST,
54
        ];
55
    }
56
57
    public function setPugoAddress(?PugoAddress $pugoAddress): void
58
    {
59
        $this->pugoAddress = $pugoAddress;
60
    }
61
62
    public function getPugoAddress(): ?PugoAddress
63
    {
64
        return $this->pugoAddress;
65
    }
66
67
    public function setPugoId(?string $pugoId): void
68
    {
69
        $this->pugoId = $pugoId;
70
    }
71
72
    public function getPugoId(): ?string
73
    {
74
        return $this->pugoId;
75
    }
76
77
    public function setPugoName(?string $pugoName): void
78
    {
79
        $this->pugoName = $pugoName;
80
    }
81
82
    public function getPugoName(): ?string
83
    {
84
        return $this->pugoName;
85
    }
86
87
    public function setReceiverCompany(?string $receiverCompany): void
88
    {
89
        $this->receiverCompany = $receiverCompany;
90
    }
91
92
    public function getReceiverCompany(): ?string
93
    {
94
        return $this->receiverCompany;
95
    }
96
97
    public function setReceiverName(?string $receiverName): void
98
    {
99
        $this->receiverName = $receiverName;
100
    }
101
102
    public function getReceiverName(): ?string
103
    {
104
        return $this->receiverName;
105
    }
106
107
    public function getRequestedDeliveryDate(): ?string
108
    {
109
        return $this->requestedDeliveryDate;
110
    }
111
112
    public function setRequestedDeliveryDate(?string $requestedDeliveryDate): void
113
    {
114
        $this->requestedDeliveryDate = $requestedDeliveryDate;
115
    }
116
117
    public function getShopHandlingInstruction(): ?string
118
    {
119
        return $this->shopHandlingInstruction?->getValue();
120
    }
121
122
    public function setShopHandlingInstruction(?string $shopHandlingInstruction): void
123
    {
124
        $this->shopHandlingInstruction = $shopHandlingInstruction !== null
125
            ? new ShopHandlingInstruction($shopHandlingInstruction)
126
            : null;
127
    }
128
129
    /**
130
     * @throws \DOMException
131
     */
132
    public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement
133
    {
134
        $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix));
135
        $boxElement = parent::toXML($document, null, 'atBpost');
136
        $nationalElement->appendChild($boxElement);
137
138
        if ($this->pugoId !== null) {
139
            $boxElement->appendChild($document->createElement('pugoId', $this->pugoId));
140
        }
141
        if ($this->pugoName !== null) {
142
            $boxElement->appendChild($document->createElement('pugoName', $this->pugoName));
143
        }
144
        if ($this->pugoAddress !== null) {
145
            $boxElement->appendChild($this->pugoAddress->toXML($document));
146
        }
147
        if ($this->receiverName !== null) {
148
            $boxElement->appendChild($document->createElement('receiverName', $this->receiverName));
149
        }
150
        if ($this->receiverCompany !== null) {
151
            $boxElement->appendChild($document->createElement('receiverCompany', $this->receiverCompany));
152
        }
153
154
        $this->addToXmlRequestedDeliveryDate($document, $boxElement, $prefix);
155
        $this->addToXmlShopHandlingInstruction($document, $boxElement, $prefix);
156
157
        return $nationalElement;
158
    }
159
160
    /**
161
     * @throws \DOMException
162
     */
163
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, ?string $prefix): void
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed. ( Ignorable by Annotation )

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

163
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, /** @scrutinizer ignore-unused */ ?string $prefix): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        if ($this->requestedDeliveryDate !== null) {
166
            $typeElement->appendChild(
167
                $document->createElement('requestedDeliveryDate', $this->requestedDeliveryDate)
168
            );
169
        }
170
    }
171
172
    private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, ?string $prefix): void
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed. ( Ignorable by Annotation )

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

172
    private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, /** @scrutinizer ignore-unused */ ?string $prefix): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
        $value = $this->getShopHandlingInstruction();
175
        if ($value !== null) {
176
            $typeElement->appendChild($document->createElement('shopHandlingInstruction', $value));
177
        }
178
    }
179
180
    /**
181
     * @throws BpostInvalidValueException
182
     * @throws BpostNotImplementedException
183
     * @throws BpostInvalidLengthException
184
     */
185
    public static function createFromXML(SimpleXMLElement $xml, National $self = null): AtBpost
186
    {
187
        $atBpost = new AtBpost();
188
189
        if (isset($xml->atBpost->product) && (string)$xml->atBpost->product !== '') {
190
            $atBpost->setProduct((string)$xml->atBpost->product);
191
        }
192
193
        if (isset($xml->atBpost->options)) {
194
            foreach ($xml->atBpost->options as $optionData) {
195
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
0 ignored issues
show
Bug introduced by
The method children() does not exist on null. ( Ignorable by Annotation )

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

195
                /** @scrutinizer ignore-call */ 
196
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
197
                if (in_array(
198
                    $optionData->getName(),
199
                    [
200
                        Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
201
                        Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
202
                        Messaging::MESSAGING_TYPE_INFO_REMINDER,
203
                        Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
204
                    ],
205
                    true
206
                )) {
207
                    $option = Messaging::createFromXML($optionData);
208
                } else {
209
                    $option = self::getOptionFromOptionData($optionData);
210
                }
211
212
                $atBpost->addOption($option);
213
            }
214
        }
215
216
        if (isset($xml->atBpost->weight) && (string)$xml->atBpost->weight !== '') {
217
            $atBpost->setWeight((int)$xml->atBpost->weight);
218
        }
219
        if (isset($xml->atBpost->receiverName) && (string)$xml->atBpost->receiverName !== '') {
220
            $atBpost->setReceiverName((string)$xml->atBpost->receiverName);
221
        }
222
        if (isset($xml->atBpost->receiverCompany) && (string)$xml->atBpost->receiverCompany !== '') {
223
            $atBpost->setReceiverCompany((string)$xml->atBpost->receiverCompany);
224
        }
225
        if (isset($xml->atBpost->pugoId) && (string)$xml->atBpost->pugoId !== '') {
226
            $atBpost->setPugoId((string)$xml->atBpost->pugoId);
227
        }
228
        if (isset($xml->atBpost->pugoName) && (string)$xml->atBpost->pugoName !== '') {
229
            $atBpost->setPugoName((string)$xml->atBpost->pugoName);
230
        }
231
        if (isset($xml->atBpost->pugoAddress)) {
232
            $pugoAddressData = $xml->atBpost->pugoAddress
233
                ->children('http://schema.post.be/shm/deepintegration/v3/common');
234
            $atBpost->setPugoAddress(PugoAddress::createFromXML($pugoAddressData));
235
        }
236
        if (isset($xml->atBpost->requestedDeliveryDate) && (string)$xml->atBpost->requestedDeliveryDate !== '') {
237
            $atBpost->setRequestedDeliveryDate((string)$xml->atBpost->requestedDeliveryDate);
238
        }
239
        if (isset($xml->atBpost->shopHandlingInstruction) && (string)$xml->atBpost->shopHandlingInstruction !== '') {
240
            $atBpost->setShopHandlingInstruction((string)$xml->atBpost->shopHandlingInstruction);
241
        }
242
243
        return $atBpost;
244
    }
245
}