Box::createFromXML()   C
last analyzed

Complexity

Conditions 14
Paths 202

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 20.3684

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 67
ccs 32
cts 47
cp 0.6808
rs 5.292
c 1
b 0
f 0
cc 14
eloc 37
nc 202
nop 1
crap 20.3684

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
namespace Bpost\BpostApiClient\Bpost\Order;
3
4
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
5
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
6
7
/**
8
 * bPost Box class
9
 *
10
 * @author Tijs Verkoyen <[email protected]>
11
 */
12
class Box
13
{
14
15
    const BOX_STATUS_OPEN = 'OPEN';
16
    const BOX_STATUS_PENDING = 'PENDING';
17
    const BOX_STATUS_PRINTED = 'PRINTED';
18
    const BOX_STATUS_CANCELLED = 'CANCELLED';
19
    const BOX_STATUS_ON_HOLD = 'ON-HOLD';
20
    const BOX_STATUS_ANNOUNCED = 'ANNOUNCED';
21
    const BOX_STATUS_IN_TRANSIT = 'IN_TRANSIT';
22
    const BOX_STATUS_AWAITING_PICKUP = 'AWAITING_PICKUP';
23
    const BOX_STATUS_DELIVERED = 'DELIVERED';
24
    const BOX_STATUS_BACK_TO_SENDER = 'BACK_TO_SENDER';
25
26
    /**
27
     * @var \Bpost\BpostApiClient\Bpost\Order\Sender
28
     */
29
    private $sender;
30
31
    /**
32
     * @var \Bpost\BpostApiClient\Bpost\Order\Box\AtHome
33
     */
34
    private $nationalBox;
35
36
    /**
37
     * @var \Bpost\BpostApiClient\Bpost\Order\Box\International
38
     */
39
    private $internationalBox;
40
41
    /**
42
     * @var string
43
     */
44
    private $remark;
45
46
    /**
47
     * @var string
48
     */
49
    private $status;
50
51
    /** @var string */
52
    private $barcode;
53
54
    /** @var string */
55
    private $additionalCustomerReference;
56
57
    /**
58
     * @param \Bpost\BpostApiClient\Bpost\Order\Box\International $internationalBox
59
     */
60 1
    public function setInternationalBox(Box\International $internationalBox)
61
    {
62 1
        $this->internationalBox = $internationalBox;
63 1
    }
64
65
    /**
66
     * @return \Bpost\BpostApiClient\Bpost\Order\Box\International
67
     */
68 4
    public function getInternationalBox()
69
    {
70 4
        return $this->internationalBox;
71
    }
72
73
    /**
74
     * @param \Bpost\BpostApiClient\Bpost\Order\Box\National $nationalBox
75
     */
76 3
    public function setNationalBox(Box\National $nationalBox)
77
    {
78 3
        $this->nationalBox = $nationalBox;
0 ignored issues
show
Documentation Bug introduced by
$nationalBox is of type object<Bpost\BpostApiCli...ost\Order\Box\National>, but the property $nationalBox was declared to be of type object<Bpost\BpostApiCli...Bpost\Order\Box\AtHome>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
79 3
    }
80
81
    /**
82
     * @return \Bpost\BpostApiClient\Bpost\Order\Box\National
83
     */
84 4
    public function getNationalBox()
85
    {
86 4
        return $this->nationalBox;
87
    }
88
89
    /**
90
     * @param string $remark
91
     */
92 4
    public function setRemark($remark)
93
    {
94 4
        $this->remark = $remark;
95 4
    }
96
97
    /**
98
     * @return string
99
     */
100 4
    public function getRemark()
101
    {
102 4
        return $this->remark;
103
    }
104
105
    /**
106
     * @param \Bpost\BpostApiClient\Bpost\Order\Sender $sender
107
     */
108 4
    public function setSender(Sender $sender)
109
    {
110 4
        $this->sender = $sender;
111 4
    }
112
113
    /**
114
     * @return \Bpost\BpostApiClient\Bpost\Order\Sender
115
     */
116 4
    public function getSender()
117
    {
118 4
        return $this->sender;
119
    }
120
121
    /**
122
     * @param string $status
123
     * @throws BpostInvalidValueException
124
     */
125 2 View Code Duplication
    public function setStatus($status)
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...
126
    {
127 2
        $status = strtoupper($status);
128 2
        if (!in_array($status, self::getPossibleStatusValues())) {
129 1
            throw new BpostInvalidValueException('status', $status, self::getPossibleStatusValues());
130
        }
131
132 1
        $this->status = $status;
133 1
    }
134
135
    /**
136
     * @param string $barcode
137
     */
138 2
    public function setBarcode($barcode)
139
    {
140 2
        $this->barcode = strtoupper((string) $barcode);
141 2
    }
142
143
    /**
144
     * @return string
145
     */
146 3
    public function getBarcode()
147
    {
148 3
        return $this->barcode;
149
    }
150
151
    /**
152
     * @return string
153
     */
154 1
    public function getStatus()
155
    {
156 1
        return $this->status;
157
    }
158
159
    /**
160
     * @param string $additionalCustomerReference
161
     */
162 2
    public function setAdditionalCustomerReference($additionalCustomerReference)
163
    {
164 2
        $this->additionalCustomerReference = (string)$additionalCustomerReference;
165 2
    }
166
167
    /**
168
     * @return string
169
     */
170 4
    public function getAdditionalCustomerReference()
171
    {
172 4
        return $this->additionalCustomerReference;
173
    }
174
175
    /**
176
     * @return array
177
     */
178 2 View Code Duplication
    public static function getPossibleStatusValues()
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...
179
    {
180
        return array(
181 2
            self::BOX_STATUS_OPEN,
182 2
            self::BOX_STATUS_PENDING,
183 2
            self::BOX_STATUS_PRINTED,
184 2
            self::BOX_STATUS_CANCELLED,
185 2
            self::BOX_STATUS_ON_HOLD,
186 2
            self::BOX_STATUS_ANNOUNCED,
187 2
            self::BOX_STATUS_IN_TRANSIT,
188 2
            self::BOX_STATUS_AWAITING_PICKUP,
189 2
            self::BOX_STATUS_DELIVERED,
190 2
            self::BOX_STATUS_BACK_TO_SENDER,
191 2
        );
192
    }
193
194
    /**
195
     * Return the object as an array for usage in the XML
196
     *
197
     * @param  \DomDocument $document
198
     * @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...
199
     * @return \DomElement
200
     */
201 3
    public function toXML(\DOMDocument $document, $prefix = null)
202
    {
203 3
        $tagName = 'box';
204 3
        if ($prefix !== null) {
205 1
            $tagName = $prefix . ':' . $tagName;
206 1
        }
207
208 3
        $box = $document->createElement($tagName);
209
210 3
        if ($this->getSender() !== null) {
211 3
            $box->appendChild(
212 3
                $this->getSender()->toXML($document, $prefix)
213 3
            );
214 3
        }
215 3
        if ($this->getNationalBox() !== null) {
216 2
            $box->appendChild(
217 2
                $this->getNationalBox()->toXML($document, $prefix)
218 2
            );
219 2
        }
220 3
        if ($this->getInternationalBox() !== null) {
221 1
            $box->appendChild(
222 1
                $this->getInternationalBox()->toXML($document, $prefix)
223 1
            );
224 1
        }
225 3
        if ($this->getRemark() !== null) {
226 3
            $tagName = 'remark';
227 3
            if ($prefix !== null) {
228 1
                $tagName = $prefix . ':' . $tagName;
229 1
            }
230 3
            $box->appendChild(
231 3
                $document->createElement(
232 3
                    $tagName,
233 3
                    $this->getRemark()
234 3
                )
235 3
            );
236 3
        }
237 3
        if ($this->getAdditionalCustomerReference() !== null) {
238 1
            $tagName = 'additionalCustomerReference';
239 1
            if ($prefix !== null) {
240 1
                $tagName = $prefix . ':' . $tagName;
241 1
            }
242 1
            $box->appendChild(
243 1
                $document->createElement(
244 1
                    $tagName,
245 1
                    $this->getAdditionalCustomerReference()
246 1
                )
247 1
            );
248 1
        }
249 3
        if ($this->getBarcode() !== null) {
250 2
            $tagName = 'barcode';
251 2
            if ($prefix !== null) {
252
                $tagName = $prefix . ':' . $tagName;
253
            }
254 2
            $box->appendChild(
255 2
                $document->createElement(
256 2
                    $tagName,
257 2
                    $this->getBarcode()
258 2
                )
259 2
            );
260 2
        }
261
262 3
        return $box;
263
    }
264
265
    /**
266
     * @param  \SimpleXMLElement $xml
267
     *
268
     * @return Box
269
     * @throws BpostInvalidValueException
270
     * @throws BpostNotImplementedException
271
     */
272 1
    public static function createFromXML(\SimpleXMLElement $xml)
273
    {
274 1
        $box = new Box();
275 1
        if (isset($xml->sender)) {
276 1
            $box->setSender(
277 1
                Sender::createFromXML(
278 1
                    $xml->sender->children(
279
                        'http://schema.post.be/shm/deepintegration/v3/common'
280 1
                    )
281 1
                )
282 1
            );
283 1
        }
284 1
        if (isset($xml->nationalBox)) {
285
            /** @var \SimpleXMLElement $nationalBoxData */
286 1
            $nationalBoxData = $xml->nationalBox->children('http://schema.post.be/shm/deepintegration/v3/national');
287
288
            // build classname based on the tag name
289 1
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($nationalBoxData->getName());
290 1
            if ($nationalBoxData->getName() == 'at24-7') {
291
                $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\At247';
292
            }
293
294 1
            if (!method_exists($className, 'createFromXML')) {
295
                throw new BpostNotImplementedException();
296
            }
297
298 1
            $nationalBox = call_user_func(
299 1
                array($className, 'createFromXML'),
300
                $nationalBoxData
301 1
            );
302
303 1
            $box->setNationalBox($nationalBox);
304 1
        }
305 1
        if (isset($xml->internationalBox)) {
306
            /** @var \SimpleXMLElement $internationalBoxData */
307
            $internationalBoxData = $xml->internationalBox->children('http://schema.post.be/shm/deepintegration/v3/international');
308
309
            // build classname based on the tag name
310
            $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\' . ucfirst($internationalBoxData->getName());
311
312
            if (!method_exists($className, 'createFromXML')) {
313
                var_dump($className);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($className); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
314
                throw new BpostNotImplementedException();
315
            }
316
317
            $internationalBox = call_user_func(
318
                array($className, 'createFromXML'),
319
                $internationalBoxData
320
            );
321
322
            $box->setInternationalBox($internationalBox);
323
        }
324 1
        if (isset($xml->remark) && $xml->remark != '') {
325 1
            $box->setRemark((string) $xml->remark);
326 1
        }
327 1
        if (isset($xml->additionalCustomerReference) && $xml->additionalCustomerReference != '') {
328 1
            $box->setAdditionalCustomerReference((string)$xml->additionalCustomerReference);
329 1
        }
330 1
        if (!empty($xml->barcode)) {
331
            $box->setBarcode((string) $xml->barcode);
332
        }
333 1
        if (isset($xml->status) && $xml->status != '') {
334 1
            $box->setStatus((string) $xml->status);
335 1
        }
336
337 1
        return $box;
338
    }
339
}
340