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

WriterFactory::createCSVWriter()   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 5
        return self::createFromType($extension);
40
    }
41
42
    /**
43
     * This creates an instance of the appropriate writer, given the type of the file to be written
44
     *
45
     * @param string $writerType Type of the writer to instantiate
46
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
47
     * @return WriterInterface
48
     */
49 102
    public static function createFromType($writerType)
50
    {
51
        switch ($writerType) {
52 102
            case Type::CSV: return self::createCSVWriter();
53 89
            case Type::XLSX: return self::createXLSXWriter();
54 44
            case Type::ODS: return self::createODSWriter();
55
            default:
56 2
                throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
57
        }
58
    }
59
60
    /**
61
     * @return CSVWriter
62
     */
63 13
    private static function createCSVWriter()
64
    {
65 13
        $optionsManager = new CSVOptionsManager();
66 13
        $globalFunctionsHelper = new GlobalFunctionsHelper();
67
68 13
        $helperFactory = new HelperFactory();
69
70 13
        return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory);
71
    }
72
73
    /**
74
     * @return XLSXWriter
75
     */
76 45
    private static function createXLSXWriter()
77
    {
78 45
        $styleBuilder = new StyleBuilder();
79 45
        $optionsManager = new XLSXOptionsManager($styleBuilder);
80 45
        $globalFunctionsHelper = new GlobalFunctionsHelper();
81
82 45
        $helperFactory = new XLSXHelperFactory();
83 45
        $managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
84
85 45
        return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
86
    }
87
88
    /**
89
     * @return ODSWriter
90
     */
91 42
    private static function createODSWriter()
92
    {
93 42
        $styleBuilder = new StyleBuilder();
94 42
        $optionsManager = new ODSOptionsManager($styleBuilder);
95 42
        $globalFunctionsHelper = new GlobalFunctionsHelper();
96
97 42
        $helperFactory = new ODSHelperFactory();
98 42
        $managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
99
100 42
        return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
101
    }
102
}
103