Failed Conditions
Pull Request — master (#532)
by
unknown
03:45
created

Workbook::setColumnsWidth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Internal;
4
5
use Box\Spout\Writer\Common\Internal\AbstractWorkbook;
6
use Box\Spout\Writer\XLSX\Helper\FileSystemHelper;
7
use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper;
8
use Box\Spout\Writer\XLSX\Helper\StyleHelper;
9
use Box\Spout\Writer\Common\Sheet;
10
11
/**
12
 * Class Workbook
13
 * Represents a workbook within a XLSX file.
14
 * It provides the functions to work with worksheets.
15
 *
16
 * @package Box\Spout\Writer\XLSX\Internal
17
 */
18
class Workbook extends AbstractWorkbook
19
{
20
    /**
21
     * Maximum number of rows a XLSX sheet can contain
22
     * @see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx
23
     */
24
    protected static $maxRowsPerWorksheet = 1048576;
25
26
    /** @var bool Whether inline or shared strings should be used */
27
    protected $shouldUseInlineStrings;
28
29
    /** @var \Box\Spout\Writer\XLSX\Helper\FileSystemHelper Helper to perform file system operations */
30
    protected $fileSystemHelper;
31
32
    /** @var \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper Helper to write shared strings */
33
    protected $sharedStringsHelper;
34
35
    /** @var \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles */
36
    protected $styleHelper;
37
38
    /** @var array Collection of column width */
39
    protected $columnsWidth;
40
41
    /**
42
     * @param string $tempFolder
43
     * @param bool $shouldUseInlineStrings
44
     * @param bool $shouldCreateNewSheetsAutomatically
45
     * @param array $columnsWidth
46
     * @param \Box\Spout\Writer\Style\Style $defaultRowStyle
47
     * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
48
     */
49 44
    public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle, $columnsWidth)
50
    {
51 44
        parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle);
52
53 44
        $this->shouldUseInlineStrings = $shouldUseInlineStrings;
54
55 44
        $this->fileSystemHelper = new FileSystemHelper($tempFolder);
56 44
        $this->fileSystemHelper->createBaseFilesAndFolders();
57
58 44
        $this->styleHelper = new StyleHelper($defaultRowStyle);
59 44
        $this->columnsWidth = $columnsWidth;
60
61
        // This helper will be shared by all sheets
62 44
        $xlFolder = $this->fileSystemHelper->getXlFolder();
63 44
        $this->sharedStringsHelper = new SharedStringsHelper($xlFolder);
64 44
    }
65
66
    /**
67
     * @return \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles to XLSX files
68
     */
69 31
    protected function getStyleHelper()
70
    {
71 31
        return $this->styleHelper;
72
    }
73
74
    /**
75
     * @return int Maximum number of rows/columns a sheet can contain
76
     */
77 31
    protected function getMaxRowsPerWorksheet()
78
    {
79 31
        return self::$maxRowsPerWorksheet;
80
    }
81
82
    /**
83
     * Defines column width for one or more columns of the worksheet.
84
     *
85
     * @param array $columnsWidth
86
     * @return void
87
     */
88
    public function setColumnsWidth($columnsWidth)
89
    {
90
        $this->columnsWidth = $columnsWidth;
91
    }
92
93
    /**
94
     * Creates a new sheet in the workbook. The current sheet remains unchanged.
95
     *
96
     * @return Worksheet The created sheet
97
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
98
     */
99 44
    public function addNewSheet()
100
    {
101 44
        $newSheetIndex = count($this->worksheets);
102 44
        $sheet = new Sheet($newSheetIndex, $this->internalId);
103
104 44
        $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder();
105 44
        $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings, $this->columnsWidth);
106 44
        $this->worksheets[] = $worksheet;
107
108 44
        return $worksheet;
109
    }
110
111
    /**
112
     * Closes the workbook and all its associated sheets.
113
     * All the necessary files are written to disk and zipped together to create the XLSX file.
114
     * All the temporary files are then deleted.
115
     *
116
     * @param resource $finalFilePointer Pointer to the XLSX that will be created
117
     * @return void
118
     */
119 34
    public function close($finalFilePointer)
120
    {
121
        /** @var Worksheet[] $worksheets */
122 34
        $worksheets = $this->worksheets;
123
124 34
        foreach ($worksheets as $worksheet) {
125 34
            $worksheet->close();
126
        }
127
128 34
        $this->sharedStringsHelper->close();
129
130
        // Finish creating all the necessary files before zipping everything together
131 34
        $this->fileSystemHelper
132 34
            ->createContentTypesFile($worksheets)
133 34
            ->createWorkbookFile($worksheets)
134 34
            ->createWorkbookRelsFile($worksheets)
135 34
            ->createStylesFile($this->styleHelper)
136 34
            ->zipRootFolderAndCopyToStream($finalFilePointer);
137
138 34
        $this->cleanupTempFolder();
139 34
    }
140
141
    /**
142
     * Deletes the root folder created in the temp folder and all its contents.
143
     *
144
     * @return void
145
     */
146 34
    protected function cleanupTempFolder()
147
    {
148 34
        $xlsxRootFolder = $this->fileSystemHelper->getRootFolder();
149 34
        $this->fileSystemHelper->deleteFolderRecursively($xlsxRootFolder);
150 34
    }
151
}
152