Completed
Push — wip ( 681ec1 )
by z38
02:37
created

CreditTransfer   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 25
lcom 4
cbo 5
dl 0
loc 283
ccs 85
cts 85
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getLocalInstrument() 0 4 1
A getServiceLevel() 0 4 1
A setPurpose() 0 6 1
A getChargeBearer() 0 4 1
A setUltimateDebtorName() 0 6 1
A setUltimateDebtorAddress() 0 6 1
A setRemittanceInformation() 0 6 1
A getAmount() 0 4 1
asDom() 0 1 ?
C buildHeader() 0 47 11
A buildCreditor() 0 8 1
A appendPurpose() 0 8 2
A appendRemittanceInformation() 0 8 2
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use Z38\SwissPayment\Money\Money;
6
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
7
use Z38\SwissPayment\PostalAddressInterface;
8
use Z38\SwissPayment\RemittanceInformation\RemittanceInformation;
9
10
/**
11
 * CreditTransfer contains all the information about the beneficiary and further information about the transaction.
12
 */
13
abstract class CreditTransfer
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $instructionId;
19
20
    /**
21
     * @var string
22
     */
23
    protected $endToEndId;
24
25
    /**
26
     * @var string
27
     */
28
    protected $creditorName;
29
30
    /**
31
     * @var PostalAddressInterface
32
     */
33
    protected $creditorAddress;
34
35
    /**
36
     * @var Money
37
     */
38
    protected $amount;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $ultimateDebtorName;
44
45
    /**
46
     * @var PostalAddressInterface|null
47
     */
48
    protected $ultimateDebtorAddress;
49
50
    /**
51
     * @var string|null
52
     */
53
    protected $localInstrument;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $serviceLevel;
59
60
    /**
61
     * @var PurposeCode|null
62
     */
63
    protected $purpose;
64
65
    /**
66
     * @var RemittanceInformation|null
67
     */
68
    protected $remittanceInformation;
69
70
    /**
71
     * Constructor
72
     *
73
     * @param string                 $instructionId   Identifier of the instruction (should be unique within the message)
74
     * @param string                 $endToEndId      End-To-End Identifier of the instruction (passed unchanged along the complete processing chain)
75
     * @param Money                  $amount          Amount of money to be transferred
76
     * @param string                 $creditorName    Name of the creditor
77
     * @param PostalAddressInterface $creditorAddress Address of the creditor
78
     */
79 2
    public function __construct($instructionId, $endToEndId, Money $amount, $creditorName, PostalAddressInterface $creditorAddress)
80
    {
81 2
        $this->instructionId = (string) $instructionId;
82 2
        $this->endToEndId = (string) $endToEndId;
83 2
        $this->amount = $amount;
84 2
        $this->creditorName = (string) $creditorName;
85 2
        $this->creditorAddress = $creditorAddress;
86 2
    }
87
88
    /**
89
     * Gets the local instrument
90
     *
91
     * @return string|null The local instrument
92
     */
93 2
    public function getLocalInstrument()
94
    {
95 2
        return $this->localInstrument;
96
    }
97
98
    /**
99
     * Gets the service level
100
     *
101
     * @return string|null The service level
102
     */
103 2
    public function getServiceLevel()
104
    {
105 2
        return $this->serviceLevel;
106
    }
107
108
    /**
109
     * Sets the purpose of the payment
110
     *
111
     * @param PurposeCode $purpose The purpose
112
     *
113
     * @return CreditTransfer This credit transfer
114
     */
115 2
    public function setPurpose(PurposeCode $purpose)
116
    {
117 2
        $this->purpose = $purpose;
118
119 2
        return $this;
120
    }
121
122
    /**
123
     * Gets the charge bearer
124
     *
125
     * @return string|null The charge bearer
126
     */
127 2
    public function getChargeBearer()
128
    {
129 2
        return null;
130
    }
131
132
    /**
133
     * Sets the ultimate debtor's name
134
     *
135
     * @param string|null $name
136
     *
137
     * @return self
138
     */
139 2
    public function setUltimateDebtorName($name)
140
    {
141 2
        $this->ultimateDebtorName = $name;
142
143 2
        return $this;
144
    }
145
146
    /**
147
     * Sets the ultimate debtor's address
148
     *
149
     * @param PostalAddressInterface|null $address
150
     *
151
     * @return self
152
     */
153 2
    public function setUltimateDebtorAddress(PostalAddressInterface $address)
154
    {
155 2
        $this->ultimateDebtorAddress = $address;
156
157 2
        return $this;
158
    }
159
160
    /**
161
     * Sets the remittance information
162
     *
163
     * @param RemittanceInformation|null $remittanceInformation
164
     *
165
     * @return self
166
     */
167 2
    public function setRemittanceInformation(RemittanceInformation $remittanceInformation)
168
    {
169 2
        $this->remittanceInformation = $remittanceInformation;
170
171 2
        return $this;
172
    }
173
174
    /**
175
     * Gets the instructed amount of this transaction
176
     *
177
     * @return Money The instructed amount
178
     */
179 2
    public function getAmount()
180
    {
181 2
        return $this->amount;
182
    }
