Completed
Pull Request — master (#86)
by
unknown
03:35
created

FreightCharges::setMonetaryValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 11
Ratio 78.57 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

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