Completed
Pull Request — develop (#102)
by
unknown
03:23
created

OtherCharges::toNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Ups\Entity;
4
5
use DOMDocument;
6
use DOMElement;
7
use Ups\NodeInterface;
8
9
class OtherCharges implements NodeInterface
10
{
11
    /**
12
     * @var string $monetaryValue
13
     */
14
    protected $monetaryValue;
15
16
    /**
17
     * @var string $description;
18
     */
19
    protected $description;
20
21 View Code Duplication
    public function __construct($attributes = null)
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...
22
    {
23
        if (null != $attributes) {
24
            if (isset($attributes->MonetaryValue)) {
25
                $this->setMonetaryValue($attributes->MonetaryValue);
26
            }
27
            if (isset($attributes->Description)) {
28
                $this->setDescription($attributes->Description);
29
            }
30
        }
31
    }
32
33
    /**
34
     * @param null|DOMDocument $document
35
     *
36
     * @return DOMElement
37
     */
38
    public function toNode(DOMDocument $document = null)
39
    {
40
        if (null === $document) {
41
            $document = new DOMDocument();
42
        }
43
44
        $node = $document->createElement('OtherCharges');
45
        $node->appendChild($document->createElement('MonetaryValue', $this->getMonetaryValue()));
46
        $node->appendChild($document->createElement('Description', $this->getDescription()));
47
48
        return $node;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getMonetaryValue()
55
    {
56
        return $this->monetaryValue;
57
    }
58
59
    /**
60
     * @param $var
61
     *
62
     * @throws \Exception
63
     *
64
     * @return $this
65
     */
66
    public function setMonetaryValue($var)
67
    {
68
        $this->monetaryValue = round($var, 2); // Max 2 decimals places
0 ignored issues
show
Documentation Bug introduced by
The property $monetaryValue was declared of type string, but round($var, 2) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
69
70
        if ($this->monetaryValue < 0) {
71
            throw new \Exception('Other charges cannot be negative');
72
        }
73
74
        if (strlen((string)$this->monetaryValue) > 15) {
75
            throw new \Exception('Value too long');
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getDescription()
85
    {
86
        return $this->description;
87
    }
88
89
    /**
90
     * @param $description
91
     *
92
     * @return $this
93
     */
94
    public function setDescription($description)
95
    {
96
        $this->description = $description;
97
98
        return $this;
99
    }
100
101
}
102