Completed
Push — feature-purposes ( ae1ada )
by z38
02:44
created

IS2CreditTransfer::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.4285
c 2
b 0
f 0
cc 3
eloc 10
nc 2
nop 8
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Z38\SwissPayment\TransactionInformation;
4
5
use DOMDocument;
6
use InvalidArgumentException;
7
use Z38\SwissPayment\IBAN;
8
use Z38\SwissPayment\Money;
9
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
10
use Z38\SwissPayment\PostalAccount;
11
use Z38\SwissPayment\PostalAddressInterface;
12
13
/**
14
 * IS2CreditTransfer contains all the information about a IS 2-stage (type 2.2) transaction.
15
 */
16
class IS2CreditTransfer extends CreditTransfer
17
{
18
    /**
19
     * @var IBAN
20
     */
21
    protected $creditorIBAN;
22
23
    /**
24
     * @var string
25
     */
26
    protected $creditorAgentName;
27
28
    /**
29
     * @var PostalAccount
30
     */
31
    protected $creditorAgentPostal;
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @param IBAN          $creditorIBAN        IBAN of the creditor
37
     * @param string        $creditorAgentName   Name of the creditor's financial institution
38
     * @param PostalAccount $creditorAgentPostal Postal account of the creditor's financial institution
39
     *
40
     * @throws \InvalidArgumentException When the amount is not in EUR or CHF.
41
     */
42 3
    public function __construct($instructionId, $endToEndId, Money\Money $amount, $creditorName, PostalAddressInterface $creditorAddress, IBAN $creditorIBAN, $creditorAgentName, PostalAccount $creditorAgentPostal)
43
    {
44 3
        if (!$amount instanceof Money\EUR && !$amount instanceof Money\CHF) {
45 1
            throw new InvalidArgumentException(sprintf(
46 1
                'The amount must be an instance of Z38\SwissPayment\Money\EUR or Z38\SwissPayment\Money\CHF (instance of %s given).',
47 1
                get_class($amount)
48 1
            ));
49
        }
50
51 2
        parent::__construct($instructionId, $endToEndId, $amount, $creditorName, $creditorAddress);
52
53 2
        $this->creditorIBAN = $creditorIBAN;
54 2
        $this->creditorAgentName = (string) $creditorAgentName;
55 2
        $this->creditorAgentPostal = $creditorAgentPostal;
56 2
        $this->localInstrument = 'CH03';
57 2
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function asDom(DOMDocument $doc, PaymentInformation $paymentInformation)
63
    {
64 2
        $root = $this->buildHeader($doc, $paymentInformation);
65
66 2
        $creditorAgent = $doc->createElement('CdtrAgt');
67 2
        $creditorAgentId = $doc->createElement('FinInstnId');
68 2
        $creditorAgentId->appendChild($doc->createElement('Nm', $this->creditorAgentName));
69 2
        $creditorAgentIdOther = $doc->createElement('Othr');
70 2
        $creditorAgentIdOther->appendChild($doc->createElement('Id', $this->creditorAgentPostal->format()));
71 2
        $creditorAgentId->appendChild($creditorAgentIdOther);
72 2
        $creditorAgent->appendChild($creditorAgentId);
73 2
        $root->appendChild($creditorAgent);
74
75 2
        $root->appendChild($this->buildCreditor($doc));
76
77 2
        $creditorAccount = $doc->createElement('CdtrAcct');
78 2
        $creditorAccount->appendChild($this->creditorIBAN->asDom($doc));
79 2
        $root->appendChild($creditorAccount);
80
81 2
        $this->appendPurpose($doc, $root);
82
83 2
        $this->appendRemittanceInformation($doc, $root);
84
85 2
        return $root;
86
    }
87
}
88