Completed
Push — master ( 96a89d...d895c6 )
by Hannes
03:11
created

PrintingVisitor::beforeRepetitions()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 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-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Writer;
24
25
use byrokrat\autogiro\Visitor\Visitor;
26
use byrokrat\autogiro\Exception\RuntimeException;
27
use byrokrat\autogiro\Tree\Node;
28
use byrokrat\amount\Currency\SEK;
29
use byrokrat\banking\AccountNumber;
30
use byrokrat\id\IdInterface;
31
use byrokrat\id\PersonalId;
32
use byrokrat\id\OrganizationId;
33
34
/**
35
 * Visitor that generates files to bgc from parse trees
36
 */
37
class PrintingVisitor extends Visitor
38
{
39
    const EOL = "\r\n";
40
41
    /**
42
     * @var Output
43
     */
44
    private $output;
45
46
    public function setOutput(Output $output): void
47
    {
48
        $this->output = $output;
49
    }
50
51
    private function write(string $text): void
52
    {
53
        if ($this->output) {
54
            $this->output->write($text);
55
        }
56
    }
57
58
    public function beforeDate(Node $node): void
59
    {
60
        if ($node->getValue() instanceof \DateTimeInterface) {
61
            $this->write($node->getValue()->format('Ymd'));
62
        }
63
    }
64
65
    public function beforeImmediateDate(): void
66
    {
67
        $this->write('GENAST  ');
68
    }
69
70
    public function beforeText(Node $node): void
71
    {
72
        $this->write($node->getValue());
73
    }
74
75
    public function beforePayerNumber(Node $node): void
76
    {
77
        $this->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT));
78
    }
79
80
    public function beforePayeeBgcNumber(Node $node): void
81
    {
82
        $this->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT));
83
    }
84
85 View Code Duplication
    public function beforePayeeBankgiro(Node $node): void
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...
86
    {
87
        if ($node->getValue() instanceof AccountNumber) {
88
            $account = $node->getValue();
89
            $this->write(
90
                str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT)
91
            );
92
        }
93
    }
94
95 View Code Duplication
    public function beforeAccount(Node $node): void
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...
96
    {
97
        if ($node->getValue() instanceof AccountNumber) {
98
            $account = $node->getValue();
99
            $this->write(
100
                $account->getClearingNumber()
101
                . str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT)
102
            );
103
        }
104
    }
105
106
    public function beforeInterval(Node $node): void
107
    {
108
        if (!in_array((int)$node->getValue(), range('0', '8'))) {
109
            throw new RuntimeException('Interval must be between 0 and 8');
110
        }
111
112
        $this->write($node->getValue());
113
    }
114
115
    public function beforeRepetitions(Node $node): void
116
    {
117
        if (!ctype_digit($node->getValue()) || strlen($node->getValue()) > 3) {
118
            throw new RuntimeException("Invalid number of repitions: {$node->getValue()}");
119
        }
120
121
        $this->write(
122
            $node->getValue() ? str_pad($node->getValue(), 3, '0', STR_PAD_LEFT) : '   '
123
        );
124
    }
125
126
    public function beforeAmount(Node $node): void
127
    {
128
        if ($node->getValue() instanceof SEK) {
129
            $amount = $node->getValue();
130
131
            if ($amount->isGreaterThan(new SEK('9999999999.99')) || $amount->isLessThan(new SEK('-9999999999.99'))) {
132
                throw new RuntimeException('Amount must be between 9999999999.99 and -9999999999.99');
133
            }
134
135
            $this->write(
136
                str_pad($amount->getSignalString(), 12, '0', STR_PAD_LEFT)
137
            );
138
        }
139
    }
140
141
    public function beforeStateId(Node $node): void
142
    {
143
        if ($node->getValue() instanceof PersonalId) {
144
            $this->write($node->getValue()->format('Ymdsk'));
145
        }
146
        if ($node->getValue() instanceof OrganizationId) {
147
            $this->write($node->getValue()->format('00Ssk'));
148
        }
149
    }
150
151
    public function beforeOpening(): void
152
    {
153
        $this->write('01');
154
    }
155
156
    public function beforeCreateMandateRequest(): void
157
    {
158
        $this->write('04');
159
    }
160
161
    public function beforeDeleteMandateRequest(): void
162
    {
163
        $this->write('03');
164
    }
165
166
    public function beforeAcceptDigitalMandateRequest(): void
167
    {
168
        $this->write('04');
169
    }
170
171
    public function beforeRejectDigitalMandateRequest(): void
172
    {
173
        $this->write('04');
174
    }
175
176
    public function beforeUpdateMandateRequest(): void
177
    {
178
        $this->write('05');
179
    }
180
181
    public function beforeIncomingPaymentRequest(): void
182
    {
183
        $this->write('82');
184
    }
185
186
    public function beforeOutgoingPaymentRequest(): void
187
    {
188
        $this->write('32');
189
    }
190
191
    public function beforeAmendmentRequest(): void
192
    {
193
        $this->write('23');
194
    }
195
196
    public function afterRecord(): void
197
    {
198
        $this->write(self::EOL);
199
    }
200
}
201