Completed
Pull Request — master (#9)
by Yann
03:38
created

BankCreditTransfer::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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