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

BankCreditTransfer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 7
c 6
b 2
f 2
lcom 1
cbo 3
dl 0
loc 65
ccs 23
cts 25
cp 0.92
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 18 5
A asDom() 0 20 2
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