Completed
Push — master ( 7e81c7...bd5f3a )
by Hannes
01:39
created

Writer::addPayment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 6
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-18 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\IdInterface;
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(): void
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 IdInterface   $id      Payer id number
86
     */
87
    public function addNewMandate(string $payerNr, AccountNumber $account, IdInterface $id): void
88
    {
89
        $this->treeBuilder->addCreateMandateRequest($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): void
98
    {
99
        $this->treeBuilder->addDeleteMandateRequest($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): void
108
    {
109
        $this->treeBuilder->addAcceptDigitalMandateRequest($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): void
118
    {
119
        $this->treeBuilder->addRejectDigitalMandateRequest($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): void
129
    {
130
        $this->treeBuilder->addUpdateMandateRequest($payerNr, $newPayerNr);
131
    }
132
133
    /**
134
     * Add an incoming payment request to the build queue
135
     *
136
     * @param string             $payerNr     Number identifying the payer
137
     * @param SEK                $amount      The requested payment amount
138
     * @param \DateTimeInterface $date        Requested date of payment (or first date for repeated payments)
139
     * @param string             $ref         Custom payment reference number
140
     * @param string             $interval    Interval for repeted payment, use one of the Intervals constants
141
     * @param integer            $repetitions Number of repititions (0 repeates payments indefinitely)
142
     */
143 View Code Duplication
    public function addPayment(
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...
144
        string $payerNr,
145
        SEK $amount,
146
        \DateTimeInterface $date,
147
        string $ref = '',
148
        string $interval = Intervals::INTERVAL_ONCE,
149
        int $repetitions = 0
150
    ): void {
151
        $this->treeBuilder->addIncomingPaymentRequest($payerNr, $amount, $date, $ref, $interval, $repetitions);
152
    }
153
154
    /**
155
     * Add an incoming payment request to the build queue
156
     *
157
     * @param string             $payerNr     Number identifying the payer
158
     * @param SEK                $amount      The requested payment amount
159
     * @param \DateTimeInterface $date        Requested  first date of payment
160
     * @param string             $ref         Custom payment reference number
161
     */
162
    public function addMonthlyPayment(
163
        string $payerNr,
164
        SEK $amount,
165
        \DateTimeInterface $date,
166
        string $ref = ''
167
    ): void {
168
        $this->addPayment($payerNr, $amount, $date, $ref, Intervals::INTERVAL_MONTHLY_ON_DATE, 0);
169
    }
170
171
    /**
172
     * Add an incoming payment at next possible bank date request to the build queue
173
     *
174
     * @param string $payerNr Number identifying the payer
175
     * @param SEK    $amount  The requested payment amount
176
     * @param string $ref     Custom payment reference number
177
     */
178
    public function addImmediatePayment(string $payerNr, SEK $amount, string $ref = ''): void
179
    {
180
        $this->treeBuilder->addImmediateIncomingPaymentRequest($payerNr, $amount, $ref);
181
    }
182
183
    /**
184
     * Add an outgoing payment request to the build queue
185
     *
186
     * @param string             $payerNr     Number identifying the payer
187
     * @param SEK                $amount      The requested payment amount
188
     * @param \DateTimeInterface $date        Requested date of payment (or first date for repeated payments)
189
     * @param string             $ref         Custom payment reference number
190
     * @param string             $interval    Interval for repeted payment, use one of the Intervals constants
191
     * @param integer            $repetitions Number of repititions (0 repeateds payments indefinitely)
192
     */
193 View Code Duplication
    public function addOutgoingPayment(
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...
194
        string $payerNr,
195
        SEK $amount,
196
        \DateTimeInterface $date,
197
        string $ref = '',
198
        string $interval = Intervals::INTERVAL_ONCE,
199
        int $repetitions = 1
200
    ): void {
201
        $this->treeBuilder->addOutgoingPaymentRequest($payerNr, $amount, $date, $ref, $interval, $repetitions);
202
    }
203
204
    /**
205
     * Add an outgoing payment on next possible bank date request to the build queue
206
     *
207
     * @param string $payerNr Number identifying the payer
208
     * @param SEK    $amount  The requested payment amount
209
     * @param string $ref     Custom payment reference number
210
     */
211
    public function addImmediateOutgoingPayment(string $payerNr, SEK $amount, string $ref = ''): void
212
    {
213
        $this->treeBuilder->addImmediateOutgoingPaymentRequest($payerNr, $amount, $ref);
214
    }
215
}
216