Completed
Push — feature-purposes ( ae1ada )
by z38
02:44
created

CreditTransfer::getServiceLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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