Completed
Push — master ( 9d4828...f71b73 )
by Hannes
02:28
created

VisitorFactory::createVisitors()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 26
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-18 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
56
        | self::VISITOR_IGNORE_AMOUNTS
57
        | self::VISITOR_IGNORE_IDS;
58
59
    /**
60
     * Create the standard set of visitors used when processing a parse tree
61
     */
62
    public function createVisitors(int $flags = 0): VisitorInterface
63
    {
64
        $flag = function (int $needle) use ($flags) {
65
            return ($needle & $flags) == $needle;
66
        };
67
68
        $errorObj = new ErrorObject;
69
70
        $container = new VisitorContainer(
71
            $errorObj,
72
            new DateVisitor($errorObj),
73
            new MessageVisitor($errorObj),
74
            new PayeeVisitor($errorObj),
75
            new TextVisitor($errorObj),
76
            new PaymentVisitor($errorObj)
77
        );
78
79
        if (!$flag(self::VISITOR_IGNORE_ACCOUNTS)) {
80
            $accountFactory = new AccountFactory;
81
            $accountFactory->blacklistFormats([BankNames::FORMAT_PLUSGIRO]);
82
83
            $bankgiroFactory = new AccountFactory;
84
            $bankgiroFactory->whitelistFormats([BankNames::FORMAT_BANKGIRO]);
85
86
            $container->addVisitor(new AccountVisitor($errorObj, $accountFactory, $bankgiroFactory));
87
        }
88
89
        if (!$flag(self::VISITOR_IGNORE_AMOUNTS)) {
90
            $container->addVisitor(new AmountVisitor($errorObj));
91
        }
92
93
        if (!$flag(self::VISITOR_IGNORE_IDS)) {
94
            $container->addVisitor(
95
                new IdVisitor(
96
                    $errorObj,
97
                    new OrganizationIdFactory,
98
                    new PersonalIdFactory(new CoordinationIdFactory(new NullIdFactory))
99
                )
100
            );
101
        }
102
103
        return $container;
104
    }
105
}
106