Box::getSender()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order;
5
6
use Bpost\BpostApiClient\Bpost\Order\Box\International;
7
use Bpost\BpostApiClient\Bpost\Order\Box\National;
8
use Bpost\BpostApiClient\Common\XmlHelper;
9
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
10
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
11
use DOMDocument;
12
use DOMElement;
13
use SimpleXMLElement;
14
15
/**
16
 * bPost Box class
17
 *
18
 * @author Tijs Verkoyen <[email protected]>
19
 */
20
class Box
21
{
22
    public const BOX_STATUS_OPEN             = 'OPEN';
23
    public const BOX_STATUS_PENDING          = 'PENDING';
24
    public const BOX_STATUS_PRINTED          = 'PRINTED';
25
    public const BOX_STATUS_CANCELLED        = 'CANCELLED';
26
    public const BOX_STATUS_ON_HOLD          = 'ON-HOLD';
27
    public const BOX_STATUS_ANNOUNCED        = 'ANNOUNCED';
28
    public const BOX_STATUS_IN_TRANSIT       = 'IN_TRANSIT';
29
    public const BOX_STATUS_AWAITING_PICKUP  = 'AWAITING_PICKUP';
30
    public const BOX_STATUS_DELIVERED        = 'DELIVERED';
31
    public const BOX_STATUS_BACK_TO_SENDER   = 'BACK_TO_SENDER';
32
33
    private ?Sender $sender = null;
34
    private ?National $nationalBox = null;
35
    private ?International $internationalBox = null;
36
37
    private ?string $remark = null;
38
    private ?string $status = null;
39
    private ?string $barcode = null;
40
    private ?string $additionalCustomerReference = null;
41
42
    public function setInternationalBox(International $internationalBox): void
43
    {
44
        $this->internationalBox = $internationalBox;
45
    }
46
47
    public function getInternationalBox(): ?International
48
    {
49
        return $this->internationalBox;
50
    }
51
52
    public function setNationalBox(National $nationalBox): void
53
    {
54
        $this->nationalBox = $nationalBox;
55
    }
56
57
    public function getNationalBox(): ?National
58
    {
59
        return $this->nationalBox;
60
    }
61
62
    public function setRemark(string $remark): void
63
    {
64
        $this->remark = $remark;
65
    }
66
67
    public function getRemark(): ?string
68
    {
69
        return $this->remark;
70
    }
71
72
    public function setSender(Sender $sender): void
73
    {
74
        $this->sender = $sender;
75
    }
76
77
    public function getSender(): ?Sender
78
    {
79
        return $this->sender;
80
    }
81
82
    /**
83
     * @throws BpostInvalidValueException
84
     */
85
    public function setStatus(string $status): void
86
    {
87
        $status = strtoupper($status);
88
        if (!in_array($status, self::getPossibleStatusValues(), true)) {
89
            throw new BpostInvalidValueException('status', $status, self::getPossibleStatusValues());
90
        }
91
        $this->status = $status;
92
    }
93
94
    public function setBarcode(string $barcode): void
95
    {
96
        $this->barcode = strtoupper($barcode);
97
    }
98
99
    public function getBarcode(): ?string
100
    {
101
        return $this->barcode;
102
    }
103
104
    public function getStatus(): ?string
105
    {
106
        return $this->status;
107
    }
108
109
    public function setAdditionalCustomerReference(string $additionalCustomerReference): void
110
    {
111
        $this->additionalCustomerReference = $additionalCustomerReference;
112
    }
113
114
    public function getAdditionalCustomerReference(): ?string
115
    {
116
        return $this->additionalCustomerReference;
117
    }
118
119
120
    public static function getPossibleStatusValues(): array
121
    {
122
        return [
123
            self::BOX_STATUS_OPEN,
124
            self::BOX_STATUS_PENDING,
125
            self::BOX_STATUS_PRINTED,
126
            self::BOX_STATUS_CANCELLED,
127
            self::BOX_STATUS_ON_HOLD,
128
            self::BOX_STATUS_ANNOUNCED,
129
            self::BOX_STATUS_IN_TRANSIT,
130
            self::BOX_STATUS_AWAITING_PICKUP,
131
            self::BOX_STATUS_DELIVERED,
132
            self::BOX_STATUS_BACK_TO_SENDER,
133
        ];
134
    }
135
136
    /**
137
     * @throws \DOMException
138
     */
139
    public function toXML(DOMDocument $document, ?string $prefix = null): DOMElement
140
    {
141
        $box = $document->createElement(XmlHelper::getPrefixedTagName('box', $prefix));
142
143
        $this->senderToXML($document, $prefix, $box);
144
        $this->boxToXML($document, $prefix, $box);
145
        $this->remarkToXML($document, $prefix, $box);
146
        $this->additionalCustomerReferenceToXML($document, $prefix, $box);
147
        $this->barcodeToXML($document, $prefix, $box);
148
149
        return $box;
150
    }
151
152
    /**
153
     * @throws BpostInvalidValueException
154
     * @throws BpostNotImplementedException
155
     */
156
    public static function createFromXML(SimpleXMLElement $xml): self
157
    {
158
        $box = new self();
159
160
        if (isset($xml->sender)) {
161
            $box->setSender(
162
                Sender::createFromXML(
163
                    $xml->sender->children('http://schema.post.be/shm/deepintegration/v3/common')
164
                )
165
            );
166
        }
167
168
        if (isset($xml->nationalBox)) {
169
            $nationalBoxData = $xml->nationalBox->children('http://schema.post.be/shm/deepintegration/v3/national');
170
            $classNameExtracted = $nationalBoxData->getName();
171
            if ($classNameExtracted === 'at24-7') {
172
                $classNameExtracted = 'at247';
173
            }
174
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst((string)$classNameExtracted);
175
            XmlHelper::assertMethodCreateFromXmlExists($className);
176
177
            /** @var National $nationalBox */
178
            $nationalBox = call_user_func([$className, 'createFromXML'], $nationalBoxData);
179
            $box->setNationalBox($nationalBox);
180
        }
181
182
        if (isset($xml->internationalBox)) {
183
            $internationalBoxData = $xml->internationalBox->children('http://schema.post.be/shm/deepintegration/v3/international');
184
            $classNameExtracted = $internationalBoxData->getName();
185
            if ($classNameExtracted === 'atIntlHome') {
186
                $classNameExtracted = 'international';
187
            }
188
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst((string)$classNameExtracted);
189
            XmlHelper::assertMethodCreateFromXmlExists($className);
190
191
            /** @var International $internationalBox */
192
            $internationalBox = call_user_func([$className, 'createFromXML'], $internationalBoxData);
193
            $box->setInternationalBox($internationalBox);
194
        }
195
196
        if (isset($xml->remark) && (string) $xml->remark !== '') {
197
            $box->setRemark((string)$xml->remark);
198
        }
199
        if (isset($xml->additionalCustomerReference) && (string) $xml->additionalCustomerReference !== '') {
200
            $box->setAdditionalCustomerReference((string)$xml->additionalCustomerReference);
201
        }
202
        if (!empty($xml->barcode)) {
203
            $box->setBarcode((string)$xml->barcode);
204
        }
205
        if (isset($xml->status) && (string) $xml->status !== '') {
206
            $box->setStatus((string)$xml->status);
207
        }
208
209
        return $box;
210
    }
211
212
    /**
213
     * @throws \DOMException
214
     */
215
    private function barcodeToXML(DOMDocument $document, ?string $prefix, DOMElement $box): void
216
    {
217
        if ($this->barcode !== null) {
218
            $box->appendChild(
219
                $document->createElement(XmlHelper::getPrefixedTagName('barcode', $prefix), $this->barcode)
220
            );
221
        }
222
    }
223
224
    /**
225
     * @throws \DOMException
226
     */
227
    private function boxToXML(DOMDocument $document, ?string $prefix, DOMElement $box): void
228
    {
229
        if ($this->nationalBox !== null) {
230
            $box->appendChild($this->nationalBox->toXML($document, $prefix));
231
        }
232
        if ($this->internationalBox !== null) {
233
            $box->appendChild($this->internationalBox->toXML($document, $prefix));
234
        }
235
    }
236
237
    private function senderToXML(DOMDocument $document, ?string $prefix, DOMElement $box): void
238
    {
239
        if ($this->sender !== null) {
240
            $box->appendChild($this->sender->toXML($document, $prefix));
241
        }
242
    }
243
244
    /**
245
     * @throws \DOMException
246
     */
247
    private function remarkToXML(DOMDocument $document, ?string $prefix, DOMElement $box): void
248
    {
249
        if ($this->remark !== null) {
250
            $box->appendChild(
251
                $document->createElement(XmlHelper::getPrefixedTagName('remark', $prefix), $this->remark)
252
            );
253
        }
254
    }
255
256
    private function additionalCustomerReferenceToXML(DOMDocument $document, ?string $prefix, DOMElement $box): void
257
    {
258
        if ($this->additionalCustomerReference !== null) {
259
            $box->appendChild(
260
                $document->createElement(
261
                    XmlHelper::getPrefixedTagName('additionalCustomerReference', $prefix),
262
                    $this->additionalCustomerReference
263
                )
264
            );
265
        }
266
    }
267
}