183
184
    /**
185
     * Builds a DOM tree of this transaction
186
     *
187
     * @param \DOMDocument       $doc
188
     * @param PaymentInformation $paymentInformation Information on B-level
189
     *
190
     * @return \DOMElement The built DOM tree
191
     */
192
    abstract public function asDom(\DOMDocument $doc, PaymentInformation $paymentInformation);
193
194
    /**
195
     * Builds a DOM tree of this transaction and adds header nodes
196
     *
197
     * @param \DOMDocument       $doc
198
     * @param PaymentInformation $paymentInformation The corresponding B-level element
199
     *
200
     * @return \DOMNode The built DOM node
201
     */
202 2
    protected function buildHeader(\DOMDocument $doc, PaymentInformation $paymentInformation, $chargeBearer = null)
0 ignored issues
show
Unused Code introduced by
The parameter $chargeBearer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    {
204 2
        $root = $doc->createElement('CdtTrfTxInf');
205
206 2
        $id = $doc->createElement('PmtId');
207 2
        $id->appendChild($doc->createElement('InstrId', $this->instructionId));
208 2
        $id->appendChild($doc->createElement('EndToEndId', $this->endToEndId));
209 2
        $root->appendChild($id);
210
211 2
        if (!$paymentInformation->hasPaymentTypeInformation() && ($this->localInstrument !== null || $this->serviceLevel !== null)) {
212 2
            $paymentType = $doc->createElement('PmtTpInf');
213 2
            if ($this->localInstrument !== null) {
214 2
                $localInstrumentNode = $doc->createElement('LclInstrm');
215 2
                $localInstrumentNode->appendChild($doc->createElement('Prtry', $this->localInstrument));
216 2
                $paymentType->appendChild($localInstrumentNode);
217 2
            }
218 2
            if ($this->serviceLevel !== null) {
219 2
                $serviceLevelNode = $doc->createElement('SvcLvl');
220 2
                $serviceLevelNode->appendChild($doc->createElement('Cd', $this->serviceLevel));
221 2
                $paymentType->appendChild($serviceLevelNode);
222 2
            }
223 2
            $root->appendChild($paymentType);
224 2
        }
225
226 2
        $amount = $doc->createElement('Amt');
227 2
        $instdAmount = $doc->createElement('InstdAmt', $this->amount->format());
228 2
        $instdAmount->setAttribute('Ccy', $this->amount->getCurrency());
229 2
        $amount->appendChild($instdAmount);
230 2
        $root->appendChild($amount);
231
232 2
        if ($chargeBearer = $this->getChargeBearer()) {
233 2
            $root->appendChild($doc->createElement('ChrgBr', $chargeBearer));
234 2
        }
235
236 2
        if (strlen($this->ultimateDebtorName) || $this->ultimateDebtorAddress !== null) {
237 2
            $ultimateDebtor = $doc->createElement('UltmtDbtr');
238 2
            if (strlen($this->ultimateDebtorName)) {
239 2
                $ultimateDebtor->appendChild($doc->createElement('Nm', $this->ultimateDebtorName));
240 2
            }
241 2
            if ($this->ultimateDebtorAddress !== null) {
242 2
                $ultimateDebtor->appendChild($this->ultimateDebtorAddress->asDom($doc));
243 2
            }
244 2
            $root->appendChild($ultimateDebtor);
245 2
        }
246
247 2
        return $root;
248
    }
249
250
    /**
251
     * Builds a DOM node of the Creditor field
252
     *
253
     * @param \DOMDocument $doc
254
     *
255
     * @return \DOMNode The built DOM node
256
     */
257 2
    protected function buildCreditor(\DOMDocument $doc)
258
    {
259 2
        $creditor = $doc->createElement('Cdtr');
260 2
        $creditor->appendChild($doc->createElement('Nm', $this->creditorName));
261 2
        $creditor->appendChild($this->creditorAddress->asDom($doc));
262
263 2
        return $creditor;
264
    }
265
266
    /**
267
     * Appends the purpose to the transaction
268
     *
269
     * @param \DOMDocument $doc
270
     * @param \DOMElement  $transaction
271
     */
272 2
    protected function appendPurpose(\DOMDocument $doc, \DOMElement $transaction)
273
    {
274 2
        if ($this->purpose !== null) {
275 2
            $purposeNode = $doc->createElement('Purp');
276 2
            $purposeNode->appendChild($this->purpose->asDom($doc));
277 2
            $transaction->appendChild($purposeNode);
278 2
        }
279 2
    }
280
281
    /**
282
     * Appends the remittance information to the transaction
283
     *
284
     * @param \DOMDocument $doc
285
     * @param \DOMElement  $transaction
286
     */
287 2
    protected function appendRemittanceInformation(\DOMDocument $doc, \DOMElement $transaction)
288
    {
289 2
        if ($this->remittanceInformation !== null) {
290 2
            $remittanceInformation = $doc->createElement('RmtInf');
291 2
            $remittanceInformation->appendChild($this->remittanceInformation->asDom($doc));
292 2
            $transaction->appendChild($remittanceInformation);
293 2
        }
294 2
    }
295
}
296