Completed
Push — master ( 85851c...4b020b )
by Harry
03:30
created

ParserFactory::getParser()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 27
loc 27
ccs 13
cts 13
cp 1
rs 8.439
cc 5
eloc 16
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Graze\DataFile\Format\Parser;
4
5
use Graze\DataFile\Format\CsvFormatInterface;
6
use Graze\DataFile\Format\FormatAwareInterface;
7
use Graze\DataFile\Format\FormatInterface;
8
use Graze\DataFile\Format\JsonFormatInterface;
9
use Graze\DataFile\Node\FileNodeInterface;
10
use InvalidArgumentException;
11
12 View Code Duplication
class ParserFactory implements ParserFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * @param FormatInterface $format
16
     *
17
     * @return ParserInterface
18
     */
19 8
    public function getParser(FormatInterface $format)
20
    {
21 8
        switch ($format->getType()) {
22 8
            case 'csv':
23 4
                if ($format instanceof CsvFormatInterface) {
24 3
                    return new CsvParser($format);
25
                } else {
26 1
                    throw new InvalidArgumentException(
27 1
                        "Format indicates it is csv but does not implement CsvFormatInterface"
28
                    );
29
                }
30
31
            // fallthrough
32 4
            case 'json':
33 3
                if ($format instanceof JsonFormatInterface) {
34 2
                    return new JsonParser($format);
35
                } else {
36 1
                    throw new InvalidArgumentexception(
37 1
                        "Format indicates it is json but does not implement JsonFormatInterface"
38
                    );
39
                }
40
41
            // fallthrough
42
            default:
43 1
                throw new InvalidArgumentException("Supplied format: {$format->getType()} is unknown");
44
        }
45
    }
46
}
47