CustomsInfo   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 452
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 52
eloc 134
dl 0
loc 452
rs 7.44
c 2
b 1
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A currencyToXML() 0 7 2
A shipmentTypeToXML() 0 7 2
A getAmtPostagePaidByAddresse() 0 3 1
A contentDescriptionToXML() 0 7 2
A getParcelValue() 0 3 1
A parcelReturnInstructionValuesToXML() 0 7 2
A toXML() 0 13 1
A setCurrency() 0 6 2
A getCurrency() 0 3 1
A setContentDescription() 0 8 2
A getPossibleParcelReturnInstructionValues() 0 6 1
A getPossibleShipmentTypeValues() 0 8 1
A setShipmentType() 0 9 2
A parcelValueToXML() 0 7 2
A setParcelValue() 0 3 1
A amtPostagePaidByAddresseToXML() 0 7 2
A getPossibleCurrencyValues() 0 7 1
A privateAddressToXML() 0 12 3
A getContentDescription() 0 3 1
C createFromXML() 0 41 15
A getShipmentType() 0 3 1
A setPrivateAddress() 0 3 1
A getPrivateAddress() 0 3 1
A setParcelReturnInstructions() 0 13 2
A getParcelReturnInstructions() 0 3 1
A setAmtPostagePaidByAddresse() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like CustomsInfo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CustomsInfo, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo;
4
5
use Bpost\BpostApiClient\Common\XmlHelper;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
8
use DOMDocument;
9
use DOMElement;
10
use DOMException;
11
use SimpleXMLElement;
12
13
/**
14
 * bPost CustomsInfo class
15
 *
16
 * @author    Tijs Verkoyen <[email protected]>
17
 *
18
 * @version   3.0.0
19
 *
20
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
21
 * @license   BSD License
22
 */
