ParserFactory::getParser()   B
last analyzed

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 0
Metric Value
dl 27
loc 27
ccs 13
cts 13
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Format\Parser;
15
16
use Graze\DataFile\Format\CsvFormatInterface;
17
use Graze\DataFile\Format\FormatInterface;
18
use Graze\DataFile\Format\JsonFormatInterface;
19
use InvalidArgumentException;
20
21 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...
22
{
23
    /**
24
     * @param FormatInterface $format
25
     *
26
     * @return ParserInterface
27
     */
28 9
    public function getParser(FormatInterface $format)
29
    {
30 9
        switch ($format->getType()) {
31 9
            case 'csv':
32 5
                if ($format instanceof CsvFormatInterface) {
33 4
                    return new CsvParser($format);
34
                } else {
35 1
                    throw new InvalidArgumentException(
36 1
                        "Format indicates it is csv but does not implement CsvFormatInterface"
37
                    );
38
                }
39
40
            // fallthrough
41 4
            case 'json':
42 3
                if ($format instanceof JsonFormatInterface) {
43 2
                    return new JsonParser($format);
44
                } else {
45 1
                    throw new InvalidArgumentexception(
46 1
                        "Format indicates it is json but does not implement JsonFormatInterface"
47
                    );
48
                }
49
50
            // fallthrough
51
            default:
52 1
                throw new InvalidArgumentException("Supplied format: {$format->getType()} is unknown");
53
        }
54
    }
55
}
56