Completed
Push — master ( 63b231...bdcfdd )
by Hannes
01:31
created

Writer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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\Visitor\Visitor;
26
use byrokrat\banking\AccountNumber;
27
use byrokrat\id\Id;
28
29
/**
30
 * Facade for creating autogiro request files
31
 */
32
class Writer
33
{
34
    /**
35
     * @var TreeBuilder Helper used when building trees
36
     */
37
    private $treeBuilder;
38
39
    /**
40
     * @var PrintingVisitor Helper used when generating content
41
     */
42
    private $printer;
43
44
    /**
45
     * @var Visitor Helper used to validate and process tree
46
     */
47
    private $visitor;
48
49
    public function __construct(TreeBuilder $treeBuilder, PrintingVisitor $printer, Visitor $visitor)
50
    {
51
        $this->treeBuilder = $treeBuilder;
52
        $this->printer = $printer;
53
        $this->visitor = $visitor;
54
    }
55
56
    /**
57
     * Build and return request content
58
     */
59
    public function getContent(): string
60
    {
61
        $tree = $this->treeBuilder->buildTree();
62
        $tree->accept($this->visitor);
63
        $output = new Output;
64
        $this->printer->setOutput($output);
65
        $tree->accept($this->printer);
66
67
        return rtrim($output->getContent(), "\r\n");
68
    }
69
70
    /**
71
     * Reset internal build queue
72
     */
73
    public function reset()
74
    {
75
        $this->treeBuilder->reset();
76
    }
77
78
    /**
79
     * Add a new mandate request to the build queue
80
     */
81
    public function addNewMandate(string $payerNr, AccountNumber $account, Id $id)
82
    {
83
        $this->treeBuilder->addCreateMandateRecord($payerNr, $account, $id);
84
    }
85
86
    /**
87
     * Add a delete mandate request to the build queue
88
     */
89
    public function deleteMandate(string $payerNr)
90
    {
91
        $this->treeBuilder->addDeleteMandateRecord($payerNr);
92
    }
93
94
    /**
95
     * Add an accept digital mandate request to the build queue
96
     */
97
    public function acceptMandate(string $payerNr)
98
    {
99
        $this->treeBuilder->addAcceptMandateRecord($payerNr);
100
    }
101
102
    /**
103
     * Add a reject digital mandate request to the build queue
104
     */
105
    public function rejectMandate(string $payerNr)
106
    {
107
        $this->treeBuilder->addRejectMandateRecord($payerNr);
108
    }
109
110
    /**
111
     * Add an update mandate request to the build queue
112
     */
113
    public function updateMandate(string $payerNr, string $newPayerNr)
114
    {
115
        $this->treeBuilder->addUpdateMandateRecord($payerNr, $newPayerNr);
116
    }
117
}
118