Completed
Push — master ( 0a5beb...6bf385 )
by z38
11:34
created

BankCreditTransfer::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 10
nc 3
nop 7
crap 5
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 3
     *
36
     * @throws \InvalidArgumentException When the amount is not in EUR or CHF or when the creditor agent is not BIC or IID.
37 3
     */
38
    public function __construct($instructionId, $endToEndId, Money\Money $amount, $creditorName, PostalAddressInterface $creditorAddress, IBAN $creditorIBAN, FinancialInstitutionInterface $creditorAgent)
39 3
    {
40 1
        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 2
                get_class($amount)
44 2
            ));
45 2
        }
46
47
        if (!$creditorAgent instanceof BIC && !$creditorAgent instanceof IID) {
48
            throw new InvalidArgumentException('The creditor agent must be an instance of BIC or IID.');
49
        }
50 2
51
        parent::__construct($instructionId, $endToEndId, $amount, $creditorName, $creditorAddress);
52 2
53
        $this->creditorIBAN = $creditorIBAN;
54 2
        $this->creditorAgent = $creditorAgent;
55 2
    }
56 2
57
    /**
58 2
     * {@inheritdoc}
59
     */
60 2
    public function asDom(DOMDocument $doc, PaymentInformation $paymentInformation)
61 2
    {
62 2
        $root = $this->buildHeader($doc, $paymentInformation);
63
64 2
        $creditorAgent = $doc->createElement('CdtrAgt');
65
        $creditorAgent->appendChild($this->creditorAgent->asDom($doc));
66
        $root->appendChild($creditorAgent);
67
68 2
        $root->appendChild($this->buildCreditor($doc));
69
70
        $creditorAccount = $doc->createElement('CdtrAcct');
71
        $creditorAccount->appendChild($this->creditorIBAN->asDom($doc));
72
        $root->appendChild($creditorAccount);
73
74
        if ($this->hasRemittanceInformation()) {
75
            $root->appendChild($this->buildRemittanceInformation($doc));
76
        }
77
78
        return $root;
79
    }
80
}
81