Completed
Push — master ( bdcfdd...ceeabd )
by Hannes
01:33
created

TreeBuilder::addRejectMandateRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
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\AcceptDigitalMandateRequestNode;
28
use byrokrat\autogiro\Tree\Record\Request\CreateMandateRequestNode;
29
use byrokrat\autogiro\Tree\Record\Request\DeleteMandateRequestNode;
30
use byrokrat\autogiro\Tree\Record\Request\RejectDigitalMandateRequestNode;
31
use byrokrat\autogiro\Tree\Record\Request\UpdateMandateRequestNode;
32
use byrokrat\autogiro\Tree\Record\Request\IncomingTransactionRequestNode;
33
use byrokrat\autogiro\Tree\Record\Request\OutgoingTransactionRequestNode;
34
use byrokrat\autogiro\Tree\FileNode;
35
use byrokrat\autogiro\Tree\LayoutNode;
36
use byrokrat\autogiro\Tree\DateNode;
37
use byrokrat\autogiro\Tree\ImmediateDateNode;
38
use byrokrat\autogiro\Tree\TextNode;
39
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
40
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
41
use byrokrat\autogiro\Tree\PayerNumberNode;
42
use byrokrat\autogiro\Tree\AccountNode;
43
use byrokrat\autogiro\Tree\IdNode;
44
use byrokrat\autogiro\Tree\IntervalNode;
45
use byrokrat\autogiro\Tree\RepetitionsNode;
46
use byrokrat\autogiro\Tree\AmountNode;
47
use byrokrat\banking\AccountNumber;
48
use byrokrat\banking\Bankgiro;
49
use byrokrat\id\Id;
50
use byrokrat\amount\Currency\SEK;
51
52
/**
53
 * Build trees representing autogiro request files
54
 */
