Passed
Pull Request — master (#3)
by Chema
07:57
created

EdifactParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 12 2
A __construct() 0 3 1
A create() 0 3 1
A __invoke() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser;
6
7
use EDI\Parser;
8
use EdifactParser\Exception\InvalidFile;
9
use EdifactParser\Segments\SegmentFactory;
10
use EdifactParser\Segments\SegmentFactoryInterface;
11
12
/** @psalmphp-immutable */
13
final class EdifactParser
14
{
15
    private SegmentFactoryInterface $segmentFactory;
16
17 4
    public static function create(?SegmentFactoryInterface $segmentFactory = null): self
18
    {
19 4
        return new self($segmentFactory ?? new SegmentFactory());
20
    }
21
22 4
    private function __construct(SegmentFactoryInterface $segmentFactory)
23
    {
24 4
        $this->segmentFactory = $segmentFactory;
25 4
    }
26
27 1
    public function __invoke(string $fileContent): TransactionResult
28
    {
29 1
        return $this->parse($fileContent);
30
    }
31
32 4
    public function parse(string $fileContent): TransactionResult
33
    {
34 4
        $parser = new Parser($fileContent);
35 4
        $errors = $parser->errors();
36
37 4
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38 1
            throw InvalidFile::withErrors($errors);
39
        }
40
41 3
        $segmentedValues = SegmentedValues::factory($this->segmentFactory)->fromRaw($parser->get());
42
43 3
        return TransactionResult::fromSegmentedValues($segmentedValues);
44
    }
45
}
46