AtBpost   B
last analyzed

Complexity

Total Complexity 50

Size/Duplication

Total Lines 326
Duplicated Lines 11.66 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 50
lcom 1
cbo 6
dl 38
loc 326
ccs 153
cts 163
cp 0.9387
rs 8.6206
c 1
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setProduct() 8 8 2
A getPossibleProductValues() 0 6 1
A setPugoAddress() 0 4 1
A getPugoAddress() 0 4 1
A setPugoId() 0 4 1
A getPugoId() 0 4 1
A setPugoName() 0 4 1
A getPugoName() 0 4 1
A setReceiverCompany() 0 4 1
A getReceiverCompany() 0 4 1
A setReceiverName() 0 4 1
A getReceiverName() 0 4 1
A getRequestedDeliveryDate() 0 4 1
A setRequestedDeliveryDate() 0 4 1
A getShopHandlingInstruction() 0 7 2
A setShopHandlingInstruction() 0 4 1
B toXML() 0 36 6
A addToXmlRequestedDeliveryDate() 0 8 2
A addToXmlShopHandlingInstruction() 0 8 2
F createFromXML() 30 86 22

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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

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

Loading history...
234
    {
235 2
        if ($this->getRequestedDeliveryDate() !== null) {
236 2
            $typeElement->appendChild(
237 2
                $document->createElement('requestedDeliveryDate', $this->getRequestedDeliveryDate())
238 2
            );
239 2
        }
240 2
    }
241
242 2
    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.

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

Loading history...
243
    {
244 2
        if ($this->getShopHandlingInstruction() !== null) {
245 1
            $typeElement->appendChild(
246 1
                $document->createElement('shopHandlingInstruction', $this->getShopHandlingInstruction())
247 1
            );
248 1
        }
249 2
    }
250
251
    /**
252
     * @param  \SimpleXMLElement $xml
253
     *
254
     * @return AtBpost
255
     * @throws BpostInvalidValueException
256
     * @throws BpostNotImplementedException
257
     */
258 1
    public static function createFromXML(\SimpleXMLElement $xml)
259
    {
260 1
        $atBpost = new AtBpost();
261
262 1
        if (isset($xml->atBpost->product) && $xml->atBpost->product != '') {
263 1
            $atBpost->setProduct(
264 1
                (string)$xml->atBpost->product
265 1
            );
266 1
        }
267 1 View Code Duplication
        if (isset($xml->atBpost->options)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
268
            /** @var \SimpleXMLElement $optionData */
269 1
            foreach ($xml->atBpost->options as $optionData) {
270 1
                $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
271
272 1
                if (in_array(
273 1
                    $optionData->getName(),
274
                    array(
275 1
                        Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED,
276 1
                        Messaging::MESSAGING_TYPE_INFO_NEXT_DAY,
277 1
                        Messaging::MESSAGING_TYPE_INFO_REMINDER,
278 1
                        Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED,
279
                    )
280 1
                )
281 1
                ) {
282 1
                    $option = Messaging::createFromXML($optionData);
283 1
                } else {
284
                    $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
285
                    if (!method_exists($className, 'createFromXML')) {
286
                        throw new BpostNotImplementedException();
287
                    }
288
                    $option = call_user_func(
289
                        array($className, 'createFromXML'),
290
                        $optionData
291
                    );
292
                }
293
294 1
                $atBpost->addOption($option);
295 1
            }
296 1
        }
297 1
        if (isset($xml->atBpost->weight) && $xml->atBpost->weight != '') {
298 1
            $atBpost->setWeight(
299 1
                (int)$xml->atBpost->weight
300 1
            );
301 1
        }
302 1
        if (isset($xml->atBpost->receiverName) && $xml->atBpost->receiverName != '') {
303 1
            $atBpost->setReceiverName(
304 1
                (string)$xml->atBpost->receiverName
305 1
            );
306 1
        }
307 1
        if (isset($xml->atBpost->receiverCompany) && $xml->atBpost->receiverCompany != '') {
308 1
            $atBpost->setReceiverCompany(
309 1
                (string)$xml->atBpost->receiverCompany
310 1
            );
311 1
        }
312 1
        if (isset($xml->atBpost->pugoId) && $xml->atBpost->pugoId != '') {
313 1
            $atBpost->setPugoId(
314 1
                (string)$xml->atBpost->pugoId
315 1
            );
316 1
        }
317 1
        if (isset($xml->atBpost->pugoName) && $xml->atBpost->pugoName != '') {
318 1
            $atBpost->setPugoName(
319 1
                (string)$xml->atBpost->pugoName
320 1
            );
321 1
        }
322 1
        if (isset($xml->atBpost->pugoAddress)) {
323
            /** @var \SimpleXMLElement $pugoAddressData */
324 1
            $pugoAddressData = $xml->atBpost->pugoAddress->children(
325
                'http://schema.post.be/shm/deepintegration/v3/common'
326 1
            );
327 1
            $atBpost->setPugoAddress(
328 1
                PugoAddress::createFromXML($pugoAddressData)
329 1
            );
330 1
        }
331 1
        if (isset($xml->atBpost->requestedDeliveryDate) && $xml->atBpost->requestedDeliveryDate != '') {
332 1
            $atBpost->setRequestedDeliveryDate(
333 1
                (string)$xml->atBpost->requestedDeliveryDate
334 1
            );
335 1
        }
336 1
        if (isset($xml->atBpost->shopHandlingInstruction) && $xml->atBpost->shopHandlingInstruction != '') {
337
            $atBpost->setShopHandlingInstruction(
338
                (string)$xml->atBpost->shopHandlingInstruction
339
            );
340
        }
341
342 1
        return $atBpost;
343
    }
344
}
345