Completed
Push — next ( 5636ac...7fba3e )
by z38
05:38 queued 01:00
created

CreditTransfer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Test Coverage

Coverage 89.39%

Importance

Changes 0
Metric Value
wmc 18
lcom 4
cbo 5
dl 0
loc 226
ccs 59
cts 66
cp 0.8939
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
asDom() 0 1 ?
A __construct() 0 8 1
A getLocalInstrument() 0 4 1
A getServiceLevel() 0 4 1
A setPurpose() 0 6 1
A setRemittanceInformation() 0 6 1
A getAmount() 0 4 1
B buildHeader() 0 32 6
A buildCreditor() 0 10 2
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\Text;
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|null
32
     */
33
    protected $creditorAddress;
34
35
    /**
36
     * @var Money
37
     */
38
    protected $amount;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $localInstrument;
44
45
    /**
46
     * @var string|null
47
     */
48
    protected $serviceLevel;
49
50
    /**
51
     * @var PurposeCode|null
52
     */
53
    protected $purpose;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $remittanceInformation;
59
60
    /**
61
     * Constructor
62
     *
63
     * @param string                      $instructionId   Identifier of the instruction (should be unique within the message)
64
     * @param string                      $endToEndId      End-To-End Identifier of the instruction (passed unchanged along the complete processing chain)
65
     * @param Money                       $amount          Amount of money to be transferred
66
     * @param string                      $creditorName    Name of the creditor
67
     * @param PostalAddressInterface|null $creditorAddress Address of the creditor
68
     *
69
     * @throws \InvalidArgumentException When any of the inputs contain invalid characters or are too long.
70
     */
71 3
    public function __construct($instructionId, $endToEndId, Money $amount, $creditorName, PostalAddressInterface $creditorAddress = null)
72
    {
73 3
        $this->instructionId = Text::assertIdentifier($instructionId);
74 3
        $this->endToEndId = Text::assertIdentifier($endToEndId);
75 3
        $this->amount = $amount;
76 3
        $this->creditorName = Text::assert($creditorName, 70);
77 3
        $this->creditorAddress = $creditorAddress;
78 3
    }
79
80
    /**
81
     * Gets the local instrument
82
     *
83
     * @return string|null The local instrument
84
     */
85 2
    public function getLocalInstrument()
86
    {
87 2
        return $this->localInstrument;
88
    }
89
90
    /**
91
     * Gets the service level
92
     *
93
     * @return string|null The service level
94
     */
95 2
    public function getServiceLevel()
96
    {
97 2
        return $this->serviceLevel;
98
    }
99
100
    /**
101
     * Sets the purpose of the payment
102
     *
103
     * @param PurposeCode $purpose The purpose
104
     *
105
     * @return CreditTransfer This credit transfer
106
     */
107 3
    public function setPurpose(PurposeCode $purpose)
108
    {
109 3
        $this->purpose = $purpose;
110
111 3
        return $this;
112
    }
113
114
    /**
115
     * Sets the unstructured remittance information
116
     *
117
     * @param string|null $remittanceInformation
118
     *
119
     * @return CreditTransfer This credit transfer
120
     *
121
     * @throws \InvalidArgumentException When the information contains invalid characters or is too long.
122
     */
123
    public function setRemittanceInformation($remittanceInformation)
