VisitorFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 8
eloc 50
c 6
b 1
f 0
dl 0
loc 115
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createVisitors() 0 59 8
1
<?php
2
3
/**
4
 * This file is part of byrokrat\autogiro.
5
 *
6
 * byrokrat\autogiro is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\autogiro is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\autogiro\Visitor;
25
26
use byrokrat\autogiro\Money\SignalMoneyParser;
27
use byrokrat\id\CoordinationIdFactory;
28
use byrokrat\id\NullIdFactory;
29
use byrokrat\id\OrganizationIdFactory;
30
use byrokrat\id\PersonalIdFactory;
31
use byrokrat\banking\AccountFactory;
32
use byrokrat\banking\BankgiroFactory;
33
use byrokrat\banking\DelegatingFactory;
34
35
/**
36
 * Creates the set of standard visitors
37
 */
38
class VisitorFactory
39
{
40
    /**
41
     * Do not include account number visitor
42
     */
43
    public const VISITOR_IGNORE_ACCOUNTS = 1;
44
45
    /**
46
     * Do not include amount visitor
47
     */
48
    public const VISITOR_IGNORE_AMOUNTS = 2;
49
50
    /**
51
     * Do not include id visitor
52
     */
53
    public const VISITOR_IGNORE_IDS = 4;
54
55
    /**
56
     * Do not include date visitor
57
     */
58
    public const VISITOR_IGNORE_DATES = 8;
59
60
    /**
61
     * Do not include message visitor
62
     */
63
    public const VISITOR_IGNORE_MESSAGES = 16;
64
65
    /**
66
     * Do not include basic validation visitors
67
     */
68
    public const VISITOR_IGNORE_BASIC_VALIDATION = 32;
69
70
    /**
71
     * Do not include strict validation visitors
72
     */
73
    public const VISITOR_IGNORE_STRICT_VALIDATION = 64;
74
75
    /**
76
     * Ignore all visitors based on external dependencies
77
     */
78
    public const VISITOR_IGNORE_OBJECTS = self::VISITOR_IGNORE_ACCOUNTS
79
        | self::VISITOR_IGNORE_AMOUNTS
80
        | self::VISITOR_IGNORE_IDS
81
        | self::VISITOR_IGNORE_DATES;
82
83
    /**
84
     * Ignore all visitors
85
     */
86
    public const VISITOR_IGNORE_ALL = self::VISITOR_IGNORE_OBJECTS
87
        | self::VISITOR_IGNORE_MESSAGES
88
        | self::VISITOR_IGNORE_BASIC_VALIDATION
89
        | self::VISITOR_IGNORE_STRICT_VALIDATION;
90
91
    /**
92
     * Create the standard set of visitors used when processing a parse tree
93
     */
94
    final public function createVisitors(int $flags = 0): VisitorInterface
95
    {
96
        $flag = function (int $needle) use ($flags) {
97
            return ($needle & $flags) == $needle;
98
        };
99
100
        $errorObj = new ErrorObject();
101
        $container = new VisitorContainer($errorObj);
102
103
        if (!$flag(self::VISITOR_IGNORE_BASIC_VALIDATION)) {
104
            $container->addVisitor(new NumberVisitor($errorObj));
105
            $container->addVisitor(new TextVisitor($errorObj));
106
        }
107
108
        if (!$flag(self::VISITOR_IGNORE_MESSAGES)) {
109
            $container->addVisitor(new MessageVisitor($errorObj));
110
        }
111
112
        if (!$flag(self::VISITOR_IGNORE_DATES)) {
113
            $container->addVisitor(new DateVisitor($errorObj));
114
        }
115
116
        if (!$flag(self::VISITOR_IGNORE_ACCOUNTS)) {
117
            $container->addVisitor(
118
                new AccountVisitor(
119
                    $errorObj,
120
                    new DelegatingFactory(new AccountFactory(), new BankgiroFactory()),
121
                    new BankgiroFactory()
122
                )
123
            );
124
        }
125
126
        if (!$flag(self::VISITOR_IGNORE_AMOUNTS)) {
127
            $container->addVisitor(
128
                new AmountVisitor(
129
                    $errorObj,
130
                    new SignalMoneyParser()
131
                )
132
            );
133
        }
134
135
        if (!$flag(self::VISITOR_IGNORE_IDS)) {
136
            $container->addVisitor(
137
                new StateIdVisitor(
138
                    $errorObj,
139
                    new OrganizationIdFactory(),
140
                    new PersonalIdFactory(new CoordinationIdFactory(new NullIdFactory()))
141
                )
142
            );
143
        }
144
145
        if (!$flag(self::VISITOR_IGNORE_STRICT_VALIDATION)) {
146
            $container->addVisitor(new PaymentVisitor($errorObj));
147
            $container->addVisitor(new PayeeVisitor($errorObj));
148
            $container->addVisitor(new CountingVisitor($errorObj));
149
            $container->addVisitor(new SummaryVisitor($errorObj));
150
        }
151
152
        return $container;
153
    }
154
}
155