Failed Conditions
Pull Request — master (#393)
by Stefan
02:59
created

Writer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 144
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setTempFolder() 0 7 1
A setShouldUseInlineStrings() 0 7 1
A setShouldUseCellAutosizing() 0 7 1
A openWriter() 0 14 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\XLSX\Internal\Workbook;
7
use Box\Spout\Writer\Style\StyleBuilder;
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 bool Determine whether cell widths should be calculated */
31
    protected $shouldUseCellAutosizing = false;
32
33
    /** @var Internal\Workbook The workbook for the XLSX file */
34
    protected $book;
35
36
    /**
37
     * Sets a custom temporary folder for creating intermediate files/folders.
38
     * This must be set before opening the writer.
39
     *
40
     * @api
41
     * @param string $tempFolder Temporary folder where the files to create the XLSX will be stored
42
     * @return Writer
43
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
44
     */
45 6
    public function setTempFolder($tempFolder)
46
    {
47 6
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
48
49 3
        $this->tempFolder = $tempFolder;
50 3
        return $this;
51
    }
52
53
    /**
54
     * Use inline string to be more memory efficient. If set to false, it will use shared strings.
55
     * This must be set before opening the writer.
56
     *
57
     * @api
58
     * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used
59
     * @return Writer
60
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
61
     */
62 84
    public function setShouldUseInlineStrings($shouldUseInlineStrings)
63
    {
64 84
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
65
66 81
        $this->shouldUseInlineStrings = $shouldUseInlineStrings;
67 81
        return $this;
68
    }
69
70
    /**
71
     * Enable or disable automated calculation of cell sizes to fit the contents of a cell value.
72
     *
73
     * @api
74
     * @param bool $shouldUseCellAutosizing
75
     * @return Writer
76
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
77
     */
78 3
    public function setShouldUseCellAutosizing($shouldUseCellAutosizing)
79
    {
80 3
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
81
82
        $this->shouldUseCellAutosizing = $shouldUseCellAutosizing;
83
        return $this;
84
    }
85
86
    /**
87
     * Configures the write and sets the current sheet pointer to a new sheet.
88
     *
89
     * @return void
90
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
91
     */
92 132
    protected function openWriter()
93
    {
94 132
        if (!$this->book) {
95 132
            $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
96 132
            $this->book = new Workbook(
97 132
                $tempFolder,
98 132
                $this->shouldUseInlineStrings,
99 132
                $this->shouldUseCellAutosizing,
100 132
                $this->shouldCreateNewSheetsAutomatically,
101 132
                $this->defaultRowStyle
102 132
            );
103 132
            $this->book->addNewSheetAndMakeItCurrent();
104 132
        }
105 132
    }
106
107
    /**
108
     * @return Internal\Workbook The workbook representing the file to be written
109
     */
110 102
    protected function getWorkbook()
111
    {
112 102
        return $this->book;
113
    }
114
115
    /**
116
     * Adds data to the currently opened writer.
117
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
118
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
119
     *
120
     * @param array $dataRow Array containing data to be written.
121
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
122
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
123
     * @return void
124
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
125
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
126
     * @throws \Box\Spout\Writer\Exception\WriterException
127
     */
128 93
    protected function addRowToWriter(array $dataRow, $style)
129
    {
130 93
        $this->throwIfBookIsNotAvailable();
131 93
        $this->book->addRowToCurrentWorksheet($dataRow, $style);
132 87
    }
133
134
    /**
135
     * Returns the default style to be applied to rows.
136
     *
137
     * @return \Box\Spout\Writer\Style\Style
138
     */
139 147
    protected function getDefaultRowStyle()
140
    {
141 147
        return (new StyleBuilder())
142 147
            ->setFontSize(self::DEFAULT_FONT_SIZE)
143 147
            ->setFontName(self::DEFAULT_FONT_NAME)
144 147
            ->build();
145
    }
146
147
    /**
148
     * Closes the writer, preventing any additional writing.
149
     * @return void
150
     * @throws \Box\Spout\Common\Exception\IOException
151
     */
152 99
    protected function closeWriter()
153
    {
154 99
        if ($this->book) {
155 99
            $this->book->close($this->filePointer);
156 99
        }
157 99
    }
158
}
159