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

PaymentInformation::addIntermediarySwift()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Z38\SwissPayment\PaymentInformation;
4
5
use Z38\SwissPayment\BC;
6
use Z38\SwissPayment\BIC;
7
use Z38\SwissPayment\FinancialInstitutionInterface;
8
use Z38\SwissPayment\IBAN;
9
use Z38\SwissPayment\IntermediarySwift;
10
use Z38\SwissPayment\Money;
11
use Z38\SwissPayment\TransactionInformation\CreditTransfer;
12
13
/**
14
 * PaymentInformation contains a group of transactions as well as details about the debtor
15
 */
16
class PaymentInformation
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $id;
22
23
    /**
24
     * @var array
25
     */
26
    protected $transactions;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $batchBooking;
32
33
    /**
34
     * @var string|null
35
     */
36
    protected $serviceLevel;
37
38
    /**
39
     * @var string|null
40
     */
41
    protected $localInstrument;
42
43
    /**
44
     * @var \DateTime
45
     */
46
    protected $executionDate;
47
48
    /**
49
     * @var string
50
     */
51
    protected $debtorName;
52
53
    /**
54
     * @var FinancialInstitutionInterface
55
     */
56
    protected $debtorAgent;
57
58
    /**
59
     * @var IBAN
60
     */
61
    protected $debtorIBAN;
62
63
    /**
64
     * @var IntermediarySwift
65
     */
66
    protected $intermediarySwift;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param string $id          Identifier of this group (should be unique within a message)
72
     * @param string $debtorName  Name of the debtor
73
     * @param BC|BIC $debtorAgent BC or BIC of the debtor's financial institution
74
     * @param IBAN   $debtorIBAN  IBAN of the debtor's account
75
     */
76 3
    public function __construct($id, $debtorName, FinancialInstitutionInterface $debtorAgent, IBAN $debtorIBAN)
77
    {
78 3
        if (!$debtorAgent instanceof BC && !$debtorAgent instanceof BIC) {
79 1
            throw new \InvalidArgumentException('The debtor agent must be an instance of BC or BIC.');
80
        }
81
82 2
        $this->id = (string) $id;
83 2
        $this->transactions = array();
84 2
        $this->batchBooking = true;
85 2
        $this->executionDate = new \DateTime();
86 2
        $this->debtorName = (string) $debtorName;
87 2
        $this->debtorAgent = $debtorAgent;
88 2
        $this->debtorIBAN = $debtorIBAN;
89 2
    }
90
91
    /**
92
     * Adds a single transaction to this payment
93
     *
94
     * @param CreditTransfer $transaction The transaction to be added
95
     *
96
     * @return PaymentInformation This payment instruction
97
     */
98 2
    public function addTransaction(CreditTransfer $transaction)
99
    {
100 2
        $this->transactions[] = $transaction;
101
102 2
        return $this;
103
    }
104
105
    /**
106
     * Gets the number of transactions
107
     *
108
     * @return int Number of transactions
109
     */
110 2
    public function getTransactionCount()
111
    {
112 2
        return count($this->transactions);
113
    }
114
115
    /**
116
     * Gets the sum of transactions
117
     *
118
     * @return Money\Mixed Sum of transactions
119
     */
120 2
    public function getTransactionSum()
121
    {
122 2
        $sum = new Money\Mixed(0);
123
124 2
        foreach ($this->transactions as $transaction) {
125 2
            $sum = $sum->plus($transaction->getAmount());
126 2
        }
127
128 2
        return $sum;
129
    }
130
131
    /**
132
     * Sets the required execution date.
133
     * Where appropriate, the value data is automatically modified to the next possible banking/Post Office working day.
134
     *
135
     * @param \DateTime $executionDate
136
     *
137
     * @return PaymentInformation This payment instruction
138
     */
139
    public function setExecutionDate(\DateTime $executionDate)
