Completed
Push — master ( 94f056...38eb88 )
by z38
02:49
created

CustomerCreditTransfer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.11%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 113
ccs 35
cts 38
cp 0.9211
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setCreationTime() 0 6 1
A addPayment() 0 6 1
A getPaymentCount() 0 4 1
A getSchemaName() 0 4 1
B buildDom() 0 27 3
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 3
    public function __construct($id, $initiatingParty)
40
    {
41 3
        $this->id = (string) $id;
42 3
        $this->initiatingParty = (string) $initiatingParty;
43 3
        $this->payments = [];
44 3
        $this->creationTime = new \DateTime();
45 3
    }
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 3
    public function addPayment(PaymentInformation $payment)
69
    {
70 3
        $this->payments[] = $payment;
71
72 3
        return $this;
73
    }
74
75
    /**
76
     * Gets the number of payments
77
     *
78
     * @return int Number of payments
79
     */
80 1
    public function getPaymentCount()
81
    {
82 1
        return count($this->payments);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    protected function getSchemaName()
89
    {
90 2
        return 'pain.001.001.03.ch.02.xsd';
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 2
    protected function buildDom(\DOMDocument $doc)
97
    {
98 2
        $transactionCount = 0;
99 2
        $transactionSum = new Money\Mixed(0);
100 2
        foreach ($this->payments as $payment) {
101 2
            $transactionCount += $payment->getTransactionCount();
102 2
            $transactionSum = $transactionSum->plus($payment->getTransactionSum());
103 2
        }
104
105 2
        $root = $doc->createElement('CstmrCdtTrfInitn');
106 2
        $header = $doc->createElement('GrpHdr');
107 2
        $header->appendChild($doc->createElement('MsgId', $this->id));
108 2
        $header->appendChild($doc->createElement('CreDtTm', $this->creationTime->format('Y-m-d\TH:i:sP')));
109 2
        $header->appendChild($doc->createElement('NbOfTxs', $transactionCount));
110 2
        $header->appendChild($doc->createElement('CtrlSum', $transactionSum->format()));
111 2
        $initgParty = $doc->createElement('InitgPty');
112 2
        $initgParty->appendChild($doc->createElement('Nm', $this->initiatingParty));
113 2
        $initgParty->appendChild($this->buildContactDetails($doc));
114 2
        $header->appendChild($initgParty);
115 2
        $root->appendChild($header);
116
117 2
        foreach ($this->payments as $payment) {
118 2
            $root->appendChild($payment->asDom($doc));
119 2
        }
120
121 2
        return $root;
122
    }
123
}
124