Completed
Pull Request — master (#40)
by
unknown
01:22
created

appendRemittanceInformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 9.536
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use DOMDocument;
6
use DOMElement;
7
use InvalidArgumentException;
8
use Z38\SwissPayment\FinancialInstitutionInterface;
9
use Z38\SwissPayment\IBAN;
10
use Z38\SwissPayment\Money;
11
use Z38\SwissPayment\PostalAccount;
12
13
/**
14
 * BankCreditTransfer contains all the information about a type 3 transaction
15
 * for a QR-Bill with Creditor Reference (SCOR)
16
 */
17
class BankCreditTransferWithCreditorReference extends BankCreditTransfer
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $creditorReference;
23
24
    /**
25
     * BankCreditTransferWithQRR constructor.
26
     * @param $instructionId
27
     * @param $endToEndId
28
     * @param Money\Money $amount
29
     * @param $creditorName
30
     * @param $creditorAddress
31
     * @param IBAN $creditorIBAN  IBAN of the creditor
32
     * @param FinancialInstitutionInterface $creditorAgent BIC or IID of the creditor's financial institution
33
     * @param string $creditorReference QR reference number (QRR)
34
     */
35 3
    public function __construct(
36
        $instructionId,
37
        $endToEndId,
38
        Money\Money $amount,
39
        $creditorName,
40
        $creditorAddress,
41
        IBAN $creditorIBAN,
42
        FinancialInstitutionInterface $creditorAgent,
43
        $creditorReference
44
    ) {
45 3
        $cleanedCreditorReference = str_replace(' ', '', strtoupper($creditorReference));
46 3
        if (!preg_match('/^RF/', $cleanedCreditorReference)) {
47
            throw new InvalidArgumentException('The creditor reference (SCOR) must starts with RF : ISO-11649');
48
        }
49 3
        $this->creditorReference = $cleanedCreditorReference;
50
51 3
        if (preg_match('/^CH[0-9]{2}3/', $creditorIBAN->normalize())) {
52
            throw new InvalidArgumentException('The IBAN must not be a QR-IBAN');
53
        }
54
55 3
        parent::__construct($instructionId, $endToEndId, $amount, $creditorName, $creditorAddress, $creditorIBAN, $creditorAgent);
56 3
    }
57
58
    /**
59
     * @param DOMDocument $doc
60
     * @param DOMElement $transaction
61
     */
62 2
    protected function appendRemittanceInformation(DOMDocument $doc, DOMElement $transaction)
63
    {
64 2
        $remittanceInformation = $doc->createElement('RmtInf');
65
66 2
        $structured = $doc->createElement('Strd');
67 2
        $remittanceInformation->appendChild($structured);
68
69 2
        $creditorReferenceInformation = $doc->createElement('CdtrRefInf');
70 2
        $structured->appendChild($creditorReferenceInformation);
71
72 2
        $codeOrProperty = $doc->createElement('CdOrPrtry');
73 2
        $codeOrProperty->appendChild($doc->createElement('Cd', 'SCOR'));
74 2
        $type = $doc->createElement('Tp');
75 2
        $type->appendChild($codeOrProperty);
76
77 2
        $creditorReferenceInformation->appendChild($type);
78 2
        $creditorReferenceInformation->appendChild($doc->createElement('Ref', $this->creditorReference));
79
80 2
        if (!empty($this->remittanceInformation)) {
81 2
            $structured->appendChild($doc->createElement('AddtlRmtInf', $this->remittanceInformation));
82 2
        }
83
84 2
        $transaction->appendChild($remittanceInformation);
85 2
    }
86
}
87