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

Writer::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Intervals;
26
use byrokrat\autogiro\Visitor\Visitor;
27
use byrokrat\banking\AccountNumber;
28
use byrokrat\id\Id;
29
use byrokrat\amount\Currency\SEK;
30
31
/**
32
 * Facade for creating autogiro request files
33
 */
34
class Writer
35
{
36
    /**
37
     * @var TreeBuilder Helper used when building trees
38
     */
39
    private $treeBuilder;
40
41
    /**
42
     * @var PrintingVisitor Helper used when generating content
43
     */
44
    private $printer;
45
46
    /**
47
     * @var Visitor Helper used to validate and process tree
48
     */
49
    private $visitor;
50
51
    public function __construct(TreeBuilder $treeBuilder, PrintingVisitor $printer, Visitor $visitor)
52
    {
53
        $this->treeBuilder = $treeBuilder;
54
        $this->printer = $printer;
55
        $this->visitor = $visitor;
56
    }
57
58
    /**
59
     * Build and return request content
60
     */
61
    public function getContent(): string
62
    {
63
        $tree = $this->treeBuilder->buildTree();
64
        $tree->accept($this->visitor);
65
        $output = new Output;
66
        $this->printer->setOutput($output);
67
        $tree->accept($this->printer);
68
69
        return rtrim($output->getContent(), "\r\n");
70
    }
71
72
    /**
73
     * Reset internal build queue
74
     */
75
    public function reset()
76
    {
77
        $this->treeBuilder->reset();
78
    }
79
80
    /**
81
     * Add a new mandate request to the build queue
82
     *
83
     * @param string        $payerNr Number identifying the payer
84
     * @param AccountNumber $account Payer account number
85
     * @param Id            $id      Payer id number
86
     */
87
    public function addNewMandate(string $payerNr, AccountNumber $account, Id $id)
88
    {
89
        $this->treeBuilder->addCreateMandateRecord($payerNr, $account, $id);
90
    }
91
92
    /**
93
     * Add a delete mandate request to the build queue
94
     *
95
     * @param string $payerNr Number identifying the payer
96
     */
97
    public function deleteMandate(string $payerNr)
98
    {
99
        $this->treeBuilder->addDeleteMandateRecord($payerNr);
100
    }
101
102
    /**
103
     * Add an accept digital mandate request to the build queue
104
     *
105
     * @param string $payerNr Number identifying the payer
106
     */
107
    public function acceptDigitalMandate(string $payerNr)
108
    {
109
        $this->treeBuilder->addAcceptDigitalMandateRecord($payerNr);
110
    }
111
112
    /**
113
     * Add a reject digital mandate request to the build queue
114
     *
115
     * @param string $payerNr Number identifying the payer
116
     */
117
    public function rejectDigitalMandate(string $payerNr)
118
    {
119
        $this->treeBuilder->addRejectDigitalMandateRecord($payerNr);
120
    }
121
122
    /**
123
     * Add an update mandate request to the build queue
124
     *
125
     * @param string $payerNr    Old number identifying the payer
126
     * @param string $newPayerNr New number identifying the payer
127
     */
128
    public function updateMandate(string $payerNr, string $newPayerNr)
129
    {
130
        $this->treeBuilder->addUpdateMandateRecord($payerNr, $newPayerNr);
131
    }
132
133
    /**
134
     * Add an incoming transaction request to the build queue
135
     *
136
     * @param string             $payerNr     Number identifying the payer
137
     * @param SEK                $amount      The requested transaction amount
138
     * @param \DateTimeInterface $date        Requested date of transaction (or first date for repeated transactions)
139
     * @param string             $ref         Custom transaction reference number
140
     * @param string             $interval    Interval for repeted transaction, use one of the Intervals constants
141
     * @param integer            $repetitions Number of repititions (0 repeates transactions indefinitely)
142
     */
143
    public function addTransaction(
144
        string $payerNr,
145
        SEK $amount,
146
        \DateTimeInterface $date,
147
        string $ref = '',
148
        string $interval = Intervals::INTERVAL_ONCE,
149
        int $repetitions = 0
150
    ) {
151
        $this->treeBuilder->addIncomingTransactionRecord($payerNr, $amount, $date, $ref, $interval, $repetitions);
152
    }
153
154
    /**
155
     * Add an incoming transaction request to the build queue
156
     *
157
     * @param string             $payerNr     Number identifying the payer
158
     * @param SEK                $amount      The requested transaction amount
159
     * @param \DateTimeInterface $date        Requested  first date of transaction
160
     * @param string             $ref         Custom transaction reference number
161
     */
162
    public function addMonthlyTransaction(string $payerNr, SEK $amount, \DateTimeInterface $date, string $ref = '')
163
    {
164
        $this->addTransaction($payerNr, $amount, $date, $ref, Intervals::INTERVAL_MONTHLY_ON_DATE, 0);
165
    }
166
167
    /**
168
     * Add an incoming transaction at next possible bank date request to the build queue
169
     *
170
     * @param string $payerNr Number identifying the payer
171
     * @param SEK    $amount  The requested transaction amount
172
     * @param string $ref     Custom transaction reference number
173
     */
174
    public function addImmediateTransaction(string $payerNr, SEK $amount, string $ref = '')
175
    {
176
        $this->treeBuilder->addImmediateIncomingTransactionRecord($payerNr, $amount, $ref);
177
    }
178
179
    /**
180
     * Add an outgoing transaction request to the build queue
181
     *
182
     * @param string             $payerNr     Number identifying the payer
183
     * @param SEK                $amount      The requested transaction amount
184
     * @param \DateTimeInterface $date        Requested date of transaction (or first date for repeated transactions)
185
     * @param string             $ref         Custom transaction reference number
186
     * @param string             $interval    Interval for repeted transaction, use one of the Intervals constants
187
     * @param integer            $repetitions Number of repititions (0 repeateds transactions indefinitely)
188
     */
189
    public function addOutgoingTransaction(
190
        string $payerNr,
191
        SEK $amount,
192
        \DateTimeInterface $date,
193
        string $ref = '',
194
        string $interval = Intervals::INTERVAL_ONCE,
195
        int $repetitions = 1
196
    ) {
197
        $this->treeBuilder->addOutgoingTransactionRecord($payerNr, $amount, $date, $ref, $interval, $repetitions);
198
    }
199
200
    /**
201
     * Add an outgoing transaction on next possible bank date request to the build queue
202
     *
203
     * @param string $payerNr Number identifying the payer
204
     * @param SEK    $amount  The requested transaction amount
205
     * @param string $ref     Custom transaction reference number
206
     */
207
    public function addImmediateOutgoingTransaction(string $payerNr, SEK $amount, string $ref = '')
208
    {
209
        $this->treeBuilder->addImmediateOutgoingTransactionRecord($payerNr, $amount, $ref);
210
    }
211
}
212