55
class TreeBuilder
56
{
57
    /**
58
     * @var RequestOpeningRecordNode Opening record used for each layout
59
     */
60
    private $opening;
61
62
    /**
63
     * @var RecordNode[] List of created mandate records
64
     */
65
    private $mandateRecords;
66
67
    /**
68
     * @var RecordNode[] List of created transaction records
69
     */
70
    private $transactionRecords;
71
72
    /**
73
     * @var RecordNode[] List of created amendment records
74
     */
75
    private $amendmentRecords;
76
77
    /**
78
     * @var string Payee BGC customer number
79
     */
80
    private $bgcNr;
81
82
    /**
83
     * @var PayeeBankgiroNode Wrapper around payee bankgiro account number
84
     */
85
    private $payeeBgNode;
86
87
    /**
88
     * @var \DateTimeInterface Date of file creation
89
     */
90
    private $date;
91
92
    /**
93
     * @var IntervalFormatter
94
     */
95
    private $intervalFormatter;
96
97
    /**
98
     * @var RepititionsFormatter
99
     */
100
    private $repititionsFormatter;
101
102
    /**
103
     * @param string               $bgcNr                The BGC customer number of payee
104
     * @param Bankgiro             $bankgiro             Payee bankgiro account number
105
     * @param \DateTimeInterface   $date                 Optional creation date
106
     * @param IntervalFormatter    $intervalFormatter    Optional interval formatter
107
     * @param RepititionsFormatter $repititionsFormatter Optional repititions formatter
108
     */
109
    public function __construct(
110
        string $bgcNr,
111
        Bankgiro $bankgiro,
112
        \DateTimeInterface $date = null,
113
        IntervalFormatter $intervalFormatter = null,
114
        RepititionsFormatter $repititionsFormatter = null
115
    ) {
116
        $this->bgcNr = $bgcNr;
117
        $this->payeeBgNode = (new PayeeBankgiroNode(0, $bankgiro->getNumber()))->setAttribute('account', $bankgiro);
118
        $this->date = $date ?: new \DateTimeImmutable;
119
        $this->intervalFormatter = $intervalFormatter ?: new IntervalFormatter;
120
        $this->repititionsFormatter = $repititionsFormatter ?: new RepititionsFormatter;
121
        $this->reset();
122
    }
123
124
    /**
125
     * Reset builder to initial state
126
     */
127
    public function reset()
128
    {
129
        $this->opening = new RequestOpeningRecordNode(
130
            0,
131
            (new DateNode(0, $this->date->format('Ymd')))->setAttribute('date', $this->date),
132
            new TextNode(0, 'AUTOGIRO'),
133
            new TextNode(0, str_pad('', 44)),
134
            new PayeeBgcNumberNode(0, $this->bgcNr),
135
            $this->payeeBgNode,
136
            [new TextNode(0, '  ')]
137
        );
138
        $this->mandateRecords = [];
139
        $this->transactionRecords = [];
140
        $this->amendmentRecords = [];
141
    }
142
143
    /**
144
     * Add a new mandate record to tree
145
     */
146
    public function addCreateMandateRecord(string $payerNr, AccountNumber $account, Id $id)
147
    {
148
        $this->mandateRecords[] = new CreateMandateRequestNode(
149
            0,
150
            $this->payeeBgNode,
151
            new PayerNumberNode(0, $payerNr),
152
            (new AccountNode(0, $account->getNumber()))->setAttribute('account', $account),
153
            (new IdNode(0, (string)$id))->setAttribute('id', $id),
154
            [new TextNode(0, str_pad('', 24))]
155
        );
156
    }
157
158
    /**
159
     * Add a delete mandate record to tree
160
     */
161 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...
162
    {
163
        $this->mandateRecords[] = new DeleteMandateRequestNode(
164
            0,
165
            $this->payeeBgNode,
166
            new PayerNumberNode(0, $payerNr),
167
            [new TextNode(0, str_pad('', 52))]
168
        );
169
    }
170
171
    /**
172
     * Add an accept digital mandate record to tree
173
     */
174 View Code Duplication
    public function addAcceptDigitalMandateRecord(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...
175
    {
176
        $this->mandateRecords[] = new AcceptDigitalMandateRequestNode(
177
            0,
178
            $this->payeeBgNode,
179
            new PayerNumberNode(0, $payerNr),
180
            [new TextNode(0, str_pad('', 52))]
181
        );
182
    }
183
184
    /**
185
     * Add a reject digital mandate record to tree
186
     */
187
    public function addRejectDigitalMandateRecord(string $payerNr)
188
    {
189
        $this->mandateRecords[] = new RejectDigitalMandateRequestNode(
190
            0,
191
            $this->payeeBgNode,
192
            new PayerNumberNode(0, $payerNr),
193
            new TextNode(0, str_pad('', 48)),
194
            new TextNode(0, 'AV'),
195
            [new TextNode(0, '  ')]
196
        );
197
    }
198
199
    /**
200
     * Add an update mandate record to tree
201
     */
202 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...
203
    {
204
        $this->mandateRecords[] = new UpdateMandateRequestNode(
205
            0,
206
            $this->payeeBgNode,
207
            new PayerNumberNode(0, $payerNr),
208
            $this->payeeBgNode,
209
            new PayerNumberNode(0, $newPayerNr),
210
            [new TextNode(0, str_pad('', 26))]
211
        );
212
    }
213
214
    /**
215
     * Add an incoming transaction record to tree
216
     */
217
    public function addIncomingTransactionRecord(
218
        string $payerNr,
219
        SEK $amount,
220
        \DateTimeInterface $date,
221
        string $ref,
222
        string $interval,
223
        int $repetitions
224
    ) {
225
        $this->addTransactionRecord(
226
            IncomingTransactionRequestNode::CLASS,
227
            $payerNr,
228
            $amount,
229
            $date,
230
            $ref,
231
            $interval,
232
            $repetitions
233
        );
234
    }
235
236
    /**
237
     * Add an outgoing transaction record to tree
238
     */
239
    public function addOutgoingTransactionRecord(
240
        string $payerNr,
241
        SEK $amount,
242
        \DateTimeInterface $date,
243
        string $ref,
244
        string $interval,
245
        int $repetitions
246
    ) {
247
        $this->addTransactionRecord(
248
            OutgoingTransactionRequestNode::CLASS,
249
            $payerNr,
250
            $amount,
251
            $date,
252
            $ref,
253
            $interval,
254
            $repetitions
255
        );
256
    }
257
258
    /**
259
     * Add an incoming transaction at next possible bank date record to tree
260
     */
261
    public function addImmediateIncomingTransactionRecord(string $payerNr, SEK $amount, string $ref = '')
262
    {
263
        $this->addImmediateTransactionRecord(IncomingTransactionRequestNode::CLASS, $payerNr, $amount, $ref);
264
    }
265
266
    /**
267
     * Add an outgoing transaction at next possible bank date record to tree
268
     */
269
    public function addImmediateOutgoingTransactionRecord(string $payerNr, SEK $amount, string $ref = '')
270
    {
271
        $this->addImmediateTransactionRecord(OutgoingTransactionRequestNode::CLASS, $payerNr, $amount, $ref);
272
    }
273
274
    /**
275
     * Get the created request tree
276
     */
277
    public function buildTree(): FileNode
278
    {
279
        $layouts = [];
280
281
        foreach (['mandateRecords', 'transactionRecords', 'amendmentRecords'] as $records) {
282
            if (!empty($this->$records)) {
283
                $layouts[] = new LayoutNode($this->opening, ...$this->$records);
284
            }
285
        }
286
287
        return new FileNode(...$layouts);
288
    }
289
290
    private function addTransactionRecord(
291
        string $classname,
292
        string $payerNr,
293
        SEK $amount,
294
        \DateTimeInterface $date,
295
        string $ref,
296
        string $interval,
297
        int $repetitions
298
    ) {
299
        $this->transactionRecords[] = new $classname(
300
            0,
301
            (new DateNode(0, $date->format('Ymd')))->setAttribute('date', $date),
302
            new IntervalNode(0, $this->intervalFormatter->format($interval)),
303
            new RepetitionsNode(0, $this->repititionsFormatter->format($repetitions)),
304
            new TextNode(0, ' '),
305
            new PayerNumberNode(0, $payerNr),
306
            (new AmountNode(0, $amount->getSignalString()))->setAttribute('amount', $amount),
307
            $this->payeeBgNode,
308
            new TextNode(0, str_pad($ref, 16, ' ', STR_PAD_LEFT), '/^.{16}$/'),
309
            [new TextNode(0, str_pad('', 11))]
310
        );
311
    }
312
313
    private function addImmediateTransactionRecord(string $classname, string $payerNr, SEK $amount, string $ref = '')
314
    {
315
        $this->transactionRecords[] = new $classname(
316
            0,
317
            new ImmediateDateNode,
318
            new IntervalNode(0, '0'),
319
            new RepetitionsNode(0, '   '),
320
            new TextNode(0, ' '),
321
            new PayerNumberNode(0, $payerNr),
322
            (new AmountNode(0, $amount->getSignalString()))->setAttribute('amount', $amount),
323
            $this->payeeBgNode,
324
            new TextNode(0, str_pad($ref, 16, ' ', STR_PAD_LEFT), '/^.{16}$/'),
325
            [new TextNode(0, str_pad('', 11))]
326
        );
327
    }
328
}
329