Completed
Push — master ( 2e59d2...61894c )
by z38
02:24
created

CreditTransfer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 80.7%

Importance

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