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

FileExtensionDiscoverStrategy   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 80%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 1
cbo 8
dl 0
loc 52
ccs 16
cts 20
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 8 1
A discoverFormat() 0 8 2
C fileExtensionToFormat() 0 25 7
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