Completed
Push — master ( a29d2d...4681dd )
by Hannes
10:53 queued 08:08
created

ParserFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 15
dl 0
loc 65
rs 9.1666
c 0
b 0
f 0

1 Method

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