Completed
Pull Request — master (#557)
by Adrien
03:10
created

WriterFactory::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4
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 type of the file to be read
31
     *
32
     * @param  string $writerType Type of the writer to instantiate
33
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
34
     * @return WriterInterface
35
     */
36 100
    public function create($writerType)
37
    {
38
        switch ($writerType) {
39 100
            case Type::CSV: return $this->getCSVWriter();
40 88
            case Type::XLSX: return $this->getXLSXWriter();
41 43
            case Type::ODS: return $this->getODSWriter();
42
            default:
43 1
                throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
44
        }
45
    }
46
47
    /**
48
     * @return CSVWriter
49
     */
50 12
    private function getCSVWriter()
51
    {
52 12
        $optionsManager = new CSVOptionsManager();
53 12
        $globalFunctionsHelper = new GlobalFunctionsHelper();
54
55 12
        $helperFactory = new HelperFactory();
56
57 12
        return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory);
58
    }
59
60
    /**
61
     * @return XLSXWriter
62
     */
63 45
    private function getXLSXWriter()
64
    {
65 45
        $styleBuilder = new StyleBuilder();
66 45
        $optionsManager = new XLSXOptionsManager($styleBuilder);
67 45
        $globalFunctionsHelper = new GlobalFunctionsHelper();
68
69 45
        $helperFactory = new XLSXHelperFactory();
70 45
        $managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
71
72 45
        return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
73
    }
74
75
    /**
76
     * @return ODSWriter
77
     */
78 42
    private function getODSWriter()
79
    {
80 42
        $styleBuilder = new StyleBuilder();
81 42
        $optionsManager = new ODSOptionsManager($styleBuilder);
82 42
        $globalFunctionsHelper = new GlobalFunctionsHelper();
83
84 42
        $helperFactory = new ODSHelperFactory();
85 42
        $managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
86
87 42
        return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
88
    }
89
}
90