140
    {
141
        $this->executionDate = $executionDate;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Sets the batch booking option.
148
     * It is recommended that one payment instruction is created for each currency transferred.
149
     *
150
     * @param bool $batchBooking
151
     *
152
     * @return PaymentInformation This payment instruction
153
     */
154
    public function setBatchBooking($batchBooking)
155
    {
156
        $this->batchBooking = boolval($batchBooking);
157
158
        return $this;
159
    }
160
161
    /**
162
     * Checks whether the payment type information is included on B- or C-level
163
     *
164
     * @return bool true if it is included on B-level
165
     */
166 4
    public function hasPaymentTypeInformation()
167
    {
168 4
        return ($this->localInstrument !== null || $this->serviceLevel !== null);
169
    }
170
171
    /**
172
     * Gets the local instrument
173
     *
174
     * @return string|null The local instrument
175
     */
176 2
    public function getLocalInstrument()
177
    {
178 2
        return $this->localInstrument;
179
    }
180
181
    /**
182
     * Gets the service level
183
     *
184
     * @return string|null The service level
185
     */
186 2
    public function getServiceLevel()
187
    {
188 2
        return $this->serviceLevel;
189
    }
190
191
    public function addIntermediarySwift(IntermediarySwift $intermediarySwift)
192
    {
193
        $this->intermediarySwift = $intermediarySwift;
194
    }
195
196
    /**
197
     * Builds a DOM tree of this payment instruction
198
     *
199
     * @param \DOMDocument $doc
200
     *
201
     * @return \DOMElement The built DOM tree
202
     */
203 4
    public function asDom(\DOMDocument $doc)
204
    {
205 4
        $root = $doc->createElement('PmtInf');
206
207 4
        $root->appendChild($doc->createElement('PmtInfId', $this->id));
208 4
        $root->appendChild($doc->createElement('PmtMtd', 'TRF'));
209 4
        $root->appendChild($doc->createElement('BtchBookg', ($this->batchBooking ? 'true' : 'false')));
210
211 4
        if ($this->hasPaymentTypeInformation()) {
212 4
            $paymentType = $doc->createElement('PmtTpInf');
213 4
            if ($this->localInstrument !== null) {
214
                $localInstrumentNode = $doc->createElement('LclInstrm');
215
                $localInstrumentNode->appendChild($doc->createElement('Prtry', $this->localInstrument));
216
                $paymentType->appendChild($localInstrumentNode);
217
            }
218 4
            if ($this->serviceLevel !== null) {
219 4
                $serviceLevelNode = $doc->createElement('SvcLvl');
220 4
                $serviceLevelNode->appendChild($doc->createElement('Cd', $this->serviceLevel));
221 4
                $paymentType->appendChild($serviceLevelNode);
222 4
            }
223 4
            $root->appendChild($paymentType);
224 4
        }
225
226 4
        $root->appendChild($doc->createElement('ReqdExctnDt', $this->executionDate->format('Y-m-d')));
227
228 4
        $debtor = $doc->createElement('Dbtr');
229 4
        $debtor->appendChild($doc->createElement('Nm', $this->debtorName));
230 4
        $root->appendChild($debtor);
231
232 4
        $debtorAccount = $doc->createElement('DbtrAcct');
233 4
        $debtorAccountId = $doc->createElement('Id');
234 4
        $debtorAccountId->appendChild($doc->createElement('IBAN', $this->debtorIBAN->normalize()));
235 4
        $debtorAccount->appendChild($debtorAccountId);
236 4
        $root->appendChild($debtorAccount);
237
238 4
        $debtorAgent = $doc->createElement('DbtrAgt');
239 4
        $debtorAgent->appendChild($this->debtorAgent->asDom($doc));
240 4
        $root->appendChild($debtorAgent);
241
242 4
        foreach ($this->transactions as $transaction) {
243 4
            $root->appendChild($transaction->asDom($doc, $this));
244 3
        }
245
246 3
        return $root;
247
    }
248
}
249