Completed
Push — master ( 98aac0...15b539 )
by Hannes
02:24
created

TreeBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A reset() 0 14 1
A addDeleteMandateRecord() 0 8 1
A buildTree() 0 12 3
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro\Writer.
4
 *
5
 * byrokrat\autogiro\Writer 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\Writer 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\Writer. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 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\FileNode;
29
use byrokrat\autogiro\Tree\LayoutNode;
30
use byrokrat\autogiro\Tree\Date\DateNode;
31
use byrokrat\autogiro\Tree\TextNode;
32
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
33
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
34
use byrokrat\autogiro\Tree\PayerNumberNode;
35
36
/**
37
 * Build trees representing autogiro request files
38
 */
39
class TreeBuilder
40
{
41
    /**
42
     * @var RequestOpeningRecordNode Opening record used for each layout
43
     */
44
    private $opening;
45
46
    /**
47
     * @var RecordNode[] List of created mandate records
48
     */
49
    private $mandateRecords;
50
51
    /**
52
     * @var RecordNode[] List of created transaction records
53
     */
54
    private $transactionRecords;
55
56
    /**
57
     * @var RecordNode[] List of created amendment records
58
     */
59
    private $amendmentRecords;
60
61
    /**
62
     * @var string Payee BGC customer number
63
     */
64
    private $payeeBgcNr;
65
66
    /**
67
     * @var string Payee bankgiro account number
68
     */
69
    private $payeeBankgiro;
70
71
    /**
72
     * @var string Date of file creation (yyyymmdd)
73
     */
74
    private $date;
75
76
    public function __construct(string $payeeBgcNr, string $payeeBankgiro, string $date = '')
77
    {
78
        $this->payeeBgcNr = $payeeBgcNr;
79
        $this->payeeBankgiro = $payeeBankgiro;
80
        $this->date = $date ?: date('Ymd');
81
        $this->reset();
82
    }
83
84
    /**
85
     * Reset builder to initial state
86
     */
87
    public function reset()
88
    {
89
        $this->opening = new RequestOpeningRecordNode(
90
            0,
91
            new DateNode(0, $this->date),
92
            new TextNode(0, 'AUTOGIRO'),
93
            new TextNode(0, str_pad('', 44)),
94
            new PayeeBgcNumberNode(0, $this->payeeBgcNr),
95
            new PayeeBankgiroNode(0, $this->payeeBankgiro)
96
        );
97
        $this->mandateRecords = [];
98
        $this->transactionRecords = [];
99
        $this->amendmentRecords = [];
100
    }
101
102
    /**
103
     * Add a delete mandate record to tree
104
     */
105
    public function addDeleteMandateRecord(string $payerNr)
106
    {
107
        $this->mandateRecords[] = new DeleteMandateRequestNode(
108
            0,
109
            new PayeeBankgiroNode(0, $this->payeeBankgiro),
110
            new PayerNumberNode(0, $payerNr)
111
        );
112
    }
113
114
    /**
115
     * Get the created request tree
116
     */
117
    public function buildTree(): FileNode
118
    {
119
        $layouts = [];
120
121
        foreach (['mandateRecords', 'transactionRecords', 'amendmentRecords'] as $records) {
122
            if (!empty($this->$records)) {
123
                $layouts[] = new LayoutNode($this->opening, ...$this->$records);
124
            }
125
        }
126
127
        return new FileNode(...$layouts);
128
    }
129
}
130