Completed
Pull Request — master (#6)
by
unknown
03:15
created

CreditTransfer::setRemittanceInformation()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use Z38\SwissPayment\IntermediarySwift;
6
use Z38\SwissPayment\Money\Money;
7
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
8
use Z38\SwissPayment\PostalAddressInterface;
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 $remittanceInformation;
44
45
    /**
46
     * @var IntermediarySwift
47
     */
48
    protected $intermediarySwift;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param string                 $instructionId   Identifier of the instruction (should be unique within the message)
54
     * @param string                 $endToEndId      End-To-End Identifier of the instruction (passed unchanged along the complete processing chain)
55
     * @param Money                  $amount          Amount of money to be transferred
56
     * @param string                 $creditorName    Name of the creditor
57
     * @param PostalAddressInterface $creditorAddress Address of the creditor
58
     * @param IntermediarySwift      $intermediarySwift
59
     */
60 3
    public function __construct($instructionId, $endToEndId, Money $amount, $creditorName, PostalAddressInterface $creditorAddress, $intermediarySwift = null)
61
    {
62 3
        $this->instructionId = (string) $instructionId;
63 3
        $this->endToEndId = (string) $endToEndId;
64 3
        $this->amount = $amount;
65 3
        $this->creditorName = (string) $creditorName;
66 3
        $this->creditorAddress = $creditorAddress;
67 3
        $this->remittanceInformation = null;
68 3
        if ($intermediarySwift) {
69 1
            $this->setIntermediarySwift($intermediarySwift);
70 1
        }
71 3
    }
72
73
    /**
74
     * Sets the unstructured remittance information
75
     *
76
     * @param string|null $remittanceInformation
77
     *
78
     * @return CreditTransfer This credit transfer
79
     */
80
    public function setRemittanceInformation($remittanceInformation)
81
    {
82
        $this->remittanceInformation = $remittanceInformation;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Gets the instructed amount of this transaction
89
     *
90
     * @return Money The instructed amount
91
     */
92 2
    public function getAmount()
93
    {
94 2
        return $this->amount;
95
    }
96
97
    /**
98
     * Builds a DOM tree of this transaction
99
     *
100
     * @param \DOMDocument       $doc
101
     * @param PaymentInformation $paymentInformation Information on B-level
102
     *
103
     * @return \DOMElement The built DOM tree
104
     */
105
    abstract public function asDom(\DOMDocument $doc, PaymentInformation $paymentInformation);
106
107
    /**
108
     * Builds a DOM tree of this transaction and adds header nodes
109
     *
110
     * @param \DOMDocument       $doc
111
     * @param PaymentInformation $paymentInformation The corresponding B-level element
112
     * @param string|null        $localInstrument    Local instrument
113
     * @param string|null        $serviceLevel       Service level
114
     *
115
     * @return \DOMNode The built DOM node
116
     */
117 2
    protected function buildHeader(\DOMDocument $doc, PaymentInformation $paymentInformation, $localInstrument = null, $serviceLevel = null)
118
    {
119 2
        $root = $doc->createElement('CdtTrfTxInf');
120
121 2
        $id = $doc->createElement('PmtId');
122 2
        $id->appendChild($doc->createElement('InstrId', $this->instructionId));
123 2
        $id->appendChild($doc->createElement('EndToEndId', $this->endToEndId));
124 2
        $root->appendChild($id);
125
126 2
        if ($paymentInformation->hasPaymentTypeInformation()) {
127 2
            if ($paymentInformation->getLocalInstrument() !== $localInstrument) {
128
                throw new \LogicException('You can not set the local instrument on B- and C-level.');
129
            }
130 2
            if ($paymentInformation->getServiceLevel() !== $serviceLevel) {
131
                throw new \LogicException('You can not set the service level on B- and C-level.');
132
            }
133 2
        } elseif (!empty($localInstrument) || !empty($serviceLevel)) {
134 2
            $paymentType = $doc->createElement('PmtTpInf');
135 2
            if (!empty($localInstrument)) {
136 2
                $localInstrumentNode = $doc->createElement('LclInstrm');
137 2
                $localInstrumentNode->appendChild($doc->createElement('Prtry', $localInstrument));
138 2
                $paymentType->appendChild($localInstrumentNode);
139 2
            }
140 2
            if (!empty($serviceLevel)) {
141 2
                $serviceLevelNode = $doc->createElement('SvcLvl');
142 2
                $serviceLevelNode->appendChild($doc->createElement('Cd', $serviceLevel));
143 2
                $paymentType->appendChild($serviceLevelNode);
144 2
            }
145 2
            $root->appendChild($paymentType);
146 2
        }
147
148 2
        $amount = $doc->createElement('Amt');
149 2
        $instdAmount = $doc->createElement('InstdAmt', $this->amount->format());
150 2
        $instdAmount->setAttribute('Ccy', $this->amount->getCurrency());
151 2
        $amount->appendChild($instdAmount);
152 2
        $root->appendChild($amount);
153
154 2
        return $root;
155
    }
156
157
    /**
158
     * Builds a DOM node of the Creditor field
159
     *
160
     * @param \DOMDocument $doc
161
     *
162
     * @return \DOMNode The built DOM node
163
     */
164 2
    protected function buildCreditor(\DOMDocument $doc)
165
    {
166 2
        $creditor = $doc->createElement('Cdtr');
167 2
        $creditor->appendChild($doc->createElement('Nm', htmlentities($this->creditorName)));
168 2
        $creditor->appendChild($this->creditorAddress->asDom($doc));
169
170 2
        return $creditor;
171
    }
172
173
    /**
174
     * Indicates whether remittance information is set
175
     *
176
     * @return bool true if remittance information is set
177
     */
178 2
    protected function hasRemittanceInformation()
179
    {
180 2
        return !empty($this->remittanceInformation);
181
    }
182
183
    /**
184
     * Builds a DOM node of the Remittance Information field
185
     *
186
     * @param \DOMDocument $doc
187
     *
188
     * @return \DOMNode The built DOM node
189
     *
190
     * @throws \LogicException When no remittance information is set
191
     */
192
    protected function buildRemittanceInformation(\DOMDocument $doc)
193
    {
194
        if ($this->hasRemittanceInformation()) {
195
            $remittance = $doc->createElement('RmtInf');
196
            $remittance->appendChild($doc->createElement('Ustrd', $this->remittanceInformation));
197
198
            return $remittance;
199
        } else {
200
            throw new \LogicException('Can not build node without data.');
201
        }
202
    }
203
204
    /**
205
     * @return IntermediarySwift
206
     */
207 1
    public function getIntermediarySwift()
208
    {
209 1
        return $this->intermediarySwift;
210
    }
211
212
    /**
213
     * @param IntermediarySwift $intermediarySwift
214
     */
215 1
    public function setIntermediarySwift(IntermediarySwift $intermediarySwift)
216
    {
217 1
        $this->intermediarySwift = $intermediarySwift;
218 1
    }
219
}
220