Completed
Pull Request — develop_3.0 (#595)
by Hura
05:29
created

ReaderFactory::createFromFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Box\Spout\Reader\Common\Creator;
4
5
use Box\Spout\Common\Creator\HelperFactory;
6
use Box\Spout\Common\Exception\IOException;
7
use Box\Spout\Common\Exception\UnsupportedTypeException;
8
use Box\Spout\Common\Type;
9
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory;
10
use Box\Spout\Reader\CSV\Manager\OptionsManager as CSVOptionsManager;
11
use Box\Spout\Reader\CSV\Reader as CSVReader;
12
use Box\Spout\Reader\ODS\Creator\HelperFactory as ODSHelperFactory;
13
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory as ODSInternalEntityFactory;
14
use Box\Spout\Reader\ODS\Creator\ManagerFactory as ODSManagerFactory;
15
use Box\Spout\Reader\ODS\Manager\OptionsManager as ODSOptionsManager;
16
use Box\Spout\Reader\ODS\Reader as ODSReader;
17
use Box\Spout\Reader\ReaderInterface;
18
use Box\Spout\Reader\XLSX\Creator\HelperFactory as XLSXHelperFactory;
19
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory as XLSXInternalEntityFactory;
20
use Box\Spout\Reader\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
21
use Box\Spout\Reader\XLSX\Manager\OptionsManager as XLSXOptionsManager;
22
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
23
use Box\Spout\Reader\XLSX\Reader as XLSXReader;
24
25
/**
26
 * Class ReaderFactory
27
 * This factory is used to create readers, based on the type of the file to be read.
28
 * It supports CSV, XLSX and ODS formats.
29
 */
30
class ReaderFactory
31
{
32
    /**
33
     * Map file extensions to reader types
34
     * @var array
35
     */
36
    private static $extensionReaderMap = [
37
        'csv' => Type::CSV,
38
        'ods' => Type::ODS,
39
        'xlsx' => Type::XLSX,
40
    ];
41
42
    /**
43
     * This creates an instance of the appropriate reader, given the type of the file to be read
44
     *
45
     * @param  string $readerType Type of the reader to instantiate
46
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
47
     * @return ReaderInterface
48
     */
49 88
    public static function create($readerType)
50
    {
51
        switch ($readerType) {
52 88
            case Type::CSV: return self::getCSVReader();
53 83
            case Type::XLSX: return self::getXLSXReader();
54 37
            case Type::ODS: return self::getODSReader();
55
            default:
56 1
                throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
57
        }
58
    }
59
60
    /**
61
     * Creates a reader by file extension
62
     *
63
     * @param string The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
64
     * @throws \Box\Spout\Common\Exception\IOException
65
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
66
     * @return ReaderInterface
67
     */
68 12
    public static function createFromFile(string $path)
69
    {
70 12
        if (!\is_file($path)) {
71 2
            throw new IOException(
72 2
                sprintf('Could not open "%s" for reading! File does not exist.', $path)
73
            );
74
        }
75
76 10
        $ext = \pathinfo($path, PATHINFO_EXTENSION);
77 10
        $ext = \strtolower($ext);
78 10
        $readerType = self::$extensionReaderMap[$ext] ?? null;
79 10
        if ($readerType === null) {
80 2
            throw new UnsupportedTypeException(
81 2
                sprintf('No readers support the file extension "%s".', $ext)
82
            );
83
        }
84
85 8
        return self::create($readerType);
86
    }
87
88
    /**
89
     * @return CSVReader
90
     */
91 5
    private static function getCSVReader()
92
    {
93 5
        $optionsManager = new CSVOptionsManager();
94 5
        $helperFactory = new HelperFactory();
95 5
        $entityFactory = new CSVInternalEntityFactory($helperFactory);
96 5
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
97
98 5
        return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
99
    }
100
101
    /**
102
     * @return XLSXReader
103
     */
104 46
    private static function getXLSXReader()
105
    {
106 46
        $optionsManager = new XLSXOptionsManager();
107 46
        $helperFactory = new XLSXHelperFactory();
108 46
        $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
109 46
        $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
110 46
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
111
112 46
        return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
113
    }
114
115
    /**
116
     * @return ODSReader
117
     */
118 36
    private static function getODSReader()
119
    {
120 36
        $optionsManager = new ODSOptionsManager();
121 36
        $helperFactory = new ODSHelperFactory();
122 36
        $managerFactory = new ODSManagerFactory();
123 36
        $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
124 36
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
125
126 36
        return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
127
    }
128
}
129