Completed
Pull Request — master (#705)
by
unknown
02:51
created

ReaderFactory::createODSReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Box\Spout\Reader\Common\Creator;
4
5
use Box\Spout\Common\Creator\HelperFactory;
6
use Box\Spout\Common\Exception\UnsupportedTypeException;
7
use Box\Spout\Common\Type;
8
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory;
9
use Box\Spout\Reader\CSV\Manager\OptionsManager as CSVOptionsManager;
10
use Box\Spout\Reader\CSV\Reader as CSVReader;
11
use Box\Spout\Reader\ODS\Creator\HelperFactory as ODSHelperFactory;
12
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory as ODSInternalEntityFactory;
13
use Box\Spout\Reader\ODS\Creator\ManagerFactory as ODSManagerFactory;
14
use Box\Spout\Reader\ODS\Manager\OptionsManager as ODSOptionsManager;
15
use Box\Spout\Reader\ODS\Reader as ODSReader;
16
use Box\Spout\Reader\ReaderInterface;
17
use Box\Spout\Reader\XLSX\Creator\HelperFactory as XLSXHelperFactory;
18
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory as XLSXInternalEntityFactory;
19
use Box\Spout\Reader\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
20
use Box\Spout\Reader\XLSX\Manager\OptionsManager as XLSXOptionsManager;
21
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
22
use Box\Spout\Reader\XLSX\Reader as XLSXReader;
23
24
/**
25
 * Class ReaderFactory
26
 * This factory is used to create readers, based on the type of the file to be read.
27
 * It supports CSV, XLSX and ODS formats.
28
 */
29
class ReaderFactory
30
{
31
    /**
32
     * File extensions and readers mapped
33
     */
34
    protected const MAPPED_EXTENSIONS = [
35
        'xlsx' => 'xlsx',
36
        'csv' => 'csv',
37
        'ods' => 'ods',
38
        'xlsm' => 'xlsx',
39
    ];
40
41
    /**
42
     * Creates a reader by file extension
43
     *
44
     * @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
45
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
46
     * @return ReaderInterface
47
     */
48 13
    public static function createFromFile(string $path)
49
    {
50 13
        $extension = \strtolower(\pathinfo($path, PATHINFO_EXTENSION));
51
52 13
        return self::createFromType($extension);
53
    }
54
55 97
    public static function getMappedFormats(string $fileExtension)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
    {
57 97
        if (!(array_key_exists($fileExtension, self::MAPPED_EXTENSIONS))) {
58 3
            throw new UnsupportedTypeException('No readers supporting the given type: ' . $fileExtension);
59
        }
60
61 94
        return self::MAPPED_EXTENSIONS[$fileExtension];
62
    }
63
64
    /**
65
     * This creates an instance of the appropriate reader, given the type of the file to be read
66
     *
67
     * @param  string $readerType Type of the reader to instantiate
68
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
69
     * @return ReaderInterface
70
     */
71 97
    public static function createFromType($readerType)
72
    {
73 97
        $readerType = self::getMappedFormats($readerType);
74 94
        $methodName = 'create' . strtoupper($readerType) . 'Reader';
75
76 94
        return self::$methodName();
77
    }
78
79
    /**
80
     * @return CSVReader
81
     */
82 7
    private static function createCSVReader()
83
    {
84 7
        $optionsManager = new CSVOptionsManager();
85 7
        $helperFactory = new HelperFactory();
86 7
        $entityFactory = new CSVInternalEntityFactory($helperFactory);
87 7
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
88
89 7
        return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
90
    }
91
92
    /**
93
     * @return XLSXReader
94
     */
95 50
    private static function createXLSXReader()
96
    {
97 50
        $optionsManager = new XLSXOptionsManager();
98 50
        $helperFactory = new XLSXHelperFactory();
99 50
        $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
100 50
        $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
101 50
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
102
103 50
        return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
104
    }
105
106
    /**
107
     * @return ODSReader
108
     */
109 37
    private static function createODSReader()
110
    {
111 37
        $optionsManager = new ODSOptionsManager();
112 37
        $helperFactory = new ODSHelperFactory();
113 37
        $managerFactory = new ODSManagerFactory();
114 37
        $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
115 37
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
116
117 37
        return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
118
    }
119
}
120