Completed
Pull Request — master (#22)
by z38
03:08 queued 01:44
created

ISRCreditTransfer::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.4742

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 15
cp 0.7332
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 3
nop 5
crap 5.4742
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use DOMDocument;
6
use InvalidArgumentException;
7
use LogicException;
8
use Z38\SwissPayment\ISRParticipant;
9
use Z38\SwissPayment\Money;
10
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
11
use Z38\SwissPayment\PostalAccount;
12
use Z38\SwissPayment\PostalAddressInterface;
13
use Z38\SwissPayment\Text;
14
15
/**
16
 * ISRCreditTransfer contains all the information about a ISR (type 1) transaction.
17
 */
18
class ISRCreditTransfer extends CreditTransfer
19
{
20
    /**
21
     * @var ISRParticipant
22
     */
23
    protected $creditorAccount;
24
25
    /**
26
     * @var string
27
     */
28
    protected $creditorReference;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param ISRParticipant $creditorAccount   ISR participation number of the creditor
34
     * @param string         $creditorReference ISR reference number
35
     *
36
     * @throws InvalidArgumentException When the amount or the creditor reference is invalid.
37
     */
38 4
    public function __construct($instructionId, $endToEndId, Money\Money $amount, ISRParticipant $creditorAccount, $creditorReference)
39
    {
40 4
        if (!$amount instanceof Money\EUR && !$amount instanceof Money\CHF) {
41
            throw new InvalidArgumentException(sprintf(
42
                'The amount must be an instance of Z38\SwissPayment\Money\EUR or Z38\SwissPayment\Money\CHF (instance of %s given).',
43
                get_class($amount)
44
            ));
45
        }
46
47 4
        if (!preg_match('/^[0-9]{1,27}$/', $creditorReference) || !PostalAccount::validateCheckDigit($creditorReference)) {
48 1
            throw new InvalidArgumentException('ISR creditor reference is invalid.');
49
        }
50
51 3
        $this->instructionId = Text::assertIdentifier($instructionId);
52 3
        $this->endToEndId = Text::assertIdentifier($endToEndId);
53 3
        $this->amount = $amount;
54 3
        $this->creditorAccount = $creditorAccount;
55 3
        $this->creditorReference = $creditorReference;
56 3
        $this->localInstrument = 'CH01';
57 3
    }
58
59
    /**
60
     * Sets creditor details
61
     *
62
     * @param string                 $creditorName
63
     * @param PostalAddressInterface $creditorAddress
64
     */
65 4
    public function setCreditorDetails($creditorName, PostalAddressInterface $creditorAddress)
66
    {
67 4
        $this->creditorName = Text::assert($creditorName, 70);
68 4
        $this->creditorAddress = $creditorAddress;
69 4
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function setRemittanceInformation($remittanceInformation)
75
    {
76 1
        throw new LogicException('ISR payments are not able to store unstructured remittance information.');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 2
    public function asDom(DOMDocument $doc, PaymentInformation $paymentInformation)
83
    {
84 2
        $root = $this->buildHeader($doc, $paymentInformation);
85
86 2
        if (strlen($this->creditorName) && isset($this->creditorAddress)) {
87 2
            $root->appendChild($this->buildCreditor($doc));
88 2
        }
89
90 2
        $creditorAccount = $doc->createElement('CdtrAcct');
91 2
        $creditorAccount->appendChild($this->creditorAccount->asDom($doc));
92 2
        $root->appendChild($creditorAccount);
93
94 2
        $this->appendPurpose($doc, $root);
95
96 2
        $this->appendRemittanceInformation($doc, $root);
97
98 2
        return $root;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 2
    protected function appendRemittanceInformation(\DOMDocument $doc, \DOMElement $transaction)
105
    {
106 2
        $remittanceInformation = $doc->createElement('RmtInf');
107
108 2
        $structured = $doc->createElement('Strd');
109 2
        $remittanceInformation->appendChild($structured);
110
111 2
        $creditorReferenceInformation = $doc->createElement('CdtrRefInf');
112 2
        $structured->appendChild($creditorReferenceInformation);
113
114 2
        $creditorReferenceInformation->appendChild($doc->createElement('Ref', $this->creditorReference));
115
116 2
        $transaction->appendChild($remittanceInformation);
117 2
    }
118
}
119