CustomsInfo::toXML()   F
last analyzed

Complexity

Conditions 13
Paths 810

Size

Total Lines 77
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 77
rs 2.5974
cc 13
eloc 50
nc 810
nop 2

How to fix   Long Method    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
3
namespace Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo;
4
5
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
6
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
7
8
/**
9
 * bPost CustomsInfo class
10
 *
11
 * @author    Tijs Verkoyen <[email protected]>
12
 * @version   3.0.0
13
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
14
 * @license   BSD License
15
 */
16
class CustomsInfo
17
{
18
19
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA = 'RTA';
20
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS = 'RTS';
21
    const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED = 'ABANDONED';
22
23
    const CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE = 'SAMPLE';
24
    const CUSTOM_INFO_SHIPMENT_TYPE_GIFT = 'GIFT';
25
    const CUSTOM_INFO_SHIPMENT_TYPE_GOODS = 'GOODS';
26
    const CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS = 'DOCUMENTS';
27
    const CUSTOM_INFO_SHIPMENT_TYPE_OTHER = 'OTHER';
28
29
    /**
30
     * @var int
31
     */
32
    private $parcelValue;
33
34
    /**
35
     * @var string
36
     */
37
    private $contentDescription;
38
39
    /**
40
     * @var string
41
     */
42
    private $shipmentType;
43
44
    /**
45
     * @var string
46
     */
47
    private $parcelReturnInstructions;
48
49
    /**
50
     * @var bool
51
     */
52
    private $privateAddress;
53
54
    /**
55
     * @param string $contentDescription
56
     * @throws BpostInvalidLengthException
57 4
     */
58 View Code Duplication
    public function setContentDescription($contentDescription)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59 4
    {
60 4
        $length = 50;
61 1
        if (mb_strlen($contentDescription) > $length) {
62
            throw new BpostInvalidLengthException('contentDescription', mb_strlen($contentDescription), $length);
63
        }
64 3
65 3
        $this->contentDescription = $contentDescription;
66
    }
67
68
    /**
69
     * @return string
70 3
     */
71
    public function getContentDescription()
72 3
    {
73
        return $this->contentDescription;
74
    }
75
76
    /**
77
     * @param string $parcelReturnInstructions
78
     * @throws BpostInvalidValueException
79 3
     */
80 View Code Duplication
    public function setParcelReturnInstructions($parcelReturnInstructions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 3
    {
82
        $parcelReturnInstructions = strtoupper($parcelReturnInstructions);
83 3
84
        if (!in_array($parcelReturnInstructions, self::getPossibleParcelReturnInstructionValues())) {
85
            throw new BpostInvalidValueException(
86
                'parcelReturnInstructions',
87
                $parcelReturnInstructions,
88
                self::getPossibleParcelReturnInstructionValues()
89
            );
90
        }
91 3
92 3
        $this->parcelReturnInstructions = $parcelReturnInstructions;
93
    }
94
95
    /**
96
     * @return string
97 3
     */
98
    public function getParcelReturnInstructions()
99 3
    {
100
        return $this->parcelReturnInstructions;
101
    }
102
103
    /**
104
     * @return array
105 3
     */
106
    public static function getPossibleParcelReturnInstructionValues()
107
    {
108 3
        return array(
109 3
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA,
110 3
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS,
111 3
            self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED,
112
        );
113
    }
114
115
    /**
116
     * @param int $parcelValue
117 3
     */
118
    public function setParcelValue($parcelValue)
119 3
    {
120 3
        $this->parcelValue = $parcelValue;
121
    }
122
123
    /**
124
     * @return int
125 3
     */
126
    public function getParcelValue()
127 3
    {
128
        return $this->parcelValue;
129
    }
130
131
    /**
132
     * @param boolean $privateAddress
133 3
     */
134
    public function setPrivateAddress($privateAddress)
135 3
    {
136 3
        $this->privateAddress = $privateAddress;
137
    }
138
139
    /**
140
     * @return boolean
141 3
     */
142
    public function getPrivateAddress()
143 3
    {
144
        return $this->privateAddress;
145
    }
146
147
    /**
148
     * @param string $shipmentType
149
     * @throws BpostInvalidValueException
150 4
     */
151 View Code Duplication
    public function setShipmentType($shipmentType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152 4
    {
153
        $shipmentType = strtoupper($shipmentType);
154 4
155 1
        if (!in_array($shipmentType, self::getPossibleShipmentTypeValues())) {
156
            throw new BpostInvalidValueException('shipmentType', $shipmentType, self::getPossibleShipmentTypeValues());
157
        }
158 3
159 3
        $this->shipmentType = $shipmentType;
160
    }
161
162
    /**
163
     * @return string
164 3
     */
165
    public function getShipmentType()
166 3
    {
167
        return $this->shipmentType;
168
    }
169
170
    /**
171
     * @return array
172 4
     */
173
    public static function getPossibleShipmentTypeValues()
174
    {
175 4
        return array(
176 4
            self::CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE,
177 4
            self::CUSTOM_INFO_SHIPMENT_TYPE_GIFT,
178 4
            self::CUSTOM_INFO_SHIPMENT_TYPE_GOODS,
179 4
            self::CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS,
180 4
            self::CUSTOM_INFO_SHIPMENT_TYPE_OTHER,
181
        );
182
    }
183
184
    /**
185
     * Return the object as an array for usage in the XML
186
     *
187
     * @param  \DomDocument $document
188
     * @param  string       $prefix
0 ignored issues
show
Documentation introduced by
Should the type for parameter $prefix not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
189
     * @return \DomElement
190 2
     */
191
    public function toXML(\DOMDocument $document, $prefix = null)
192 2
    {
193 2
        $tagName = 'customsInfo';
194 1
        if ($prefix !== null) {
195 1
            $tagName = $prefix . ':' . $tagName;
196
        }
197 2
198
        $customsInfo = $document->createElement($tagName);
199 2
200 2
        if ($this->getParcelValue() !== null) {
201 2
            $tagName = 'parcelValue';
202 1
            if ($prefix !== null) {
203 1
                $tagName = $prefix . ':' . $tagName;
204 2
            }
205 2
            $customsInfo->appendChild(
206 2
                $document->createElement(
207 2
                    $tagName,
208 2
                    $this->getParcelValue()
209 2
                )
210 2
            );
211 2
        }
212 2
        if ($this->getContentDescription() !== null) {
213 2
            $tagName = 'contentDescription';
214 1
            if ($prefix !== null) {
215 1
                $tagName = $prefix . ':' . $tagName;
216 2
            }
217 2
            $customsInfo->appendChild(
218 2
                $document->createElement(
219 2
                    $tagName,
220 2
                    $this->getContentDescription()
221 2
                )
222 2
            );
223 2
        }
224 2
        if ($this->getShipmentType() !== null) {
225 2
            $tagName = 'shipmentType';
226 1
            if ($prefix !== null) {
227 1
                $tagName = $prefix . ':' . $tagName;
228 2
            }
229 2
            $customsInfo->appendChild(
230 2
                $document->createElement(
231 2
                    $tagName,
232 2
                    $this->getShipmentType()
233 2
                )
234 2
            );
235 2
        }
236 2
        if ($this->getPossibleParcelReturnInstructionValues() !== null) {
237 2
            $tagName = 'parcelReturnInstructions';
238 1
            if ($prefix !== null) {
239 1
                $tagName = $prefix . ':' . $tagName;
240 2
            }
241 2
            $customsInfo->appendChild(
242 2
                $document->createElement(
243 2
                    $tagName,
244 2
                    $this->getParcelReturnInstructions()
245 2
                )
246 2
            );
247 2
        }
248 2
        if ($this->getPrivateAddress() !== null) {
249 2
            $tagName = 'privateAddress';
250 1
            if ($prefix !== null) {
251 1
                $tagName = $prefix . ':' . $tagName;
252 2
            }
253 1
            if ($this->getPrivateAddress()) {
254 1
                $value = 'true';
255 2
            } else {
256
                $value = 'false';
257 2
            }
258 2
            $customsInfo->appendChild(
259 2
                $document->createElement(
260
                    $tagName,
261 2
                    $value
262 2
                )
263 2
            );
264
        }
265 2
266
        return $customsInfo;
267
    }
268
269
    /**
270
     * @param  \SimpleXMLElement $xml
271
     *
272
     * @return CustomsInfo
273
     * @throws BpostInvalidLengthException
274
     * @throws BpostInvalidValueException
275 1
     */
276
    public static function createFromXML(\SimpleXMLElement $xml)
277 1
    {
278
        $customsInfo = new CustomsInfo();
279 1
280 1
        if (isset($xml->parcelValue) && $xml->parcelValue != '') {
281 1
            $customsInfo->setParcelValue(
282 1
                (int) $xml->parcelValue
283 1
            );
284 1
        }
285 1
        if (isset($xml->contentDescription) && $xml->contentDescription != '') {
286 1
            $customsInfo->setContentDescription(
287 1
                (string) $xml->contentDescription
288 1
            );
289 1
        }
290 1
        if (isset($xml->shipmentType) && $xml->shipmentType != '') {
291 1
            $customsInfo->setShipmentType(
292 1
                (string) $xml->shipmentType
293 1
            );
294 1
        }
295 1
        if (isset($xml->parcelReturnInstructions) && $xml->parcelReturnInstructions != '') {
296 1
            $customsInfo->setParcelReturnInstructions(
297 1
                (string) $xml->parcelReturnInstructions
298 1
            );
299 1
        }
300 1
        if (isset($xml->privateAddress) && $xml->privateAddress != '') {
301 1
            $customsInfo->setPrivateAddress(
302 1
                ((string) $xml->privateAddress == 'true')
303 1
            );
304
        }
305 1
306
        return $customsInfo;
307
    }
308
}
309