Completed
Pull Request — develop_3.0 (#642)
by Adrien
02:15
created

ReaderFactory::createCSVReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
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
     * Creates a reader by file extension
33
     *
34
     * @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
35
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
36
     * @return ReaderInterface
37
     */
38 12
    public static function createFromFile(string $path)
39
    {
40 12
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
41 12
        return self::createFromType($extension);
42
    }
43
44
    /**
45
     * This creates an instance of the appropriate reader, given the type of the file to be read
46
     *
47
     * @param  string $readerType Type of the reader to instantiate
48
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
49
     * @return ReaderInterface
50
     */
51 92
    public static function createFromType($readerType)
52
    {
53
        switch ($readerType) {
54 92
            case Type::CSV: return self::createCSVReader();
55 85
            case Type::XLSX: return self::createXLSXReader();
56 39
            case Type::ODS: return self::createODSReader();
57
            default:
58 3
                throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
59
        }
60
    }
61
62
    /**
63
     * @return CSVReader
64
     */
65 7
    private static function createCSVReader()
66
    {
67 7
        $optionsManager = new CSVOptionsManager();
68 7
        $helperFactory = new HelperFactory();
69 7
        $entityFactory = new CSVInternalEntityFactory($helperFactory);
70 7
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
71
72 7
        return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
73
    }
74
75
    /**
76
     * @return XLSXReader
77
     */
78 46
    private static function createXLSXReader()
79
    {
80 46
        $optionsManager = new XLSXOptionsManager();
81 46
        $helperFactory = new XLSXHelperFactory();
82 46
        $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
83 46
        $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
84 46
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
85
86 46
        return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
87
    }
88
89
    /**
90
     * @return ODSReader
91
     */
92 36
    private static function createODSReader()
93
    {
94 36
        $optionsManager = new ODSOptionsManager();
95 36
        $helperFactory = new ODSHelperFactory();
96 36
        $managerFactory = new ODSManagerFactory();
97 36
        $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
98 36
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
99
100 36
        return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
101
    }
102
}
103