Completed
Push — master ( 9e3ac7...521b2a )
by Hannes
9s
created

ParserFactory   B

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 16
dl 0
loc 66
rs 8.4614
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createParser() 0 40 4
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;
24
25
use byrokrat\autogiro\Processor\AccountProcessor;
26
use byrokrat\autogiro\Processor\AmountProcessor;
27
use byrokrat\autogiro\Processor\BankgiroProcessor;
28
use byrokrat\autogiro\Processor\DateProcessor;
29
use byrokrat\autogiro\Processor\FileProcessor;
30
use byrokrat\autogiro\Processor\IdProcessor;
31
use byrokrat\autogiro\Processor\LayoutProcessor;
32
use byrokrat\autogiro\Processor\MessageProcessor;
33
use byrokrat\autogiro\Processor\MultiCore;
34
use byrokrat\autogiro\Processor\PayeeProcessor;
35
use byrokrat\id\CoordinationIdFactory;
36
use byrokrat\id\NullIdFactory;
37
use byrokrat\id\OrganizationIdFactory;
38
use byrokrat\id\PersonalIdFactory;
39
use byrokrat\banking\AccountFactory;
40
use byrokrat\banking\Formats as AccountFormats;
41
42
/**
43
 * Simplifies the creation of parser objects
44
 */
45
class ParserFactory
46
{
47
    /**
48
     * Do not include account number processor in parser
49
     */
50
    const NO_ACCOUNT_PROCESSOR = 1;
51
52
    /**
53
     * Do not include amount processor in parser
54
     */
55
    const NO_AMOUNT_PROCESSOR = 2;
56
57
    /**
58
     * Do not include id processor in parser
59
     */
60
    const NO_ID_PROCESSOR = 4;
61
62
    /**
63
     * Ignore all processors based on external dependencies
64
     */
65
    const NO_EXTERNAL_PROCESSORS = self::NO_ACCOUNT_PROCESSOR | self::NO_AMOUNT_PROCESSOR | self::NO_ID_PROCESSOR;
66
67
    /**
68
     * Create a new parser
69
     */
70
    public function createParser(int $flags = 0): Parser
71
    {
72
        $flag = function (int $needle) use ($flags) {
73
            return ($needle & $flags) == $needle;
74
        };
75
76
        $processors = new MultiCore(
77
            new BankgiroProcessor,
78
            new DateProcessor,
79
            new FileProcessor,
80
            new LayoutProcessor,
81
            new MessageProcessor,
82
            new PayeeProcessor
83
        );
84
85
        if (!$flag(self::NO_ACCOUNT_PROCESSOR)) {
86
            $accountFactory = new AccountFactory;
87
            $accountFactory->blacklistFormats([AccountFormats::FORMAT_PLUSGIRO]);
88
89
            $bankgiroFactory = new AccountFactory;
90
            $bankgiroFactory->whitelistFormats([AccountFormats::FORMAT_BANKGIRO]);
91
92
            $processors->addProcessor(new AccountProcessor($accountFactory, $bankgiroFactory));
93
        }
94
95
        if (!$flag(self::NO_AMOUNT_PROCESSOR)) {
96
            $processors->addProcessor(new AmountProcessor);
97
        }
98
99
        if (!$flag(self::NO_ID_PROCESSOR)) {
100
            $processors->addProcessor(
101
                new IdProcessor(
102
                    new OrganizationIdFactory,
103
                    new PersonalIdFactory(new CoordinationIdFactory(new NullIdFactory))
104
                )
105
            );
106
        }
107
108
        return new Parser(new Grammar, $processors);
109
    }
110
}
111