Completed
Pull Request — develop_3.0 (#642)
by Adrien
01:54
created

WriterFactory::getCSVWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
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\Writer\Common\Creator;
4
5
use Box\Spout\Common\Creator\HelperFactory;
6
use Box\Spout\Common\Exception\UnsupportedTypeException;
7
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
8
use Box\Spout\Common\Type;
9
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
10
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager;
11
use Box\Spout\Writer\CSV\Writer as CSVWriter;
12
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory;
13
use Box\Spout\Writer\ODS\Creator\ManagerFactory as ODSManagerFactory;
14
use Box\Spout\Writer\ODS\Manager\OptionsManager as ODSOptionsManager;
15
use Box\Spout\Writer\ODS\Writer as ODSWriter;
16
use Box\Spout\Writer\WriterInterface;
17
use Box\Spout\Writer\XLSX\Creator\HelperFactory as XLSXHelperFactory;
18
use Box\Spout\Writer\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
19
use Box\Spout\Writer\XLSX\Manager\OptionsManager as XLSXOptionsManager;
20
use Box\Spout\Writer\XLSX\Writer as XLSXWriter;
21
22
/**
23
 * Class WriterFactory
24
 * This factory is used to create writers, based on the type of the file to be read.
25
 * It supports CSV, XLSX and ODS formats.
26
 */
27
class WriterFactory
28
{
29
    /**
30
     * This creates an instance of the appropriate writer, given the extension of the file to be written
31
     *
32
     * @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
33
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
34
     * @return WriterInterface
35
     */
36 5
    public static function createFromFile(string $path)
37
    {
38 5
        $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
39
40 5
        return self::createFromType($extension);
41
    }
42
43
    /**
44
     * This creates an instance of the appropriate writer, given the type of the file to be written
45
     *
46
     * @param string $writerType Type of the writer to instantiate
47
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
48
     * @return WriterInterface
49
     */
50 102
    public static function createFromType($writerType)
51
    {
52
        switch ($writerType) {
53 102
            case Type::CSV: return self::createCSVWriter();
54 89
            case Type::XLSX: return self::createXLSXWriter();
55 44
            case Type::ODS: return self::createODSWriter();
56
            default:
57 2
                throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
58
        }
59
    }
60
61
    /**
62
     * @return CSVWriter
63
     */
64 13
    private static function createCSVWriter()
65
    {
66 13
        $optionsManager = new CSVOptionsManager();
67 13
        $globalFunctionsHelper = new GlobalFunctionsHelper();
68
69 13
        $helperFactory = new HelperFactory();
70
71 13
        return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory);
72
    }
73
74
    /**
75
     * @return XLSXWriter
76
     */
77 45
    private static function createXLSXWriter()
78
    {
79 45
        $styleBuilder = new StyleBuilder();
80 45
        $optionsManager = new XLSXOptionsManager($styleBuilder);
81 45
        $globalFunctionsHelper = new GlobalFunctionsHelper();
82
83 45
        $helperFactory = new XLSXHelperFactory();
84 45
        $managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
85
86 45
        return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
87
    }
88
89
    /**
90
     * @return ODSWriter
91
     */
92 42
    private static function createODSWriter()
93
    {
94 42
        $styleBuilder = new StyleBuilder();
95 42
        $optionsManager = new ODSOptionsManager($styleBuilder);
96 42
        $globalFunctionsHelper = new GlobalFunctionsHelper();
97
98 42
        $helperFactory = new ODSHelperFactory();
99 42
        $managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
100
101 42
        return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
102
    }
103
}
104