Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

fileExtensionToFormat()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.8142

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 25
ccs 8
cts 12
cp 0.6667
rs 6.7272
cc 7
eloc 14
nc 7
nop 1
crap 8.8142
1
<?php
2
namespace Mathielen\ImportEngine\Storage\Format\Discovery;
3
4
use Mathielen\ImportEngine\Storage\Format\CsvFormat;
5
use Mathielen\ImportEngine\Storage\Format\ExcelFormat;
6
use Mathielen\ImportEngine\Storage\Format\XmlFormat;
7
use Mathielen\ImportEngine\Storage\Format\CompressedFormat;
8
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
9
use Mathielen\ImportEngine\ValueObject\StorageSelection;
10
11
class FileExtensionDiscoverStrategy extends AbstractDiscoverStrategy
12
{
13
14
    /**
15
     * (non-PHPdoc)
16
     * @see \Mathielen\ImportEngine\Storage\Format\Discovery\FormatDiscoverStrategyInterface::getFormat()
17
     */
18 8
    public function getFormat(StorageSelection $selection)
19
    {
20 8
        $ext = pathinfo($selection->getId(), PATHINFO_EXTENSION);
21
22 8
        $type = $this->discoverFormat($ext, $selection->getId());
23
24 7
        return $type;
25
    }
26
27 8
    private function discoverFormat($ext, $uri)
28
    {
29 8
        if (isset($this->formatFactories[$ext])) {
30 1
            return $this->formatFactories[$ext]->factor($uri);
31
        }
32
33 7
        return self::fileExtensionToFormat($ext);
34
    }
35
36 7
    public static function fileExtensionToFormat($ext)
37
    {
38
        //defaults
39
        switch ($ext) {
40 7
            case 'zip':
41
                /*if ($subInformation) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
                    list($subMimeType, $subFile) = explode('@', $subInformation);
43
44
                    return new CompressedFormat($subFile, 'zip', $this->mimeTypeToFormat($subMimeType));
45
                } else {*/
46
47 1
                    return new CompressedFormat();
48
                //}
49
            case 'csv':
50
            case 'txt':
51 2
                return new CsvFormat();
52
            case 'xls':
53
            case 'xlsx':
54 2
                return new ExcelFormat();
55 1
            case 'xml':
56 1
                return new XmlFormat();
57
            default:
58 1
                throw new InvalidConfigurationException("Unknown file-extension: '$ext'.");
59
        }
60
    }
61
62
}
63