Completed
Push — master ( 6bf385...7dc2a6 )
by z38
13:00 queued 02:55
created

BankCreditTransfer::asDom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 20
ccs 11
cts 13
cp 0.8462
rs 9.4285
cc 2
eloc 12
nc 2
nop 2
crap 2.0145
1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use DOMDocument;
6
use InvalidArgumentException;
7
use Z38\SwissPayment\BIC;
8
use Z38\SwissPayment\FinancialInstitutionInterface;
9
use Z38\SwissPayment\IBAN;
10
use Z38\SwissPayment\IID;
11
use Z38\SwissPayment\Money;
12
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
13
use Z38\SwissPayment\PostalAddressInterface;
14
15
/**
16
 * BankCreditTransfer contains all the information about a type 3 transaction.
17
 */
18
class BankCreditTransfer extends CreditTransfer
19
{
20
    /**
21
     * @var IBAN
22
     */
23
    protected $creditorIBAN;
24
25
    /**
26
     * @var FinancialInstitutionInterface
27
     */
28
    protected $creditorAgent;
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param IBAN    $creditorIBAN  IBAN of the creditor
34
     * @param BIC|IID $creditorAgent BIC or IID of the creditor's financial institution
35
     *
36
     * @throws \InvalidArgumentException When the amount is not in EUR or CHF or when the creditor agent is not BIC or IID.
37
     */
38 4
    public function __construct($instructionId, $endToEndId, Money\Money $amount, $creditorName, PostalAddressInterface $creditorAddress, IBAN $creditorIBAN, FinancialInstitutionInterface $creditorAgent)
39
    {
40 4
        if (!$amount instanceof Money\EUR && !$amount instanceof Money\CHF) {
41 1
            throw new InvalidArgumentException(sprintf(
42 1
                'The amount must be an instance of Z38\SwissPayment\Money\EUR or Z38\SwissPayment\Money\CHF (instance of %s given).',
43 1
                get_class($amount)
44 1
            ));
45
        }
46
47 3
        if (!$creditorAgent instanceof BIC && !$creditorAgent instanceof IID) {
48 1
            throw new InvalidArgumentException('The creditor agent must be an instance of BIC or IID.');
49
        }
50
51 2
        parent::__construct($instructionId, $endToEndId, $amount, $creditorName, $creditorAddress);
52
53 2
        $this->creditorIBAN = $creditorIBAN;
54 2
        $this->creditorAgent = $creditorAgent;
55 2
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function asDom(DOMDocument $doc, PaymentInformation $paymentInformation)
61
    {
62 2
        $root = $this->buildHeader($doc, $paymentInformation);
63
64 2
        $creditorAgent = $doc->createElement('CdtrAgt');
65 2
        $creditorAgent->appendChild($this->creditorAgent->asDom($doc));
66 2
        $root->appendChild($creditorAgent);
67
68 2
        $root->appendChild($this->buildCreditor($doc));
69
70 2
        $creditorAccount = $doc->createElement('CdtrAcct');
71 2
        $creditorAccount->appendChild($this->creditorIBAN->asDom($doc));
72 2
        $root->appendChild($creditorAccount);
73
74 2
        if ($this->hasRemittanceInformation()) {
75
            $root->appendChild($this->buildRemittanceInformation($doc));
76
        }
77
78 2
        return $root;
79
    }
80
}
81