Completed
Push — master ( 63b231...bdcfdd )
by Hannes
01:31
created

TreeBuilder::addUpdateMandateRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-17 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Writer;
24
25
use byrokrat\autogiro\Tree\Record\RecordNode;
26
use byrokrat\autogiro\Tree\Record\Request\RequestOpeningRecordNode;
27
use byrokrat\autogiro\Tree\Record\Request\AcceptMandateRequestNode;
28
use byrokrat\autogiro\Tree\Record\Request\CreateMandateRequestNode;
29
use byrokrat\autogiro\Tree\Record\Request\DeleteMandateRequestNode;
30
use byrokrat\autogiro\Tree\Record\Request\RejectMandateRequestNode;
31
use byrokrat\autogiro\Tree\Record\Request\UpdateMandateRequestNode;
32
use byrokrat\autogiro\Tree\FileNode;
33
use byrokrat\autogiro\Tree\LayoutNode;
34
use byrokrat\autogiro\Tree\DateNode;
35
use byrokrat\autogiro\Tree\TextNode;
36
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
37
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
38
use byrokrat\autogiro\Tree\PayerNumberNode;
39
use byrokrat\autogiro\Tree\AccountNode;
40
use byrokrat\autogiro\Tree\IdNode;
41
use byrokrat\banking\AccountNumber;
42
use byrokrat\banking\Bankgiro;
43
use byrokrat\id\Id;
44
45
/**
46
 * Build trees representing autogiro request files
47
 */
48
class TreeBuilder
49
{
50
    /**
51
     * @var RequestOpeningRecordNode Opening record used for each layout
52
     */
53
    private $opening;
54
55
    /**
56
     * @var RecordNode[] List of created mandate records
57
     */
58
    private $mandateRecords;
59
60
    /**
61
     * @var RecordNode[] List of created transaction records
62
     */
63
    private $transactionRecords;
64
65
    /**
66
     * @var RecordNode[] List of created amendment records
67
     */
68
    private $amendmentRecords;
69
70
    /**
71
     * @var string Payee BGC customer number
72
     */
73
    private $bgcNr;
74
75
    /**
76
     * @var PayeeBankgiroNode Wrapper around payee bankgiro account number
77
     */
78
    private $payeeBgNode;
79
80
    /**
81
     * @var \DateTimeInterface Date of file creation
82
     */
83
    private $date;
84
85
    /**
86
     * @param string             $bgcNr    The BGC customer number of payee
87
     * @param Bankgiro           $bankgiro Payee bankgiro account number
88
     * @param \DateTimeInterface $date     Optional creation date
89
     */
90
    public function __construct(string $bgcNr, Bankgiro $bankgiro, \DateTimeInterface $date = null)
91
    {
92
        $this->bgcNr = $bgcNr;
93
        $this->payeeBgNode = (new PayeeBankgiroNode(0, $bankgiro->getNumber()))->setAttribute('account', $bankgiro);
94
        $this->date = $date ?: new \DateTimeImmutable;
95
        $this->reset();
96
    }
97
98
    /**
99
     * Reset builder to initial state
100
     */
101
    public function reset()
102
    {
103
        $this->opening = new RequestOpeningRecordNode(
104
            0,
105
            (new DateNode(0, $this->date->format('Ymd')))->setAttribute('date', $this->date),
106
            new TextNode(0, 'AUTOGIRO'),
107
            new TextNode(0, str_pad('', 44)),
108
            new PayeeBgcNumberNode(0, $this->bgcNr),
109
            $this->payeeBgNode,
110
            [new TextNode(0, '  ')]
111
        );
112
        $this->mandateRecords = [];
113
        $this->transactionRecords = [];
114
        $this->amendmentRecords = [];
115
    }
116
117
    /**
118
     * Add a new mandate record to tree
119
     */
120
    public function addCreateMandateRecord(string $payerNr, AccountNumber $account, Id $id)
121
    {
122
        $this->mandateRecords[] = new CreateMandateRequestNode(
123
            0,
124
            $this->payeeBgNode,
125
            new PayerNumberNode(0, $payerNr),
126
            (new AccountNode(0, $account->getNumber()))->setAttribute('account', $account),
127
            (new IdNode(0, (string)$id))->setAttribute('id', $id),
128
            [new TextNode(0, str_pad('', 24))]
129
        );
130
    }
131
132
    /**
133
     * Add a delete mandate record to tree
134
     */
135 View Code Duplication
    public function addDeleteMandateRecord(string $payerNr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $this->mandateRecords[] = new DeleteMandateRequestNode(
138
            0,
139
            $this->payeeBgNode,
140
            new PayerNumberNode(0, $payerNr),
141
            [new TextNode(0, str_pad('', 52))]
142
        );
143
    }
144
145
    /**
146
     * Add an accept digital mandate record to tree
147
     */
148 View Code Duplication
    public function addAcceptMandateRecord(string $payerNr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $this->mandateRecords[] = new AcceptMandateRequestNode(
151
            0,
152
            $this->payeeBgNode,
153
            new PayerNumberNode(0, $payerNr),
154
            [new TextNode(0, str_pad('', 52))]
155
        );
156
    }
157
158
    /**
159
     * Add a reject digital mandate record to tree
160
     */
161
    public function addRejectMandateRecord(string $payerNr)
162
    {
163
        $this->mandateRecords[] = new RejectMandateRequestNode(
164
            0,
165
            $this->payeeBgNode,
166
            new PayerNumberNode(0, $payerNr),
167
            new TextNode(0, str_pad('', 48)),
168
            new TextNode(0, 'AV'),
169
            [new TextNode(0, '  ')]
170
        );
171
    }
172
173
    /**
174
     * Add an update mandate record to tree
175
     */
176 View Code Duplication
    public function addUpdateMandateRecord(string $payerNr, string $newPayerNr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $this->mandateRecords[] = new UpdateMandateRequestNode(
179
            0,
180
            $this->payeeBgNode,
181
            new PayerNumberNode(0, $payerNr),
182
            $this->payeeBgNode,
183
            new PayerNumberNode(0, $newPayerNr),
184
            [new TextNode(0, str_pad('', 26))]
185
        );
186
    }
187
188
    /**
189
     * Get the created request tree
190
     */
191
    public function buildTree(): FileNode
192
    {
193
        $layouts = [];
194
195
        foreach (['mandateRecords', 'transactionRecords', 'amendmentRecords'] as $records) {
196
            if (!empty($this->$records)) {
197
                $layouts[] = new LayoutNode($this->opening, ...$this->$records);
198
            }
199
        }
200
201
        return new FileNode(...$layouts);
202
    }
203
}
204