Completed
Pull Request — master (#327)
by Adrien
06:29 queued 03:48
created

Writer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 118
rs 10
c 0
b 0
f 0
ccs 31
cts 31
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setShouldUseInlineStrings() 0 7 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
A setTempFolder() 0 7 1
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 6
    public function setTempFolder($tempFolder)
43
    {
44 6
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
45
46 3
        $this->tempFolder = $tempFolder;
47 3
        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 78
    public function setShouldUseInlineStrings($shouldUseInlineStrings)
60
    {
61 78
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
62
63 75
        $this->shouldUseInlineStrings = $shouldUseInlineStrings;
64 75
        return $this;
65
    }
66
67
    /**
68
     * Configures the write and sets the current sheet pointer to a new sheet.
69
     *
70
     * @return void
71
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
72
     */
73 123
    protected function openWriter()
74
    {
75 123
        if (!$this->book) {
76 123
            $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
77 123
            $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
78 123
            $this->book->addNewSheetAndMakeItCurrent();
79 123
        }
80 123
    }
81
82
    /**
83
     * @return Internal\Workbook The workbook representing the file to be written
84
     */
85 96
    protected function getWorkbook()
86
    {
87 96
        return $this->book;
88
    }
89
90
    /**
91
     * Adds data to the currently opened writer.
92
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
93
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
94
     *
95
     * @param array $dataRow Array containing data to be written.
96
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
97
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
98
     * @return void
99
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
100
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
101
     */
102 87
    protected function addRowToWriter(array $dataRow, $style)
103
    {
104 87
        $this->throwIfBookIsNotAvailable();
105 87
        $this->book->addRowToCurrentWorksheet($dataRow, $style);
106 84
    }
107
108
    /**
109
     * Returns the default style to be applied to rows.
110
     *
111
     * @return \Box\Spout\Writer\Style\Style
112
     */
113 138
    protected function getDefaultRowStyle()
114
    {
115 138
        return (new StyleBuilder())
116 138
            ->setFontSize(self::DEFAULT_FONT_SIZE)
117 138
            ->setFontName(self::DEFAULT_FONT_NAME)
118 138
            ->build();
119
    }
120
121
    /**
122
     * Closes the writer, preventing any additional writing.
123
     *
124
     * @return void
125
     */
126 93
    protected function closeWriter()
127
    {
128 93
        if ($this->book) {
129 93
            $this->book->close($this->filePointer);
130 93
        }
131 93
    }
132
}
133