23
class CustomsInfo
24
{
25
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA = 'RTA';
26
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS = 'RTS';
27
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED = 'ABANDONED';
28
29
    const CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE = 'SAMPLE';
30
    const CUSTOM_INFO_SHIPMENT_TYPE_GIFT = 'GIFT';
31
    const CUSTOM_INFO_SHIPMENT_TYPE_GOODS = 'GOODS';
32
    const CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS = 'DOCUMENTS';
33
    const CUSTOM_INFO_SHIPMENT_TYPE_OTHER = 'OTHER';
34
35
    const CUSTOM_INFO_CURRENCY_EUR = 'EUR';
36
    const CUSTOM_INFO_CURRENCY_GBP = 'GBP';
37
    const CUSTOM_INFO_CURRENCY_USD = 'USD';
38
    const CUSTOM_INFO_CURRENCY_CNY = 'CNY';
39
40
    /**
41
     * @var int
42
     */
43
    private $parcelValue;
44
45
    /**
46
     * @var string
47
     */
48
    private $contentDescription;
49
50
    /**
51
     * @var string
52
     */
53
    private $shipmentType;
54
55
    /**
56
     * @var string
57
     */
58
    private $parcelReturnInstructions;
59
60
    /**
61
     * @var bool
62
     */
63
    private $privateAddress;
64
65
    /**
66
     * this is the currency used for field parcelValue.In case of shipment to non-European country,
67
     * this is also the currency used for all parcel contents value (field valueOfitem) in 3 letters format.
68
     *
69
     * Possible values are: EUR=Euro    GBP=Pound   Sterling    USD=US Dollar   CNY=Yuan Renminbi
70
     *
71
     * @var string
72
     */
73
    private $currency;
74
75
    /**
76
     * Amount paid by the sender for the sending of this shipment. See contract pricing with bpost.
77
     * Decimal format field (3.2)
78
     * Minimum value : 0
79
     * Maximum value : 999.99
80
     * Currency for field amtPostagePaidByAddresse is always EUR !
81
     *
82
     * @var float
83
     */
84
    private $amtPostagePaidByAddresse;
85
86
    /**
87
     * @param string $contentDescription
88
     *
89
     * @throws BpostInvalidLengthException
90
     */
91
    public function setContentDescription($contentDescription)
92
    {
93
        $length = 50;
94
        if (mb_strlen($contentDescription) > $length) {
95
            throw new BpostInvalidLengthException('contentDescription', mb_strlen($contentDescription), $length);
96
        }
97
98
        $this->contentDescription = $contentDescription;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getContentDescription()
105
    {
106
        return $this->contentDescription;
107
    }
108
109
    /**
110
     * @param string $parcelReturnInstructions
111
     *
112
     * @throws BpostInvalidValueException
113
     */
114
    public function setParcelReturnInstructions($parcelReturnInstructions)
115
    {
116
        $parcelReturnInstructions = strtoupper($parcelReturnInstructions);
117
118
        if (!in_array($parcelReturnInstructions, self::getPossibleParcelReturnInstructionValues())) {
119
            throw new BpostInvalidValueException(
120
                'parcelReturnInstructions',
121
                $parcelReturnInstructions,
122
                self::getPossibleParcelReturnInstructionValues()
123
            );
124
        }
125
126
        $this->parcelReturnInstructions = $parcelReturnInstructions;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getParcelReturnInstructions()
133
    {
134
        return $this->parcelReturnInstructions;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public static function getPossibleParcelReturnInstructionValues()
141
    {
142
        return array(
143
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA,
144
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS,
145
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED,
146
        );
147
    }
148
149
    /**
150
     * @param int $parcelValue
151
     */
152
    public function setParcelValue($parcelValue)
153
    {
154
        $this->parcelValue = $parcelValue;
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getParcelValue()
161
    {
162
        return $this->parcelValue;
163
    }
164
165
    /**
166
     * @param bool $privateAddress
167
     */
168
    public function setPrivateAddress($privateAddress)
169
    {
170
        $this->privateAddress = $privateAddress;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function getPrivateAddress()
177
    {
178
        return $this->privateAddress;
179
    }
180
181
    /**
182
     * @param string $shipmentType
183
     *
184
     * @throws BpostInvalidValueException
185
     */
186
    public function setShipmentType($shipmentType)
187
    {
188
        $shipmentType = strtoupper($shipmentType);
189
190
        if (!in_array($shipmentType, self::getPossibleShipmentTypeValues())) {
191
            throw new BpostInvalidValueException('shipmentType', $shipmentType, self::getPossibleShipmentTypeValues());
192
        }
193
194
        $this->shipmentType = $shipmentType;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getShipmentType()
201
    {
202
        return $this->shipmentType;
203
    }
204
205
    /**
206
     * @return array
207
     */
208
    public static function getPossibleShipmentTypeValues()
209
    {
210
        return array(
211
            self::CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE,
212
            self::CUSTOM_INFO_SHIPMENT_TYPE_GIFT,
213
            self::CUSTOM_INFO_SHIPMENT_TYPE_GOODS,
214
            self::CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS,
215
            self::CUSTOM_INFO_SHIPMENT_TYPE_OTHER,
216
        );
217
    }
218
219
    /**
220
     * @return float
221
     */
222
    public function getAmtPostagePaidByAddresse()
223
    {
224
        return $this->amtPostagePaidByAddresse;
225
    }
226
227
    /**
228
     * @param float $amtPostagePaidByAddresse
229
     */
230
    public function setAmtPostagePaidByAddresse($amtPostagePaidByAddresse)
231
    {
232
        $this->amtPostagePaidByAddresse = $amtPostagePaidByAddresse;
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getCurrency()
239
    {
240
        return $this->currency;
241
    }
242
243
    /**
244
     * @param string $currency
245
     *
246
     * @throws BpostInvalidValueException
247
     */
248
    public function setCurrency($currency)
249
    {
250
        if (!in_array($currency, self::getPossibleCurrencyValues())) {
251
            throw new BpostInvalidValueException('currency', $currency, self::getPossibleCurrencyValues());
252
        }
253
        $this->currency = $currency;
254
    }
255
256
    public static function getPossibleCurrencyValues()
257
    {
258
        return array(
259
            self::CUSTOM_INFO_CURRENCY_EUR,
260
            self::CUSTOM_INFO_CURRENCY_GBP,
261
            self::CUSTOM_INFO_CURRENCY_USD,
262
            self::CUSTOM_INFO_CURRENCY_CNY,
263
        );
264
    }
265
266
    /**
267
     * Return the object as an array for usage in the XML
268
     *
269
     * @param DomDocument $document
270
     * @param string      $prefix
271
     *
272
     * @return DomElement
273
     *
274
     * @throws DOMException
275
     */
276
    public function toXML(DOMDocument $document, $prefix = null)
277
    {
278
        $customsInfo = $document->createElement(XmlHelper::getPrefixedTagName('customsInfo', $prefix));
279
280
        $this->parcelValueToXML($document, $prefix, $customsInfo);
281
        $this->contentDescriptionToXML($document, $prefix, $customsInfo);
282
        $this->shipmentTypeToXML($document, $prefix, $customsInfo);
283
        $this->parcelReturnInstructionValuesToXML($document, $prefix, $customsInfo);
284
        $this->privateAddressToXML($document, $prefix, $customsInfo);
285
        $this->currencyToXML($document, $prefix, $customsInfo);
286
        $this->amtPostagePaidByAddresseToXML($document, $prefix, $customsInfo);
287
288
        return $customsInfo;
289
    }
290
291
    /**
292
     * @param SimpleXMLElement $xml
293
     *
294
     * @return CustomsInfo
295
     *
296
     * @throws BpostInvalidLengthException
297
     * @throws BpostInvalidValueException
298
     */
299
    public static function createFromXML(SimpleXMLElement $xml)
300
    {
301
        $customsInfo = new CustomsInfo();
302
303
        if (isset($xml->parcelValue) && $xml->parcelValue != '') {
304
            $customsInfo->setParcelValue(
305
                (int) $xml->parcelValue
306
            );
307
        }
308
        if (isset($xml->contentDescription) && $xml->contentDescription != '') {
309
            $customsInfo->setContentDescription(
310
                (string) $xml->contentDescription
311
            );
312
        }
313
        if (isset($xml->shipmentType) && $xml->shipmentType != '') {
314
            $customsInfo->setShipmentType(
315
                (string) $xml->shipmentType
316
            );
317
        }
318
        if (isset($xml->parcelReturnInstructions) && $xml->parcelReturnInstructions != '') {
319
            $customsInfo->setParcelReturnInstructions(
320
                (string) $xml->parcelReturnInstructions
321
            );
322
        }
323
        if (isset($xml->privateAddress) && $xml->privateAddress != '') {
324
            $customsInfo->setPrivateAddress(
325
                (string) $xml->privateAddress == 'true'
326
            );
327
        }
328
        if (isset($xml->currency) && $xml->currency != '') {
329
            $customsInfo->setCurrency(
330
                (string) $xml->currency
331
            );
332
        }
333
        if (isset($xml->amtPostagePaidByAddresse) && $xml->amtPostagePaidByAddresse != '') {
334
            $customsInfo->setAmtPostagePaidByAddresse(
335
                (float) $xml->amtPostagePaidByAddresse
336
            );
337
        }
338
339
        return $customsInfo;
340
    }
341
342
    /**
343
     * @param DOMDocument $document
344
     * @param string      $prefix
345
     * @param DOMElement  $customsInfo
346
     *
347
     * @throws DOMException
348
     */
349
    private function parcelValueToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
350
    {
351
        if ($this->getParcelValue() !== null) {
0 ignored issues
show
introduced by
The condition $this->getParcelValue() !== null is always true.
Loading history...
352
            $customsInfo->appendChild(
353
                $document->createElement(
354
                    XmlHelper::getPrefixedTagName('parcelValue', $prefix),
355
                    $this->getParcelValue()
356
                )
357
            );
358
        }
359
    }
360
361
    /**
362
     * @param DOMDocument $document
363
     * @param string      $prefix
364
     * @param DOMElement  $customsInfo
365
     *
366
     * @throws DOMException
367
     */
368
    private function currencyToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
369
    {
370
        if ($this->getCurrency() !== null) {
0 ignored issues
show
introduced by
The condition $this->getCurrency() !== null is always true.
Loading history...
371
            $customsInfo->appendChild(
372
                $document->createElement(
373
                    XmlHelper::getPrefixedTagName('currency', $prefix),
374
                    $this->getCurrency()
375
                )
376
            );
377
        }
378
    }
379
380
    /**
381
     * @param DOMDocument $document
382
     * @param string      $prefix
383
     * @param DOMElement  $customsInfo
384
     *
385
     * @throws DOMException
386
     */
387
    private function amtPostagePaidByAddresseToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
388
    {
389
        if ($this->getAmtPostagePaidByAddresse() !== null) {
0 ignored issues
show
introduced by
The condition $this->getAmtPostagePaidByAddresse() !== null is always true.
Loading history...
390
            $customsInfo->appendChild(
391
                $document->createElement(
392
                    XmlHelper::getPrefixedTagName('amtPostagePaidByAddresse', $prefix),
393
                    sprintf('%0.2f', $this->getAmtPostagePaidByAddresse())
394
                )
395
            );
396
        }
397
    }
398
399
    /**
400
     * @param DOMDocument $document
401
     * @param string      $prefix
402
     * @param DOMElement  $customsInfo
403
     *
404
     * @throws DOMException
405
     */
406
    private function contentDescriptionToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
407
    {
408
        if ($this->getContentDescription() !== null) {
0 ignored issues
show
introduced by
The condition $this->getContentDescription() !== null is always true.
Loading history...
409
            $customsInfo->appendChild(
410
                $document->createElement(
411
                    XmlHelper::getPrefixedTagName('contentDescription', $prefix),
412
                    $this->getContentDescription()
413
                )
414
            );
415
        }
416
    }
417
418
    /**
419
     * @param DOMDocument $document
420
     * @param string      $prefix
421
     * @param DOMElement  $customsInfo
422
     *
423
     * @throws DOMException
424
     */
425
    private function shipmentTypeToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
426
    {
427
        if ($this->getShipmentType() !== null) {
0 ignored issues
show
introduced by
The condition $this->getShipmentType() !== null is always true.
Loading history...
428
            $customsInfo->appendChild(
429
                $document->createElement(
430
                    XmlHelper::getPrefixedTagName('shipmentType', $prefix),
431
                    $this->getShipmentType()
432
                )
433
            );
434
        }
435
    }
436
437
    /**
438
     * @param DOMDocument $document
439
     * @param string      $prefix
440
     * @param DOMElement  $customsInfo
441
     *
442
     * @throws DOMException
443
     */
444
    private function parcelReturnInstructionValuesToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
445
    {
446
        if ($this->getParcelReturnInstructions() !== null) {
0 ignored issues
show
introduced by
The condition $this->getParcelReturnInstructions() !== null is always true.
Loading history...
447
            $customsInfo->appendChild(
448
                $document->createElement(
449
                    XmlHelper::getPrefixedTagName('parcelReturnInstructions', $prefix),
450
                    $this->getParcelReturnInstructions()
451
                )
452
            );
453
        }
454
    }
455
456
    /**
457
     * @param DOMDocument $document
458
     * @param string      $prefix
459
     * @param DOMElement  $customsInfo
460
     *
461
     * @throws DOMException
462
     */
463
    private function privateAddressToXML(DOMDocument $document, $prefix, DOMElement $customsInfo)
464
    {
465
        if ($this->getPrivateAddress() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPrivateAddress() !== null is always true.
Loading history...
466
            if ($this->getPrivateAddress()) {
467
                $value = 'true';
468
            } else {
469
                $value = 'false';
470
            }
471
            $customsInfo->appendChild(
472
                $document->createElement(
473
                    XmlHelper::getPrefixedTagName('privateAddress', $prefix),
474
                    $value
475
                )
476
            );
477
        }
478
    }
479
}
480