Completed
Push — master ( 15b539...926e74 )
by Hannes
10:57
created

VisitorFactory::createVisitors()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 27
nc 8
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\Visitor;
24
25
use byrokrat\id\CoordinationIdFactory;
26
use byrokrat\id\NullIdFactory;
27
use byrokrat\id\OrganizationIdFactory;
28
use byrokrat\id\PersonalIdFactory;
29
use byrokrat\banking\AccountFactory;
30
use byrokrat\banking\BankNames;
31
32
/**
33
 * Creates the set of standard visitors
34
 */
35
class VisitorFactory
36
{
37
    /**
38
     * Do not include account number visitor
39
     */
40
    const VISITOR_IGNORE_ACCOUNTS = 1;
41
42
    /**
43
     * Do not include amount visitor
44
     */
45
    const VISITOR_IGNORE_AMOUNTS = 2;
46
47
    /**
48
     * Do not include id visitor
49
     */
50
    const VISITOR_IGNORE_IDS = 4;
51
52
    /**
53
     * Ignore all visitors based on external dependencies
54
     */
55
    const VISITOR_IGNORE_EXTERNAL = self::VISITOR_IGNORE_ACCOUNTS | self::VISITOR_IGNORE_AMOUNTS | self::VISITOR_IGNORE_IDS;
56
57
    /**
58
     * Create the standard set of visitors used when processing a parse tree
59
     */
60
    public function createVisitors(int $flags = 0): Visitor
61
    {
62
        $flag = function (int $needle) use ($flags) {
63
            return ($needle & $flags) == $needle;
64
        };
65
66
        $errorObj = new ErrorObject;
67
68
        $visitors = new ContainingVisitor(
69
            $errorObj,
70
            new DateVisitor($errorObj),
71
            new LayoutVisitor($errorObj),
72
            new MessageVisitor($errorObj),
73
            new PayeeVisitor($errorObj),
74
            new TextVisitor($errorObj),
75
            new TransactionVisitor($errorObj)
76
        );
77
78
        if (!$flag(self::VISITOR_IGNORE_ACCOUNTS)) {
79
            $accountFactory = new AccountFactory;
80
            $accountFactory->blacklistFormats([BankNames::FORMAT_PLUSGIRO]);
81
82
            $bankgiroFactory = new AccountFactory;
83
            $bankgiroFactory->whitelistFormats([BankNames::FORMAT_BANKGIRO]);
84
85
            $visitors->addVisitor(new AccountVisitor($errorObj, $accountFactory, $bankgiroFactory));
86
        }
87
88
        if (!$flag(self::VISITOR_IGNORE_AMOUNTS)) {
89
            $visitors->addVisitor(new AmountVisitor($errorObj));
90
        }
91
92
        if (!$flag(self::VISITOR_IGNORE_IDS)) {
93
            $visitors->addVisitor(
94
                new IdVisitor(
95
                    $errorObj,
96
                    new OrganizationIdFactory,
97
                    new PersonalIdFactory(new CoordinationIdFactory(new NullIdFactory))
98
                )
99
            );
100
        }
101
102
        return $visitors;
103
    }
104
}
105