124
    {
125
        $this->remittanceInformation = Text::assertOptional($remittanceInformation, 140);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Gets the instructed amount of this transaction
132
     *
133
     * @return Money The instructed amount
134
     */
135 2
    public function getAmount()
136
    {
137 2
        return $this->amount;
138
    }
139
140
    /**
141
     * Builds a DOM tree of this transaction
142
     *
143
     * @param \DOMDocument       $doc
144
     * @param PaymentInformation $paymentInformation Information on B-level
145
     *
146
     * @return \DOMElement The built DOM tree
147
     */
148
    abstract public function asDom(\DOMDocument $doc, PaymentInformation $paymentInformation);
149
150
    /**
151
     * Builds a DOM tree of this transaction and adds header nodes
152
     *
153
     * @param \DOMDocument       $doc
154
     * @param PaymentInformation $paymentInformation The corresponding B-level element
155
     *
156
     * @return \DOMNode The built DOM node
157
     */
158 2
    protected function buildHeader(\DOMDocument $doc, PaymentInformation $paymentInformation)
159
    {
160 2
        $root = $doc->createElement('CdtTrfTxInf');
161
162 2
        $id = $doc->createElement('PmtId');
163 2
        $id->appendChild(Text::xml($doc, 'InstrId', $this->instructionId));
164 2
        $id->appendChild(Text::xml($doc, 'EndToEndId', $this->endToEndId));
165 2
        $root->appendChild($id);
166
167 2
        if (!$paymentInformation->hasPaymentTypeInformation() && ($this->localInstrument !== null || $this->serviceLevel !== null)) {
168 2
            $paymentType = $doc->createElement('PmtTpInf');
169 2
            if ($this->localInstrument !== null) {
170 2
                $localInstrumentNode = $doc->createElement('LclInstrm');
171 2
                $localInstrumentNode->appendChild($doc->createElement('Prtry', $this->localInstrument));
172 2
                $paymentType->appendChild($localInstrumentNode);
173 2
            }
174 2
            if ($this->serviceLevel !== null) {
175 2
                $serviceLevelNode = $doc->createElement('SvcLvl');
176 2
                $serviceLevelNode->appendChild($doc->createElement('Cd', $this->serviceLevel));
177 2
                $paymentType->appendChild($serviceLevelNode);
178 2
            }
179 2
            $root->appendChild($paymentType);
180 2
        }
181
182 2
        $amount = $doc->createElement('Amt');
183 2
        $instdAmount = $doc->createElement('InstdAmt', $this->amount->format());
184 2
        $instdAmount->setAttribute('Ccy', $this->amount->getCurrency());
185 2
        $amount->appendChild($instdAmount);
186 2
        $root->appendChild($amount);
187
188 2
        return $root;
189
    }
190
191
    /**
192
     * Builds a DOM node of the Creditor field
193
     *
194
     * @param \DOMDocument $doc
195
     *
196
     * @return \DOMNode The built DOM node
197
     */
198 2
    protected function buildCreditor(\DOMDocument $doc)
199
    {
200 2
        $creditor = $doc->createElement('Cdtr');
201 2
        $creditor->appendChild(Text::xml($doc, 'Nm', $this->creditorName));
202 2
        if ($this->creditorAddress !== null) {
203 2
            $creditor->appendChild($this->creditorAddress->asDom($doc));
204 2
        }
205
206 2
        return $creditor;
207
    }
208
209
    /**
210
     * Appends the purpose to the transaction
211
     *
212
     * @param \DOMDocument $doc
213
     * @param \DOMElement  $transaction
214
     */
215 2
    protected function appendPurpose(\DOMDocument $doc, \DOMElement $transaction)
216
    {
217 2
        if ($this->purpose !== null) {
218 2
            $purposeNode = $doc->createElement('Purp');
219 2
            $purposeNode->appendChild($this->purpose->asDom($doc));
220 2
            $transaction->appendChild($purposeNode);
221 2
        }
222 2
    }
223
224
    /**
225
     * Appends the remittance information to the transaction
226
     *
227
     * @param \DOMDocument $doc
228
     * @param \DOMElement  $transaction
229
     */
230 2
    protected function appendRemittanceInformation(\DOMDocument $doc, \DOMElement $transaction)
231
    {
232 2
        if (!empty($this->remittanceInformation)) {
233
            $remittanceNode = $doc->createElement('RmtInf');
234
            $remittanceNode->appendChild(Text::xml($doc, 'Ustrd', $this->remittanceInformation));
235
            $transaction->appendChild($remittanceNode);
236
        }
237 2
    }
238
}
239