AtBpost   B
last analyzed

Complexity

Total Complexity 49

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 49
eloc 102
dl 0
loc 324
rs 8.48
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getShopHandlingInstruction() 0 7 2
A addToXmlRequestedDeliveryDate() 0 5 2
A setReceiverName() 0 3 1
A addToXmlShopHandlingInstruction() 0 5 2
A getReceiverName() 0 3 1
A getPossibleProductValues() 0 4 1
A setPugoId() 0 3 1
A setPugoName() 0 3 1
A getPugoAddress() 0 3 1
A getPugoId() 0 3 1
A setProduct() 0 7 2
A setRequestedDeliveryDate() 0 3 1
A getRequestedDeliveryDate() 0 3 1
B toXML() 0 35 6
F createFromXML() 0 78 21
A getPugoName() 0 3 1
A getReceiverCompany() 0 3 1
A setPugoAddress() 0 3 1
A setReceiverCompany() 0 3 1
A setShopHandlingInstruction() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like AtBpost 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 AtBpost, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bpost\BpostApiClient\Bpost\Order\Box;
4
5
use Bpost\BpostApiClient\Bpost\Order\Box\National\ShopHandlingInstruction;
6
use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging;
7
use Bpost\BpostApiClient\Bpost\Order\PugoAddress;
8
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Product;
9
use Bpost\BpostApiClient\Common\XmlHelper;
10
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
11
use Bpost\BpostApiClient\Exception\BpostNotImplementedException;
12
use DOMDocument;
13
use DOMElement;
14
use SimpleXMLElement;
15
16
/**
17
 * bPost AtBpost class
18
 *
19
 * @author    Tijs Verkoyen <[email protected]>
20
 *
21
 * @version   3.0.0
22
 *
23
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
24
 * @license   BSD License
25
 */
