Completed
Pull Request — develop_3.0 (#423)
by Adrien
02:30
created

WriterFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 65
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 22 4
A getCSVWriter() 0 6 1
A getXLSXWriter() 0 7 1
A getODSWriter() 0 7 1
1
<?php
2
3
namespace Box\Spout\Writer;
4
5
use Box\Spout\Common\Exception\UnsupportedTypeException;
6
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
7
use Box\Spout\Common\Type;
8
use Box\Spout\Writer\Style\StyleBuilder;
9
10
/**
11
 * Class WriterFactory
12
 * This factory is used to create writers, based on the type of the file to be read.
13
 * It supports CSV, XLSX and ODS formats.
14
 *
15
 * @package Box\Spout\Writer
16
 */
17
class WriterFactory
18
{
19
    /**
20
     * This creates an instance of the appropriate writer, given the type of the file to be read
21
     *
22
     * @api
23
     * @param  string $writerType Type of the writer to instantiate
24
     * @return WriterInterface
25
     * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
26
     */
27 113
    public static function create($writerType)
28
    {
29 113
        $writer = null;
30
31
        switch ($writerType) {
32 113
            case Type::CSV:
33 13
                $writer = self::getCSVWriter();
34 13
                break;
35 100
            case Type::XLSX:
36 51
                $writer = self::getXLSXWriter();
37 51
                break;
38 49
            case Type::ODS:
39 48
                $writer = self::getODSWriter();
40 48
                break;
41
            default:
42 1
                throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
43
        }
44
45 112
        $writer->setGlobalFunctionsHelper(new GlobalFunctionsHelper());
46
47 112
        return $writer;
48
    }
49
50
    /**
51
     * @return CSV\Writer
52
     */
53 13
    private static function getCSVWriter()
54
    {
55 13
        $optionsManager = new CSV\Manager\OptionsManager();
56
57 13
        return new CSV\Writer($optionsManager);
58
    }
59
60
    /**
61
     * @return XLSX\Writer
62
     */
63 51
    private static function getXLSXWriter()
64
    {
65 51
        $styleBuilder = new StyleBuilder();
66 51
        $optionsManager = new XLSX\Manager\OptionsManager($styleBuilder);
67
68 51
        return new XLSX\Writer($optionsManager);
69
    }
70
71
    /**
72
     * @return ODS\Writer
73
     */
74 48
    private static function getODSWriter()
75
    {
76 48
        $styleBuilder = new StyleBuilder();
77 48
        $optionsManager = new ODS\Manager\OptionsManager($styleBuilder);
78
79 48
        return new ODS\Writer($optionsManager);
80
    }
81
}
82