Completed
Push — master ( 85efbc...98aac0 )
by Hannes
02:14
created

PrintingVisitor::beforeIdNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 4
nop 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 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Writer;
24
25
use byrokrat\autogiro\Visitor;
26
use byrokrat\autogiro\Exception\LogicException;
27
use byrokrat\autogiro\Tree\Node;
28
use byrokrat\autogiro\Tree\Date\DateNode;
29
use byrokrat\autogiro\Tree\Date\ImmediateDateNode;
30
use byrokrat\autogiro\Tree\TextNode;
31
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
32
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
33
use byrokrat\autogiro\Tree\PayerNumberNode;
34
use byrokrat\autogiro\Tree\AccountNode;
35
use byrokrat\autogiro\Tree\AmountNode;
36
use byrokrat\autogiro\Tree\IntervalNode;
37
use byrokrat\autogiro\Tree\IdNode;
38
use byrokrat\autogiro\Tree\RepetitionsNode;
39
use byrokrat\amount\Currency\SEK;
40
use byrokrat\banking\AccountNumber;
41
use byrokrat\id\Id;
42
use byrokrat\id\PersonalId;
43
use byrokrat\id\OrganizationId;
44
45
/**
46
 * Visitor that generates files to bgc from parse trees
47
 */
48
class PrintingVisitor extends Visitor
49
{
50
    /**
51
     * @var OutputInterface
52
     */
53
    private $output;
54
55
    public function __construct(OutputInterface $output)
56
    {
57
        $this->output = $output;
58
    }
59
60
    public function beforeDateNode(DateNode $node)
61
    {
62
        $this->assertAttribute($node, 'date', \DateTime::CLASS);
63
        $this->output->write($node->getAttribute('date')->format('Ymd'));
64
    }
65
66
    public function beforeImmediateDateNode(ImmediateDateNode $node)
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        $this->output->write('GENAST  ');
69
    }
70
71
    public function beforeTextNode(TextNode $node)
72
    {
73
        $this->output->write($node->getValue());
74
    }
75
76
    public function beforePayeeBgcNumberNode(PayeeBgcNumberNode $node)
77
    {
78
        $this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT));
79
    }
80
81
    public function beforePayeeBankgiroNode(PayeeBankgiroNode $node)
82
    {
83
        $this->assertAttribute($node, 'account', AccountNumber::CLASS);
84
        $this->output->write(str_pad($node->getAttribute('account')->getSerialNumber(), 10, '0', STR_PAD_LEFT));
85
    }
86
87
    public function beforePayerNumberNode(PayerNumberNode $node)
88
    {
89
        $this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT));
90
    }
91
92
    public function beforeAccountNode(AccountNode $node)
93
    {
94
        $this->assertAttribute($node, 'account', AccountNumber::CLASS);
95
        $this->output->write(
96
            $node->getAttribute('account')->getClearingNumber()
97
            . str_pad($node->getAttribute('account')->getSerialNumber(), 12, '0', STR_PAD_LEFT)
98
        );
99
    }
100
101
    private function assertAttribute(Node $node, string $attr, string $classname)
102
    {
103
        if (!$node->hasAttribute($attr) || !$node->getAttribute($attr) instanceof $classname) {
104
            throw new LogicException("Missing attribute '$attr' in {$node->getType()}");
105
        }
106
    }
107
108
    public function beforeIntervalNode(IntervalNode $node)
109
    {
110
        $this->output->write($node->getValue());
111
    }
112
113
    public function beforeRepetitionsNode(RepetitionsNode $node)
114
    {
115
        $this->output->write($node->getValue());
116
    }
117
118
    public function beforeAmountNode(AmountNode $node)
119
    {
120
        $this->assertAttribute($node, 'amount', SEK::CLASS);
121
        $this->output->write(
122
            str_pad($node->getAttribute('amount')->getSignalString(), 12, '0', STR_PAD_LEFT)
123
        );
124
    }
125
126
    public function beforeIdNode(IdNode $node)
127
    {
128
        $this->assertAttribute($node, 'id', Id::CLASS);
129
        if ($node->getAttribute('id') instanceof PersonalId) {
130
            $this->output->write($node->getAttribute('id')->format('Ymdsk'));
131
        }
132
        if ($node->getAttribute('id') instanceof OrganizationId) {
133
            $this->output->write($node->getAttribute('id')->format('00Ssk'));
134
        }
135
    }
136
}
137