26
class AtBpost extends National
27
{
28
    /** @var string */
29
    protected $product = Product::PRODUCT_NAME_BPACK_AT_BPOST;
30
31
    /** @var string */
32
    private $pugoId;
33
34
    /** @var string */
35
    private $pugoName;
36
37
    /** @var \Bpost\BpostApiClient\Bpost\Order\PugoAddress */
38
    private $pugoAddress;
39
40
    /** @var string */
41
    private $receiverName;
42
43
    /** @var string */
44
    private $receiverCompany;
45
46
    /** @var string */
47
    protected $requestedDeliveryDate;
48
49
    /** @var ShopHandlingInstruction */
50
    private $shopHandlingInstruction;
51
52
    /**
53
     * @param string $product Possible values are: bpack@bpost
54
     *
55
     * @throws BpostInvalidValueException
56
     */
57
    public function setProduct($product)
58
    {
59
        if (!in_array($product, self::getPossibleProductValues())) {
60
            throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues());
61
        }
62
63
        parent::setProduct($product);
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public static function getPossibleProductValues()
70
    {
71
        return array(
72
            Product::PRODUCT_NAME_BPACK_AT_BPOST,
73
        );
74
    }
75
76
    /**
77
     * @param \Bpost\BpostApiClient\Bpost\Order\PugoAddress $pugoAddress
78
     */
79
    public function setPugoAddress($pugoAddress)
80
    {
81
        $this->pugoAddress = $pugoAddress;
82
    }
83
84
    /**
85
     * @return \Bpost\BpostApiClient\Bpost\Order\PugoAddress
86
     */
87
    public function getPugoAddress()
88
    {
89
        return $this->pugoAddress;
90
    }
91
92
    /**
93
     * @param string $pugoId
94
     */
95
    public function setPugoId($pugoId)
96
    {
97
        $this->pugoId = $pugoId;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getPugoId()
104
    {
105
        return $this->pugoId;
106
    }
107
108
    /**
109
     * @param string $pugoName
110
     */
111
    public function setPugoName($pugoName)
112
    {
113
        $this->pugoName = $pugoName;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getPugoName()
120
    {
121
        return $this->pugoName;
122
    }
123
124
    /**
125
     * @param string $receiverCompany
126
     */
127
    public function setReceiverCompany($receiverCompany)
128
    {
129
        $this->receiverCompany = $receiverCompany;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getReceiverCompany()
136
    {
137
        return $this->receiverCompany;
138
    }
139
140
    /**
141
     * @param string $receiverName
142
     */
143
    public function setReceiverName($receiverName)
144
    {
145
        $this->receiverName = $receiverName;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getReceiverName()
152
    {
153
        return $this->receiverName;
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getRequestedDeliveryDate()
160
    {
161
        return $this->requestedDeliveryDate;
162
    }
163
164
    /**
165
     * @param string $requestedDeliveryDate
166
     */
167
    public function setRequestedDeliveryDate($requestedDeliveryDate)
168
    {
169
        $this->requestedDeliveryDate = $requestedDeliveryDate;
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getShopHandlingInstruction()
176
    {
177
        if ($this->shopHandlingInstruction !== null) {
178
            return $this->shopHandlingInstruction->getValue();
179
        }
180
181
        return null;
182
    }
183
184
    /**
185
     * @param string $shopHandlingInstruction
186
     */
187
    public function setShopHandlingInstruction($shopHandlingInstruction)
188
    {
189
        $this->shopHandlingInstruction = new ShopHandlingInstruction($shopHandlingInstruction);
190
    }
191
192
    /**
193
     * Return the object as an array for usage in the XML
194
     *
195
     * @param DomDocument $document
196
     * @param string      $prefix
197
     * @param string      $type
198
     *
199
     * @return DomElement
200
     */
201
    public function toXML(DOMDocument $document, $prefix = null, $type = null)
202
    {
203
        $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix));
204
        $boxElement = parent::toXML($document, null, 'atBpost');
205
        $nationalElement->appendChild($boxElement);
206
207
        if ($this->getPugoId() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPugoId() !== null is always true.
Loading history...
208
            $boxElement->appendChild(
209
                $document->createElement('pugoId', $this->getPugoId())
210
            );
211
        }
212
        if ($this->getPugoName() !== null) {
0 ignored issues
show
introduced by
The condition $this->getPugoName() !== null is always true.
Loading history...
213
            $boxElement->appendChild(
214
                $document->createElement('pugoName', $this->getPugoName())
215
            );
216
        }
217
        if ($this->getPugoAddress() !== null) {
218
            $boxElement->appendChild(
219
                $this->getPugoAddress()->toXML($document, 'common')
220
            );
221
        }
222
        if ($this->getReceiverName() !== null) {
0 ignored issues
show
introduced by
The condition $this->getReceiverName() !== null is always true.
Loading history...
223
            $boxElement->appendChild(
224
                $document->createElement('receiverName', $this->getReceiverName())
225
            );
226
        }
227
        if ($this->getReceiverCompany() !== null) {
0 ignored issues
show
introduced by
The condition $this->getReceiverCompany() !== null is always true.
Loading history...
228
            $boxElement->appendChild(
229
                $document->createElement('receiverCompany', $this->getReceiverCompany())
230
            );
231
        }
232
        $this->addToXmlRequestedDeliveryDate($document, $boxElement, $prefix);
233
        $this->addToXmlShopHandlingInstruction($document, $boxElement, $prefix);
234
235
        return $nationalElement;
236
    }
237
238
    /**
239
     * @param DOMDocument $document
240
     * @param DOMElement  $typeElement
241
     * @param string      $prefix
242
     */
243
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, $prefix)
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

243
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, /** @scrutinizer ignore-unused */ $prefix)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
244
    {
245
        if ($this->getRequestedDeliveryDate() !== null) {
0 ignored issues
show
introduced by
The condition $this->getRequestedDeliveryDate() !== null is always true.
Loading history...
246
            $typeElement->appendChild(
247
                $document->createElement('requestedDeliveryDate', $this->getRequestedDeliveryDate())
248
            );
249
        }
250
    }
251
252
    private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, $prefix)
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

252
    private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, /** @scrutinizer ignore-unused */ $prefix)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
253
    {
254
        if ($this->getShopHandlingInstruction() !== null) {
0 ignored issues
show
introduced by
The condition $this->getShopHandlingInstruction() !== null is always true.
Loading history...
255
            $typeElement->appendChild(
256
                $document->createElement('shopHandlingInstruction', $this->getShopHandlingInstruction())
257
            );
258
        }
259
    }
260
261
    /**
262
     * @param SimpleXMLElement $xml
263
     * @param National|null    $self
264
     *
265
     * @return AtBpost
266
     *
267
     * @throws BpostInvalidValueException
268
     * @throws BpostNotImplementedException
269
     * @throws \Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException
270
     * @throws \Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException
271
     */
272
    public static function createFromXML(SimpleXMLElement $xml, National $self = null)
273
    {
274
        $atBpost = new AtBpost();
275
276
        if (isset($xml->atBpost->product) && $xml->atBpost->product != '') {
277
            $atBpost->setProduct(
278
                (string) $xml->atBpost->product
279
            );
280
        }
281
        if (isset($xml->atBpost->options)) {
282
            /** @var SimpleXMLElement $optionData */
283
            foreach ($xml->atBpost->options as $optionData) {
284
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
0 ignored issues
show
Bug introduced by
The method children() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

284
                /** @scrutinizer ignore-call */ 
285
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
285
286
                if (in_array(
287
                    $optionData->getName(),
288
                    array(
289
                        Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
290
                        Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
291
                        Messaging::MESSAGING_TYPE_INFO_REMINDER,
292
                        Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
293
                    )
294
                )
295
                ) {
296
                    $option = Messaging::createFromXML($optionData);
297
                } else {
298
                    $option = self::getOptionFromOptionData($optionData);
299
                }
300
301
                $atBpost->addOption($option);
302
            }
303
        }
304
        if (isset($xml->atBpost->weight) && $xml->atBpost->weight != '') {
305
            $atBpost->setWeight(
306
                (int) $xml->atBpost->weight
307
            );
308
        }
309
        if (isset($xml->atBpost->receiverName) && $xml->atBpost->receiverName != '') {
310
            $atBpost->setReceiverName(
311
                (string) $xml->atBpost->receiverName
312
            );
313
        }
314
        if (isset($xml->atBpost->receiverCompany) && $xml->atBpost->receiverCompany != '') {
315
            $atBpost->setReceiverCompany(
316
                (string) $xml->atBpost->receiverCompany
317
            );
318
        }
319
        if (isset($xml->atBpost->pugoId) && $xml->atBpost->pugoId != '') {
320
            $atBpost->setPugoId(
321
                (string) $xml->atBpost->pugoId
322
            );
323
        }
324
        if (isset($xml->atBpost->pugoName) && $xml->atBpost->pugoName != '') {
325
            $atBpost->setPugoName(
326
                (string) $xml->atBpost->pugoName
327
            );
328
        }
329
        if (isset($xml->atBpost->pugoAddress)) {
330
            /** @var SimpleXMLElement $pugoAddressData */
331
            $pugoAddressData = $xml->atBpost->pugoAddress->children(
332
                'http://schema.post.be/shm/deepintegration/v3/common'
333
            );
334
            $atBpost->setPugoAddress(
335
                PugoAddress::createFromXML($pugoAddressData)
336
            );
337
        }
338
        if (isset($xml->atBpost->requestedDeliveryDate) && $xml->atBpost->requestedDeliveryDate != '') {
339
            $atBpost->setRequestedDeliveryDate(
340
                (string) $xml->atBpost->requestedDeliveryDate
341
            );
342
        }
343
        if (isset($xml->atBpost->shopHandlingInstruction) && $xml->atBpost->shopHandlingInstruction != '') {
344
            $atBpost->setShopHandlingInstruction(
345
                (string) $xml->atBpost->shopHandlingInstruction
346
            );
347
        }
348
349
        return $atBpost;
350
    }
351
}
352