Failed Conditions
Pull Request — master (#209)
by
unknown
02:31
created

Writer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.29%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 11
c 7
b 0
f 2
lcom 1
cbo 3
dl 0
loc 122
ccs 29
cts 34
cp 0.8529
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setTempFolder() 0 7 1
A setShouldUseInlineStrings() 0 7 1
A registerStyle() 0 3 1
A openWriter() 0 8 3
A getWorkbook() 0 4 1
A addRowToWriter() 0 5 1
A getDefaultRowStyle() 0 7 1
A closeWriter() 0 6 2
1
<?php
2
3
namespace Box\Spout\Writer\XLSX;
4
5
use Box\Spout\Writer\AbstractMultiSheetsWriter;
6
use Box\Spout\Writer\Style\StyleBuilder;
7
use Box\Spout\Writer\XLSX\Internal\Workbook;
8
9
/**
10
 * Class Writer
11
 * This class provides base support to write data to XLSX files
12
 *
13
 * @package Box\Spout\Writer\XLSX
14
 */
15
class Writer extends AbstractMultiSheetsWriter
16
{
17
    /** Default style font values */
18
    const DEFAULT_FONT_SIZE = 12;
19
    const DEFAULT_FONT_NAME = 'Calibri';
20
21
    /** @var string Content-Type value for the header */
22
    protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
23
24
    /** @var string Temporary folder where the files to create the XLSX will be stored */
25
    protected $tempFolder;
26
27
    /** @var bool Whether inline or shared strings should be used - inline string is more memory efficient */
28
    protected $shouldUseInlineStrings = true;
29
30
    /** @var Internal\Workbook The workbook for the XLSX file */
31
    protected $book;
32
33
    /**
34
     * Sets a custom temporary folder for creating intermediate files/folders.
35
     * This must be set before opening the writer.
36
     *
37
     * @api
38
     * @param string $tempFolder Temporary folder where the files to create the XLSX will be stored
39
     * @return Writer
40
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
41
     */
42 3
    public function setTempFolder($tempFolder)
43
    {
44 3
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
45
46
        $this->tempFolder = $tempFolder;
47
        return $this;
48
    }
49
50
    /**
51
     * Use inline string to be more memory efficient. If set to false, it will use shared strings.
52
     * This must be set before opening the writer.
53
     *
54
     * @api
55
     * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used
56
     * @return Writer
57
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
58
     */
59 54
    public function setShouldUseInlineStrings($shouldUseInlineStrings)
60
    {
61 54
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
62
63 51
        $this->shouldUseInlineStrings = $shouldUseInlineStrings;
64 51
        return $this;
65
    }
66
67
    public function registerStyle($style) {
68
        $this->book->registerStyle($style);
69
    }
70
71
    /**
72
     * Configures the write and sets the current sheet pointer to a new sheet.
73
     *
74
     * @return void
75
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
76
     */
77 96
    protected function openWriter()
78
    {
79 96
        if (!$this->book) {
80 96
            $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
81 96
            $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
82 96
            $this->book->addNewSheetAndMakeItCurrent();
83 96
        }
84 96
    }
85
86
    /**
87
     * @return Internal\Workbook The workbook representing the file to be written
88
     */
89 69
    protected function getWorkbook()
90
    {
91 69
        return $this->book;
92
    }
93
94
    /**
95
     * Adds data to the currently opened writer.
96
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
97
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
98
     *
99
     * @param array $dataRow Array containing data to be written.
100
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
101
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
102
     * @return void
103
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
104
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
105
     */
106 60
    protected function addRowToWriter(array $dataRow, $style)
107
    {
108 60
        $this->throwIfBookIsNotAvailable();
109 60
        $this->book->addRowToCurrentWorksheet($dataRow, $style);
110 57
    }
111
112
    /**
113
     * Returns the default style to be applied to rows.
114
     *
115
     * @return \Box\Spout\Writer\Style\Style
116
     */
117 111
    protected function getDefaultRowStyle()
118
    {
119 111
        return (new StyleBuilder())
120 111
            ->setFontSize(self::DEFAULT_FONT_SIZE)
121 111
            ->setFontName(self::DEFAULT_FONT_NAME)
122 111
            ->build();
123
    }
124
125
    /**
126
     * Closes the writer, preventing any additional writing.
127
     *
128
     * @return void
129
     */
130 63
    protected function closeWriter()
131
    {
132 63
        if ($this->book) {
133 63
            $this->book->close($this->filePointer);
134 63
        }
135 63
    }
136
}
137