fileExtensionToFormat()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 8.5866
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 7
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage\Format\Discovery;
4
5
use Mathielen\ImportEngine\Storage\Format\CsvFormat;
6
use Mathielen\ImportEngine\Storage\Format\ExcelFormat;
7
use Mathielen\ImportEngine\Storage\Format\XmlFormat;
8
use Mathielen\ImportEngine\Storage\Format\CompressedFormat;
9
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
10
use Mathielen\ImportEngine\ValueObject\StorageSelection;
11
12
class FileExtensionDiscoverStrategy extends AbstractDiscoverStrategy
13
{
14
    /**
15
     * (non-PHPdoc).
16
     *
17
     * @see \Mathielen\ImportEngine\Storage\Format\Discovery\FormatDiscoverStrategyInterface::getFormat()
18
     */
19 8
    public function getFormat(StorageSelection $selection)
20
    {
21 8
        $ext = pathinfo($selection->getId(), PATHINFO_EXTENSION);
22
23 8
        $type = $this->discoverFormat($ext, $selection->getId());
24
25 7
        return $type;
26
    }
27
28 8
    private function discoverFormat($ext, $uri)
29
    {
30 8
        if (isset($this->formatFactories[$ext])) {
31 1
            return $this->formatFactories[$ext]->factor($uri);
32
        }
33
34 7
        return self::fileExtensionToFormat($ext);
35
    }
36
37 7
    public static function fileExtensionToFormat($ext)
38
    {
39
        //defaults
40
        switch ($ext) {
41 7
            case 'zip':
42
                /*if ($subInformation) {
43
                    list($subMimeType, $subFile) = explode('@', $subInformation);
44
45
                    return new CompressedFormat($subFile, 'zip', $this->mimeTypeToFormat($subMimeType));
46
                } else {*/
47
48 1
                    return new CompressedFormat();
49
                //}
50 6
            case 'csv':
51 5
            case 'txt':
52 2
                return new CsvFormat();
53 4
            case 'xls':
54 3
            case 'xlsx':
55 2
                return new ExcelFormat();
56 2
            case 'xml':
57 1
                return new XmlFormat();
58
            default:
59 1
                throw new InvalidConfigurationException("Unknown file-extension: '$ext'.");
60
        }
61
    }
62
}
63