Passed
Pull Request — master (#223)
by
unknown
02:52
created

FreightCharges::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 3
cts 7
cp 0.4286
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.679
1
<?php
2
3
namespace Ups\Entity;
4
5
use DOMDocument;
6
use DOMElement;
7
use Ups\NodeInterface;
8
9 View Code Duplication
class FreightCharges implements NodeInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    protected $monetaryValue;
12
13 2
    public function __construct($response = null)
14
    {
15 2
        if (null !== $response) {
16
            if (isset($response->MonetaryValue)) {
17
                $this->setMonetaryValue($response->MonetaryValue);
18
            }
19
        }
20 2
    }
21
22
    /**
23
     * @param null|DOMDocument $document
24
     *
25
     * @return DOMElement
26
     */
27 1
    public function toNode(DOMDocument $document = null)
28
    {
29 1
        if (null === $document) {
30
            $document = new DOMDocument();
31
        }
32
33 1
        $node = $document->createElement('FreightCharges');
34 1
        $node->appendChild($document->createElement('MonetaryValue', $this->getMonetaryValue()));
35
36 1
        return $node;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42 2
    public function getMonetaryValue()
43
    {
44 2
        return $this->monetaryValue;
45
    }
46
47
    /**
48
     * @param $var
49
     *
50
     * @throws \Exception
51
     *
52
     * @return $this
53
     */
54
    public function setMonetaryValue($var)
55
    {
56
        $this->monetaryValue = round($var, 2); // Max 2 decimals places
57
58
        if ($this->monetaryValue < 0) {
59
            throw new \Exception('Freight charges cannot be negative');
60
        }
61
62
        if (strlen((string)$this->monetaryValue) > 15) {
63
            throw new \Exception('Value too long');
64
        }
65
66
        return $this;
67
    }
68
}
69