Completed
Pull Request — develop_3.0 (#556)
by Adrien
03:20
created

ReaderFactory::getXLSXReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
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
     * This creates an instance of the appropriate reader, given the type of the file to be read
33
     *
34
     * @param  string $readerType Type of the reader to instantiate
35
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
36
     * @return ReaderInterface
37
     */
38 80
    public static function create($readerType)
39
    {
40
        switch ($readerType) {
41 80
            case Type::CSV: return self::getCSVReader();
42 79
            case Type::XLSX: return self::getXLSXReader();
43 35
            case Type::ODS: return self::getODSReader();
44
            default:
45 1
                throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
46
        }
47
    }
48
49
    /**
50
     * @return CSVReader
51
     */
52 1
    private static function getCSVReader()
53
    {
54 1
        $optionsManager = new CSVOptionsManager();
55 1
        $helperFactory = new HelperFactory();
56 1
        $entityFactory = new CSVInternalEntityFactory($helperFactory);
57 1
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
58
59 1
        return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
60
    }
61
62
    /**
63
     * @return XLSXReader
64
     */
65 44
    private static function getXLSXReader()
66
    {
67 44
        $optionsManager = new XLSXOptionsManager();
68 44
        $helperFactory = new XLSXHelperFactory();
69 44
        $managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
70 44
        $entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
71 44
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
72
73 44
        return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
74
    }
75
76
    /**
77
     * @return ODSReader
78
     */
79 34
    private static function getODSReader()
80
    {
81 34
        $optionsManager = new ODSOptionsManager();
82 34
        $helperFactory = new ODSHelperFactory();
83 34
        $managerFactory = new ODSManagerFactory();
84 34
        $entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
85 34
        $globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
86
87 34
        return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
88
    }
89
}
90