Completed
Push — master ( 71d84f...5581d8 )
by Hannes
01:45
created

VisitorFactory::createVisitors()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 1
dl 0
loc 46
rs 8.867
c 0
b 0
f 0
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\BankgiroFactory;
31
use byrokrat\banking\DelegatingFactory;
32
33
/**
34
 * Creates the set of standard visitors
35
 */
36
class VisitorFactory
37
{
38
    /**
39
     * Do not include account number visitor
40
     */
41
    const VISITOR_IGNORE_ACCOUNTS = 1;
42
43
    /**
44
     * Do not include amount visitor
45
     */
46
    const VISITOR_IGNORE_AMOUNTS = 2;
47
48
    /**
49
     * Do not include id visitor
50
     */
51
    const VISITOR_IGNORE_IDS = 4;
52
53
    /**
54
     * Ignore all visitors based on external dependencies
55
     */
56
    const VISITOR_IGNORE_EXTERNAL = self::VISITOR_IGNORE_ACCOUNTS
57
        | self::VISITOR_IGNORE_AMOUNTS
58
        | self::VISITOR_IGNORE_IDS;
59
60
    /**
61
     * Do not include strict validation visitors
62
     */
63
    const VISITOR_IGNORE_STRICT_VALIDATION = 8;
64
65
    /**
66
     * Create the standard set of visitors used when processing a parse tree
67
     */
68
    public function createVisitors(int $flags = 0): VisitorInterface
69
    {
70
        $flag = function (int $needle) use ($flags) {
71
            return ($needle & $flags) == $needle;
72
        };
73
74
        $errorObj = new ErrorObject;
75
76
        $container = new VisitorContainer(
77
            $errorObj,
78
            new DateVisitor($errorObj),
79
            new MessageVisitor($errorObj),
80
            new TextVisitor($errorObj),
81
            new PaymentVisitor($errorObj)
82
        );
83
84
        if (!$flag(self::VISITOR_IGNORE_ACCOUNTS)) {
85
            $container->addVisitor(
86
                new AccountVisitor(
87
                    $errorObj,
88
                    new DelegatingFactory(new AccountFactory, new BankgiroFactory),
89
                    new BankgiroFactory
90
                )
91
            );
92
        }
93
94
        if (!$flag(self::VISITOR_IGNORE_AMOUNTS)) {
95
            $container->addVisitor(new AmountVisitor($errorObj));
96
        }
97
98
        if (!$flag(self::VISITOR_IGNORE_IDS)) {
99
            $container->addVisitor(
100
                new IdVisitor(
101
                    $errorObj,
102
                    new OrganizationIdFactory,
103
                    new PersonalIdFactory(new CoordinationIdFactory(new NullIdFactory))
104
                )
105
            );
106
        }
107
108
        if (!$flag(self::VISITOR_IGNORE_STRICT_VALIDATION)) {
109
            $container->addVisitor(new PayeeVisitor($errorObj));
110
        }
111
112
        return $container;
113
    }
114
}
115