Completed
Push — master ( 310596...ecf895 )
by Hannes
02:21
created

MandateResponseStrategy::on01()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 42
ccs 0
cts 26
cp 0
rs 8.8571
cc 1
eloc 19
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro\Parser\Strategy;
6
7
use byrokrat\autogiro\Parser\StateMachine;
8
use byrokrat\autogiro\Parser\Matcher;
9
use byrokrat\autogiro\Line;
10
11
/**
12
 * Strategy for parsing responses to previously made mandate requests
13
 */
14
class MandateResponseStrategy implements Strategy
15
{
16
    public function createStates(): StateMachine
17
    {
18
        return new StateMachine([
19
            StateMachine::STATE_INIT => ['01'],
20
            '01' => ['73', '09'],
21
            '73' => ['73', '09'],
22
            '09' => [StateMachine::STATE_DONE]
23
        ]);
24
    }
25
26
    public function begin()
27
    {
28
        $this->records = [];
0 ignored issues
show
Bug introduced by
The property records does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
    }
30
31
    public function on01(Line $line)
32
    {
33
        /*
34
            TODO Readers kan olika beroende av om det är ny eller gammal layout
35
                därför ska detta vara DI, så kan jag bygga en strategi som passar med
36
                filen jag har framför mig..
37
         */
38
        $openingRecordReader = new \byrokrat\autogiro\Parser\RecordReader(
39
            [
40
                'tc' => new Matcher\Text(1, '01'),
41
                'autogiro' => new Matcher\Text(3, str_pad('AUTOGIRO', 20, ' ')),
42
                'backup1' => new Matcher\Space(23, 2),
43
                'date' => new Matcher\Number(25, 8),
44
                'backup2' => new Matcher\Space(33, 12),
45
                'layout' => new Matcher\Text(45, str_pad('AG-MEDAVI', 20, ' ')),
46
                'customerNr' => new Matcher\Number(65, 6),
47
                'bankgiro' => new Matcher\Number(71, 10),
48
            ],
49
            function (array $values) {
50
                // TODO ska såklart vara DI...
51
                $factory = new \byrokrat\banking\AccountFactory;
52
                $factory->whitelistFormats([\byrokrat\banking\BankNames::FORMAT_BANKGIRO]);
53
54
                return new \byrokrat\autogiro\Record\OpeningRecord(
55
                    trim($values['layout']),
56
                    \DateTimeImmutable::createFromFormat('Ymd', $values['date']),
57
                    $factory->createAccount(ltrim($values['bankgiro'], '0')),
0 ignored issues
show
Compatibility introduced by
$factory->createAccount(...lues['bankgiro'], '0')) of type object<byrokrat\banking\AccountNumber> is not a sub-type of object<byrokrat\banking\Bankgiro>. It seems like you assume a concrete implementation of the interface byrokrat\banking\AccountNumber to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
                    ltrim($values['customerNr'], '0')
59
                );
60
            }
61
        );
62
63
        // TODO definiera en Section som första steg för att definiera Parser return value
64
            // kan vara riktigt enkel...
65
        // TODO skriv test för Parser
66
        // TODO börjar det se bra ut? Utvärdera och tänk...
67
68
        // TODO nu är det tydligen självklart så att asylgrp har gammal layout..
69
            // innan jag börjar hårdjobba med format osv måste jag ha testing strategy på plats!!
70
71
        $this->records[] = $openingRecordReader->readRecord($line);
72
    }
73
74
    public function on73(Line $line)
0 ignored issues
show
Unused Code introduced by
The parameter $line is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
    }
77
78
    public function on09(Line $line)
0 ignored issues
show
Unused Code introduced by
The parameter $line is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
    }
81
82
    public function done(): array
83
    {
84
        return $this->records;
85
    }
86
}
87