Completed
Pull Request — master (#19)
by
unknown
05:54
created

CustomerCreditTransfer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Z38\SwissPayment\Message;
4
5
use Z38\SwissPayment\Money;
6
use Z38\SwissPayment\PaymentInformation\PaymentInformation;
7
8
/**
9
 * CustomerCreditTransfer represents a Customer Credit Transfer Initiation (pain.001) message
10
 */
11
class CustomerCreditTransfer extends AbstractMessage
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $id;
17
18
    /**
19
     * @var string
20
     */
21
    protected $initiatingParty;
22
23
    /**
24
     * @var array
25
     */
26
    protected $payments;
27
28
    /**
29
     * @var \DateTime
30
     */
31
    protected $creationTime;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param string $id              Identifier of the message (should usually be unique over a period of at least 90 days)
37
     * @param string $initiatingParty Name of the initiating party
38
     */
39
    public function __construct($id, $initiatingParty)
40
    {
41
        $this->id = (string) $id;
42
        $this->initiatingParty = (string) $initiatingParty;
43
        $this->payments = [];
44
        $this->creationTime = new \DateTime();
45
    }
46
47
    /**
48
     * Manually sets the creation time
49
     *
50
     * @param \DateTime $creationTime The desired creation time
51
     *
52
     * @return CustomerCreditTransfer This message
53
     */
54
    public function setCreationTime(\DateTime $creationTime)
55
    {
56
        $this->creationTime = $creationTime;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Adds a payment instruction
63
     *
64
     * @param PaymentInformation $payment The payment to be added
65
     *
66
     * @return CustomerCreditTransfer This message
67
     */
68
    public function addPayment(PaymentInformation $payment)
69
    {
70
        $this->payments[] = $payment;
71
72
        return $this;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function getSchemaName()
79
    {
80
        return 'pain.001.001.03.ch.02.xsd';
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function buildDom(\DOMDocument $doc)
87
    {
88
        $transactionCount = 0;
89
        $transactionSum = new Money\Mixed(0);
90
        foreach ($this->payments as $payment) {
91
            $transactionCount += $payment->getTransactionCount();
92
            $transactionSum = $transactionSum->plus($payment->getTransactionSum());
93
        }
94
95
        $root = $doc->createElement('CstmrCdtTrfInitn');
96
        $header = $doc->createElement('GrpHdr');
97
        $header->appendChild($doc->createElement('MsgId', $this->id));
98
        $header->appendChild($doc->createElement('CreDtTm', $this->creationTime->format('Y-m-d\TH:i:sP')));
99
        $header->appendChild($doc->createElement('NbOfTxs', $transactionCount));
100
        $header->appendChild($doc->createElement('CtrlSum', $transactionSum->format()));
101
        $initgParty = $doc->createElement('InitgPty');
102
        $initgParty->appendChild($doc->createElement('Nm', $this->initiatingParty));
103
        $initgParty->appendChild($this->buildContactDetails($doc));
104
        $header->appendChild($initgParty);
105
        $root->appendChild($header);
106
107
        foreach ($this->payments as $payment) {
108
            $root->appendChild($payment->asDom($doc));
109
        }
110
111
        return $root;
112
    }
113
}
114