Completed
Push — master ( 94f056...38eb88 )
by z38
02:49
created

ISRCreditTransfer::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.3035

Importance

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