ParcelContent::createFromXML()   C
last analyzed

Complexity

Conditions 13
Paths 64

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 36
rs 6.6166
c 1
b 0
f 0
cc 13
nc 64
nop 1

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
3
namespace Bpost\BpostApiClient\Bpost\Order\Box\International;
4
5
use Bpost\BpostApiClient\Common\XmlHelper;
6
use DOMDocument;
7
use DOMElement;
8
use DOMException;
9
use SimpleXMLElement;
10
11
/**
12
 * bpost ParcelContent for international shipment.
13
 */
14
class ParcelContent
15
{
16
    /**
17
     * Number of items of each type for the specified parcel content.
18
     *
19
     * @var int
20
     */
21
    private $numberOfItemType;
22
23
    /**
24
     * Value for the number of items and NOT per item
25
     * Max length = 50
26
     * Integer format in cents, for example for 10€, you must sent 1000, NO decima.
27
     *
28
     * @var string
29
     */
30
    private $valueOfItem;
31
32
    /**
33
     * description of parcel content
34
     * Max length = 30 characters.
35
     *
36
     * @var string
37
     */
38
    private $itemDescription;
39
40
    /**
41
     * Weight for the number of itemsof each typeand NOT per item.
42
     * Integer format, NO decimal ! In gramme (gr).
43
     * Range 1-30000.
44
     *
45
     * @var int
46
     */
47
    private $nettoWeight;
48
49
    /**
50
     * HS stands for Harmonized System.
51
     * It’s a multipurpose international product nomenclature that describes the type of good that is shipped.
52
     * Today, customs officers must use HS code to clear every commodity that enters or crosses any international borders.
53
     * Integer format, maximum 9 digits
54
     * you can find the code on https://www.tariffnumber.com/.
55
     *
56
     * @var string
57
     */
58
    private $hsTariffCode;
59
60
    /**
61
     * 2 letters country code from the orign of goods
62
     * you can find the code on https://countrycode.org/.
63
     *
64
     * @var string
65
     */
66
    private $originOfGoods;
67
68
    /**
69
     * @return int
70
     */
71
    public function getNumberOfItemType()
72
    {
73
        return $this->numberOfItemType;
74
    }
75
76
    /**
77
     * @param int $numberOfItemType
78
     *
79
     * @return self
80
     */
81
    public function setNumberOfItemType($numberOfItemType)
82
    {
83
        $this->numberOfItemType = $numberOfItemType;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getValueOfItem()
92
    {
93
        return $this->valueOfItem;
94
    }
95
96
    /**
97
     * @param string $valueOfItem
98
     *
99
     * @return self
100
     */
101
    public function setValueOfItem($valueOfItem)
102
    {
103
        $this->valueOfItem = $valueOfItem;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getItemDescription()
112
    {
113
        return $this->itemDescription;
114
    }
115
116
    /**
117
     * @param string $itemDescription
118
     *
119
     * @return self
120
     */
121
    public function setItemDescription($itemDescription)
122
    {
123
        if (strlen($itemDescription) > 30) {
124
            $itemDescription = substr($itemDescription, 0, 30);
125
        }
126
127
        $this->itemDescription = $itemDescription;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @return int
134
     */
135
    public function getNettoWeight()
136
    {
137
        return $this->nettoWeight;
138
    }
139
140
    /**
141
     * @param int $nettoWeight
142
     *
143
     * @return self
144
     */
145
    public function setNettoWeight($nettoWeight)
146
    {
147
        $this->nettoWeight = $nettoWeight;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getHsTariffCode()
156
    {
157
        return $this->hsTariffCode;
158
    }
159
160
    /**
161
     * @param string $hsTariffCode
162
     *
163
     * @return self
164
     */
165
    public function setHsTariffCode($hsTariffCode)
166
    {
167
        $this->hsTariffCode = $hsTariffCode;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getOriginOfGoods()
176
    {
177
        return $this->originOfGoods;
178
    }
179
180
    /**
181
     * @param string $originOfGoods
182
     *
183
     * @return self
184
     */
185
    public function setOriginOfGoods($originOfGoods)
186
    {
187
        $this->originOfGoods = $originOfGoods;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param DOMDocument $document
194
     * @param string|null $prefix
195
     *
196
     * @return DOMElement|false
197
     *
198
     * @throws DOMException
199
     */
200
    public function toXML(DOMDocument $document, $prefix = null)
201
    {
202
        $parcelContent = $document->createElement(XmlHelper::getPrefixedTagName('parcelContent', $prefix));
203
204
        $parcelContent->appendChild(
205
            $document->createElement(
206
                XmlHelper::getPrefixedTagName('numberOfItemType', $prefix),
207
                $this->getNumberOfItemType()
208
            )
209
        );
210
        $parcelContent->appendChild(
211
            $document->createElement(
212
                XmlHelper::getPrefixedTagName('valueOfItem', $prefix),
213
                $this->getValueOfItem()
214
            )
215
        );
216
        $parcelContent->appendChild(
217
            $document->createElement(
218
                XmlHelper::getPrefixedTagName('itemDescription', $prefix),
219
                $this->getItemDescription()
220
            )
221
        );
222
        $parcelContent->appendChild(
223
            $document->createElement(
224
                XmlHelper::getPrefixedTagName('nettoWeight', $prefix),
225
                $this->getNettoWeight()
226
            )
227
        );
228
        $parcelContent->appendChild(
229
            $document->createElement(
230
                XmlHelper::getPrefixedTagName('hsTariffCode', $prefix),
231
                $this->getHsTariffCode()
232
            )
233
        );
234
        $parcelContent->appendChild(
235
            $document->createElement(
236
                XmlHelper::getPrefixedTagName('originOfGoods', $prefix),
237
                $this->getOriginOfGoods()
238
            )
239
        );
240
241
        return $parcelContent;
242
    }
243
244
    public static function createFromXML(SimpleXMLElement $xml)
245
    {
246
        $parcelContent = new ParcelContent();
247
248
        if (isset($xml->numberOfItemType) && $xml->numberOfItemType != '') {
249
            $parcelContent->setNumberOfItemType(
250
                (int) $xml->numberOfItemType
251
            );
252
        }
253
        if (isset($xml->valueOfItem) && $xml->valueOfItem != '') {
254
            $parcelContent->setValueOfItem(
255
                (int) $xml->valueOfItem
256
            );
257
        }
258
        if (isset($xml->itemDescription) && $xml->itemDescription != '') {
259
            $parcelContent->setItemDescription(
260
                (string) $xml->itemDescription
261
            );
262
        }
263
        if (isset($xml->nettoWeight) && $xml->nettoWeight != '') {
264
            $parcelContent->setNettoWeight(
265
                (int) $xml->nettoWeight
266
            );
267
        }
268
        if (isset($xml->hsTariffCode) && $xml->hsTariffCode != '') {
269
            $parcelContent->setHsTariffCode(
270
                (int) $xml->hsTariffCode
271
            );
272
        }
273
        if (isset($xml->originOfGoods) && $xml->originOfGoods != '') {
274
            $parcelContent->setOriginOfGoods(
275
                (string) $xml->originOfGoods
276
            );
277
        }
278
279
        return $parcelContent;
280
    }
281
}
282