Completed
Push — master ( 2917fa...796bfc )
by Hannes
02:44
created

PrintingVisitor::assertAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A PrintingVisitor::beforeOpening() 0 4 1
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\Exception\LogicException;
28
use byrokrat\autogiro\Tree\Node;
29
use byrokrat\autogiro\Tree\Text;
30
use byrokrat\amount\Currency\SEK;
31
use byrokrat\banking\AccountNumber;
32
use byrokrat\id\IdInterface;
33
use byrokrat\id\PersonalId;
34
use byrokrat\id\OrganizationId;
35
36
/**
37
 * Visitor that generates files to bgc from parse trees
38
 */
39
class PrintingVisitor extends Visitor
40
{
41
    /**
42
     * End-of-line chars used when generating files
43
     */
44
    const EOL = "\r\n";
45
46
    /**
47
     * @var Output
48
     */
49
    private $output;
50
51
    public function setOutput(Output $output): void
52
    {
53
        $this->output = $output;
54
    }
55
56
    public function beforeDate(Node $node): void
57
    {
58
        if ($node->getValue() instanceof \DateTimeInterface) {
59
            $this->output->write($node->getValue()->format('Ymd'));
60
        }
61
    }
62
63
    public function beforeImmediateDate(): void
64
    {
65
        $this->output->write('GENAST  ');
66
    }
67
68
    public function beforeText(Text $node): void
69
    {
70
        $this->output->write($node->getValue());
71
    }
72
73
    public function beforePayerNumber(Node $node): void
74
    {
75
        $this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT));
76
    }
77
78
    public function beforePayeeBgcNumber(Node $node): void
79
    {
80
        $this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT));
81
    }
82
83 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...
84
    {
85
        if ($node->getValue() instanceof AccountNumber) {
86
            $account = $node->getValue();
87
            $this->output->write(
88
                str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT)
89
            );
90
        }
91
    }
92
93 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...
94
    {
95
        if ($node->getValue() instanceof AccountNumber) {
96
            $account = $node->getValue();
97
            $this->output->write(
98
                $account->getClearingNumber()
99
                . str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT)
100
            );
101
        }
102
    }
103
104
    public function beforeInterval(Node $node): void
105
    {
106
        if (!in_array((int)$node->getValue(), range('0', '8'))) {
107
            throw new RuntimeException('Interval must be between 0 and 8');
108
        }
109
        $this->output->write($node->getValue());
110
    }
111
112
    public function beforeAmount(Node $node): void
113
    {
114
        if ($node->getValue() instanceof SEK) {
115
            $amount = $node->getValue();
116
117
            if ($amount->isGreaterThan(new SEK('9999999999.99')) || $amount->isLessThan(new SEK('-9999999999.99'))) {
118
                throw new RuntimeException('Amount must be between 9999999999.99 and -9999999999.99');
119
            }
120
121
            $this->output->write(
122
                str_pad($amount->getSignalString(), 12, '0', STR_PAD_LEFT)
123
            );
124
        }
125
    }
126
127
    public function beforeStateId(Node $node): void
128
    {
129
        if ($node->getValue() instanceof PersonalId) {
130
            $this->output->write($node->getValue()->format('Ymdsk'));
131
        }
132
        if ($node->getValue() instanceof OrganizationId) {
133
            $this->output->write($node->getValue()->format('00Ssk'));
134
        }
135
    }
136
137
    public function beforeOpening(): void
138
    {
139
        $this->output->write('01');
140
    }
141
142
    public function afterOpening(): void
143
    {
144
        $this->output->write(self::EOL);
145
    }
146
147
    public function beforeCreateMandateRequest(): void
148
    {
149
        $this->output->write('04');
150
    }
151
152
    public function afterCreateMandateRequest(): void
153
    {
154
        $this->output->write(self::EOL);
155
    }
156
157
    public function beforeDeleteMandateRequest(): void
158
    {
159
        $this->output->write('03');
160
    }
161
162
    public function afterDeleteMandateRequest(): void
163
    {
164
        $this->output->write(self::EOL);
165
    }
166
167
    public function beforeAcceptDigitalMandateRequest(): void
168
    {
169
        $this->output->write('04');
170
    }
171
172
    public function afterAcceptDigitalMandateRequest(): void
173
    {
174
        $this->output->write(self::EOL);
175
    }
176
177
    public function beforeRejectDigitalMandateRequest(): void
178
    {
179
        $this->output->write('04');
180
    }
181
182
    public function afterRejectDigitalMandateRequest(): void
183
    {
184
        $this->output->write(self::EOL);
185
    }
186
187
    public function beforeUpdateMandateRequest(): void
188
    {
189
        $this->output->write('05');
190
    }
191
192
    public function afterUpdateMandateRequest(): void
193
    {
194
        $this->output->write(self::EOL);
195
    }
196
197
    public function beforeIncomingPaymentRequest(): void
198
    {
199
        $this->output->write('82');
200
    }
201
202
    public function afterIncomingPaymentRequest(): void
203
    {
204
        $this->output->write(self::EOL);
205
    }
206
207
    public function beforeOutgoingPaymentRequest(): void
208
    {
209
        $this->output->write('32');
210
    }
211
212
    public function afterOutgoingPaymentRequest(): void
213
    {
214
        $this->output->write(self::EOL);
215
    }
216
}
217