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

Writer::setTempFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 3
cts 5
cp 0.6
cc 1
eloc 4
nc 1
nop 1
crap 1.064
1
<?php
2
3
namespace Box\Spout\Writer\ODS;
4
5
use Box\Spout\Writer\AbstractMultiSheetsWriter;
6
use Box\Spout\Writer\Common;
7
use Box\Spout\Writer\ODS\Internal\Workbook;
8
9
/**
10
 * Class Writer
11
 * This class provides base support to write data to ODS files
12
 *
13
 * @package Box\Spout\Writer\ODS
14
 */
15
class Writer extends AbstractMultiSheetsWriter
16
{
17
    /** @var string Content-Type value for the header */
18
    protected static $headerContentType = 'application/vnd.oasis.opendocument.spreadsheet';
19
20
    /** @var string Temporary folder where the files to create the ODS will be stored */
21
    protected $tempFolder;
22
23
    /** @var Internal\Workbook The workbook for the XLSX file */
24
    protected $book;
25
26
    /**
27
     * Sets a custom temporary folder for creating intermediate files/folders.
28
     * This must be set before opening the writer.
29
     *
30
     * @api
31
     * @param string $tempFolder Temporary folder where the files to create the ODS will be stored
32
     * @return Writer
33
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
34
     */
35 3
    public function setTempFolder($tempFolder)
36
    {
37 3
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
38
39
        $this->tempFolder = $tempFolder;
40
        return $this;
41 3
    }
42
43
    /**
44
     * Configures the write and sets the current sheet pointer to a new sheet.
45
     *
46
     * @return void
47
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
48
     */
49 102
    protected function openWriter()
50
    {
51 102
        $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
52 102
        $this->book = new Workbook($tempFolder, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
53 102
        $this->book->addNewSheetAndMakeItCurrent();
54 102
    }
55
56
    /**
57
     * @return Internal\Workbook The workbook representing the file to be written
58
     */
59 78
    protected function getWorkbook()
60
    {
61 78
        return $this->book;
62
    }
63
64
    public function registerStyle($style) {
0 ignored issues
show
Unused Code introduced by
The parameter $style is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
        return false;
66
    }
67
68
    /**
69
     * Adds data to the currently opened writer.
70
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
71
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
72
     *
73
     * @param array $dataRow Array containing data to be written.
74
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
75
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
76
     * @return void
77
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
78
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
79
     */
80 69
    protected function addRowToWriter(array $dataRow, $style)
81
    {
82 69
        $this->throwIfBookIsNotAvailable();
83 69
        $this->book->addRowToCurrentWorksheet($dataRow, $style);
84 66
    }
85
86
    /**
87
     * Closes the writer, preventing any additional writing.
88
     *
89
     * @return void
90
     */
91 72
    protected function closeWriter()
92
    {
93 72
        if ($this->book) {
94 72
            $this->book->close($this->filePointer);
95 72
        }
96 72
    }
97
}
98