Passed
Pull Request — master (#149)
by
unknown
03:42
created

ItemizedPaymentInformation::setShipmentCharges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Ups\Entity;
4
5
use DOMDocument;
6
use Ups\NodeInterface;
7
8
class ItemizedPaymentInformation implements NodeInterface
9
{
10
11
    /**
12
     * Charges for Transportation (max 1) and Duties and Tax (max 1)
13
     *
14
     * @var array
15
     */
16
    private $charges = array();
17
18
    /**
19
     * @param null|DOMDocument $document
20
     *
21
     * @return DOMElement
22
     */
23 1
    public function toNode(DOMDocument $document = null)
24
    {
25 1
        if (null === $document) {
26
            $document = new DOMDocument();
27
        }
28
29 1
        $node = $document->createElement('ItemizedPaymentInformation');
30
31 1
        foreach ($this->getShipmentCharges() as $shipmentCharge) {
32
            $chNode = $document->createElement('ShipmentCharge');
33
            $chNode->appendChild($document->createElement('Type', $shipmentCharge->getType()));
34
35
            if ($shipmentCharge->getBillShipper()) {
36
                $chNode->appendChild($shipmentCharge->getBillShipper()->toNode($document));
37
            } elseif ($shipmentCharge->getBillReceiver()) {
38
                $receiverNode = $document->createElement('BillReceiver');
39
40
                if ($shipmentCharge->getBillReceiver()->getAccountNumber()) {
41
                    $receiverNode->appendChild($document->createElement('AccountNumber', $shipmentCharge->getBillReceiver()->getAccountNumber()));
42
                }
43
                if ($shipmentCharge->getBillReceiver()->getPostalCode()) {
44
                    $address = $document->createElement('Address');
45
                    $address->appendChild($document->createElement('PostalCode', $shipmentCharge->getBillReceiver()->getPostalCode()));
46
                    $receiverNode->appendChild($address);
47
                }
48
49
                $chNode->appendChild($receiverNode);
50
            } elseif ($shipmentCharge->getThirdPartyBilled()) {
51
                $chNode->appendChild($shipmentCharge->getThirdPartyBilled()->toNode($document));
52
            } elseif ($shipmentCharge->getConsigneeBilled()) {
53
                $chNode->appendChild($document->createElement('ConsigneeBilled'));
54
            }
55
56
            $node->appendChild($chNode);
57
58
            if ($shipmentCharge->getSplitDutyVATIndicator()) {
59
                $node->appendChild($document->createElement('SplitDutyVATIndicator'));
60
            }
61 1
        }
62
        
63 1
        return $node;
64
    }
65
66
    /**
67
     * @param ShipmentCharge $charge
68
     * @return $this
69
     */
70
    public function addShipmentCharge(ShipmentCharge $charge)
71
    {
72
        $this->charges[] = $charge;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return ShipmentCharge[]
79
     */
80 1
    public function getShipmentCharges()
81
    {
82 1
        return $this->charges;
83
    }
84
85
    /**
86
     * @param ShipmentCharge[] $charges
87
     * @return $this
88
     */
89
    public function setShipmentCharges(array $charges)
90
    {
91
        $this->charges = $charges;
92
93
        return $this;
94
    }
95
}
96