Completed
Push — master ( 172fae...63b231 )
by Hannes
01:34
created

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