Passed
Pull Request — develop_3.0 (#490)
by Adrien
02:41
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\Common\Manager\Style\StyleMerger;
11
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager;
12
use Box\Spout\Writer\CSV\Writer as CSVWriter;
13
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory;
14
use Box\Spout\Writer\ODS\Creator\ManagerFactory as ODSManagerFactory;
15
use Box\Spout\Writer\ODS\Manager\OptionsManager as ODSOptionsManager;
16
use Box\Spout\Writer\ODS\Writer as ODSWriter;
17
use Box\Spout\Writer\WriterInterface;
18
use Box\Spout\Writer\XLSX\Creator\HelperFactory as XLSXHelperFactory;
19
use Box\Spout\Writer\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
20
use Box\Spout\Writer\XLSX\Manager\OptionsManager as XLSXOptionsManager;
21
use Box\Spout\Writer\XLSX\Writer as XLSXWriter;
22
23
/**
24
 * Class WriterFactory
25
 * This factory is used to create writers, based on the type of the file to be read.
26
 * It supports CSV, XLSX and ODS formats.
27
 */
28
class WriterFactory
29
{
30
    /**
31
     * This creates an instance of the appropriate writer, given the type of the file to be read
32
     *
33
     * @param  string $writerType Type of the writer to instantiate
34
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
35
     * @return WriterInterface
36
     */
37 95
    public function create($writerType)
38
    {
39
        switch ($writerType) {
40 95
            case Type::CSV: return $this->getCSVWriter();
41 84
            case Type::XLSX: return $this->getXLSXWriter();
42 41
            case Type::ODS: return $this->getODSWriter();
43
            default:
44 1
                throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
45
        }
46
    }
47
48
    /**
49
     * @return CSVWriter
50
     */
51 11
    private function getCSVWriter()
52
    {
53 11
        $optionsManager = new CSVOptionsManager();
54 11
        $styleMerger = new StyleMerger();
55 11
        $globalFunctionsHelper = new GlobalFunctionsHelper();
56
57 11
        $helperFactory = new HelperFactory();
58
59 11
        return new CSVWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory);
60
    }
61
62
    /**
63
     * @return XLSXWriter
64
     */
65 43
    private function getXLSXWriter()
66
    {
67 43
        $styleBuilder = new StyleBuilder();
68 43
        $optionsManager = new XLSXOptionsManager($styleBuilder);
69 43
        $styleMerger = new StyleMerger();
70 43
        $globalFunctionsHelper = new GlobalFunctionsHelper();
71
72 43
        $helperFactory = new XLSXHelperFactory();
73 43
        $managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
74
75 43
        return new XLSXWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
76
    }
77
78
    /**
79
     * @return ODSWriter
80
     */
81 40
    private function getODSWriter()
82
    {
83 40
        $styleBuilder = new StyleBuilder();
84 40
        $optionsManager = new ODSOptionsManager($styleBuilder);
85 40
        $styleMerger = new StyleMerger();
86 40
        $globalFunctionsHelper = new GlobalFunctionsHelper();
87
88 40
        $helperFactory = new ODSHelperFactory();
89 40
        $managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
90
91 40
        return new ODSWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
92
    }
93
}
94