Completed
Push — master ( 2c0bf4...172fae )
by Hannes
02:27
created

TreeBuilder::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
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\DeleteMandateRequestNode;
28
use byrokrat\autogiro\Tree\Record\Request\CreateMandateRequestNode;
29
use byrokrat\autogiro\Tree\FileNode;
30
use byrokrat\autogiro\Tree\LayoutNode;
31
use byrokrat\autogiro\Tree\DateNode;
32
use byrokrat\autogiro\Tree\TextNode;
33
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
34
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
35
use byrokrat\autogiro\Tree\PayerNumberNode;
36
use byrokrat\autogiro\Tree\AccountNode;
37
use byrokrat\autogiro\Tree\IdNode;
38
use byrokrat\banking\AccountNumber;
39
use byrokrat\banking\Bankgiro;
40
use byrokrat\id\Id;
41
42
/**
43
 * Build trees representing autogiro request files
44
 */
45
class TreeBuilder
46
{
47
    /**
48
     * @var RequestOpeningRecordNode Opening record used for each layout
49
     */
50
    private $opening;
51
52
    /**
53
     * @var RecordNode[] List of created mandate records
54
     */
55
    private $mandateRecords;
56
57
    /**
58
     * @var RecordNode[] List of created transaction records
59
     */
60
    private $transactionRecords;
61
62
    /**
63
     * @var RecordNode[] List of created amendment records
64
     */
65
    private $amendmentRecords;
66
67
    /**
68
     * @var string Payee BGC customer number
69
     */
70
    private $bgcNr;
71
72
    /**
73
     * @var PayeeBankgiroNode Wrapper around payee bankgiro account number
74
     */
75
    private $payeeBgNode;
76
77
    /**
78
     * @var \DateTimeInterface Date of file creation
79
     */
80
    private $date;
81
82
    /**
83
     * @param string             $bgcNr    The BGC customer number of payee
84
     * @param Bankgiro           $bankgiro Payee bankgiro account number
85
     * @param \DateTimeInterface $date     Optional creation date
86
     */
87
    public function __construct(string $bgcNr, Bankgiro $bankgiro, \DateTimeInterface $date = null)
88
    {
89
        $this->bgcNr = $bgcNr;
90
        $this->payeeBgNode = (new PayeeBankgiroNode(0, $bankgiro->getNumber()))->setAttribute('account', $bankgiro);
91
        $this->date = $date ?: new \DateTimeImmutable;
92
        $this->reset();
93
    }
94
95
    /**
96
     * Reset builder to initial state
97
     */
98
    public function reset()
99
    {
100
        $this->opening = new RequestOpeningRecordNode(
101
            0,
102
            (new DateNode(0, $this->date->format('Ymd')))->setAttribute('date', $this->date),
103
            new TextNode(0, 'AUTOGIRO'),
104
            new TextNode(0, str_pad('', 44)),
105
            new PayeeBgcNumberNode(0, $this->bgcNr),
106
            $this->payeeBgNode,
107
            [new TextNode(0, '  ')]
108
        );
109
        $this->mandateRecords = [];
110
        $this->transactionRecords = [];
111
        $this->amendmentRecords = [];
112
    }
113
114
    /**
115
     * Add a new mandate record to tree
116
     */
117
    public function addCreateMandateRecord(string $payerNr, AccountNumber $account, Id $id)
118
    {
119
        $this->mandateRecords[] = new CreateMandateRequestNode(
120
            0,
121
            $this->payeeBgNode,
122
            new PayerNumberNode(0, $payerNr),
123
            (new AccountNode(0, $account->getNumber()))->setAttribute('account', $account),
124
            (new IdNode(0, (string)$id))->setAttribute('id', $id),
125
            [new TextNode(0, str_pad('', 24))]
126
        );
127
    }
128
129
    /**
130
     * Add a delete mandate record to tree
131
     */
132
    public function addDeleteMandateRecord(string $payerNr)
133
    {
134
        $this->mandateRecords[] = new DeleteMandateRequestNode(
135
            0,
136
            $this->payeeBgNode,
137
            new PayerNumberNode(0, $payerNr),
138
            [new TextNode(0, str_pad('', 52))]
139
        );
140
    }
141
142
    /**
143
     * Get the created request tree
144
     */
145
    public function buildTree(): FileNode
146
    {
147
        $layouts = [];
148
149
        foreach (['mandateRecords', 'transactionRecords', 'amendmentRecords'] as $records) {
150
            if (!empty($this->$records)) {
151
                $layouts[] = new LayoutNode($this->opening, ...$this->$records);
152
            }
153
        }
154
155
        return new FileNode(...$layouts);
156
    }
157
}
158