Completed
Push — scrutinizer ( 42ac6a...7bc18e )
by z38
02:40
created

CreditTransfer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 82.69%

Importance

Changes 11
Bugs 1 Features 6
Metric Value
wmc 12
c 11
b 1
f 6
lcom 3
cbo 2
dl 0
loc 173
ccs 43
cts 52
cp 0.8269
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setRemittanceInformation() 0 6 1
A getAmount() 0 4 1
asDom() 0 1 ?
B buildHeader() 0 32 5
A buildCreditor() 0 8 1
A hasRemittanceInformation() 0 4 1
A buildRemittanceInformation() 0 11 2
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use Z38\SwissPayment\Money\Money;
6
use Z38\SwissPayment\PostalAddressInterface;
7
8
/**
9
 * CreditTransfer contains all the information about the beneficiary and further information about the transaction.
10
 */
11
abstract class CreditTransfer
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $instructionId;
17
18
    /**
19
     * @var string
20
     */
21
    protected $endToEndId;
22
23
    /**
24
     * @var string
25
     */
26
    protected $creditorName;
27
28
    /**
29
     * @var PostalAddressInterface
30
     */
31
    protected $creditorAddress;
32
33
    /**
34
     * @var Money
35
     */
36
    protected $amount;
37
38
    /**
39
     * @var string|null
40
     */
41
    protected $remittanceInformation;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param string                 $instructionId   Identifier of the instruction (should be unique within the message)
47
     * @param string                 $endToEndId      End-To-End Identifier of the instruction (passed unchanged along the complete processing chain)
48
     * @param Money                  $amount          Amount of money to be transferred
49
     * @param string                 $creditorName    Name of the creditor
50
     * @param PostalAddressInterface $creditorAddress Address of the creditor
51
     */
52 2
    public function __construct($instructionId, $endToEndId, Money $amount, $creditorName, PostalAddressInterface $creditorAddress)
53
    {
54 2
        $this->instructionId = (string) $instructionId;
55 2
        $this->endToEndId = (string) $endToEndId;
56 2
        $this->amount = $amount;
57 2
        $this->creditorName = (string) $creditorName;
58 2
        $this->creditorAddress = $creditorAddress;
59 2
        $this->remittanceInformation = null;
60 2
    }
61
62
    /**
63
     * Sets the unstructured remittance information
64
     *
65
     * @param string|null $remittanceInformation
66
     *
67
     * @return CreditTransfer This credit transfer
68
     */
69
    public function setRemittanceInformation($remittanceInformation)
70
    {
71
        $this->remittanceInformation = $remittanceInformation;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Gets the instructed amount of this transaction
78
     *
79
     * @return Money The instructed amount
80
     */
81 2
    public function getAmount()
82
    {
83 2
        return $this->amount;
84
    }
85
86
    /**
87
     * Builds a DOM tree of this transaction
88
     *
89
     * @param \DOMDocument $doc
90
     *
91
     * @return \DOMElement The built DOM tree
92
     */
93
    abstract public function asDom(\DOMDocument $doc);
94
95
    /**
96
     * Builds a DOM tree of this transaction and adds header nodes
97
     *
98
     * @param \DOMDocument $doc
99
     * @param string|null  $localInstrument Local Instrument
100
     * @param string|null  $serviceLevel
101
     *
102
     * @return \DOMNode The built DOM node
103
     */
104 2
    protected function buildHeader(\DOMDocument $doc, $localInstrument = null, $serviceLevel = null)
105
    {
106 2
        $root = $doc->createElement('CdtTrfTxInf');
107
108 2
        $id = $doc->createElement('PmtId');
109 2
        $id->appendChild($doc->createElement('InstrId', $this->instructionId));
110 2
        $id->appendChild($doc->createElement('EndToEndId', $this->endToEndId));
111 2
        $root->appendChild($id);
112
113 2
        if (!empty($localInstrument) || !empty($serviceLevel)) {
114 2
            $paymentType = $doc->createElement('PmtTpInf');
115 2
            if (!empty($localInstrument)) {
116 2
                $localInstrumentNode = $doc->createElement('LclInstrm');
117 2
                $localInstrumentNode->appendChild($doc->createElement('Prtry', $localInstrument));
118 2
                $paymentType->appendChild($localInstrumentNode);
119 2
            }
120 2
            if (!empty($serviceLevel)) {
121 2
                $serviceLevelNode = $doc->createElement('SvcLvl');
122 2
                $serviceLevelNode->appendChild($doc->createElement('Cd', $serviceLevel));
123 2
                $paymentType->appendChild($serviceLevelNode);
124 2
            }
125 2
            $root->appendChild($paymentType);
126 2
        }
127
128 2
        $amount = $doc->createElement('Amt');
129 2
        $instdAmount = $doc->createElement('InstdAmt', $this->amount->format());
130 2
        $instdAmount->setAttribute('Ccy', $this->amount->getCurrency());
131 2
        $amount->appendChild($instdAmount);
132 2
        $root->appendChild($amount);
133
134 2
        return $root;
135
    }
136
137
    /**
138
     * Builds a DOM node of the Creditor field
139
     *
140
     * @param \DOMDocument $doc
141
     *
142
     * @return \DOMNode The built DOM node
143
     */
144 2
    protected function buildCreditor(\DOMDocument $doc)
145
    {
146 2
        $creditor = $doc->createElement('Cdtr');
147 2
        $creditor->appendChild($doc->createElement('Nm', $this->creditorName));
148 2
        $creditor->appendChild($this->creditorAddress->asDom($doc));
149
150 2
        return $creditor;
151
    }
152
153
    /**
154
     * Indicates whether remittance information is set
155
     *
156
     * @return bool true if remittance information is set
157
     */
158 2
    protected function hasRemittanceInformation()
159
    {
160 2
        return !empty($this->remittanceInformation);
161
    }
162
163
    /**
164
     * Builds a DOM node of the Remittance Information field
165
     *
166
     * @param \DOMDocument $doc
167
     *
168
     * @return \DOMNode The built DOM node
169
     *
170
     * @throws \LogicException When no remittance information is set
171
     */
172
    protected function buildRemittanceInformation(\DOMDocument $doc)
173
    {
174
        if ($this->hasRemittanceInformation()) {
175
            $remittance = $doc->createElement('RmtInf');
176
            $remittance->appendChild($doc->createElement('Ustrd', $this->remittanceInformation));
177
178
            return $remittance;
179
        } else {
180
            throw new \LogicException('Can not build node without data.');
181
        }
182
    }
183
}
184