Completed
Push — master ( 5c880c...25348b )
by Freek
01:50 queued 10s
created

SimpleExcelWriter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 106
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A respond() 0 8 1
A __construct() 0 8 1
A getPath() 0 4 1
A getWriter() 0 4 1
A getNumberOfRows() 0 4 1
A noHeaderRow() 0 6 1
A addRow() 0 17 4
A writeHeaderFromRow() 0 9 1
A streamToBrowser() 0 5 1
A close() 0 4 1
A __destruct() 0 4 1
1
<?php
2
3
namespace Spatie\SimpleExcel;
4
5
use Box\Spout\Writer\WriterInterface;
6
use Box\Spout\Common\Entity\Style\Style;
7
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
8
9
class SimpleExcelWriter
10
{
11
    /** @var \Box\Spout\Writer\WriterInterface */
12
    private $writer;
13
14
    private $path = '';
15
16
    private $processHeader = true;
17
18
    private $processingFirstRow = true;
19
20
    private $numberOfRows = 0;
21
22
    public static function create(string $file)
23
    {
24
        return new static($file);
25
    }
26
27
    public static function respond(string $downloadName)
28
    {
29
        $simpleExcelWriter = new static($downloadName);
30
31
        $simpleExcelWriter->getWriter()->openToBrowser($downloadName);
32
33
        return $simpleExcelWriter;
34
    }
35
36
    public function __construct(string $path)
37
    {
38
        $this->writer = WriterEntityFactory::createWriterFromFile($path);
39
40
        $this->path = $path;
41
42
        $this->writer->openToFile($this->path);
43
    }
44
45
    public function getPath(): string
46
    {
47
        return $this->path;
48
    }
49
50
    public function getWriter(): WriterInterface
51
    {
52
        return $this->writer;
53
    }
54
55
    public function getNumberOfRows(): int
56
    {
57
        return $this->numberOfRows;
58
    }
59
60
    public function noHeaderRow()
61
    {
62
        $this->processHeader = false;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param \Box\Spout\Common\Entity\Row|array $row
69
     * @param \Box\Spout\Common\Entity\Style\Style|null $style
70
     */
71
    public function addRow($row, Style $style = null)
72
    {
73
        if (is_array($row)) {
74
            if ($this->processHeader && $this->processingFirstRow) {
75
                $this->writeHeaderFromRow($row);
76
            }
77
78
            $row = WriterEntityFactory::createRowFromArray($row, $style);
79
        }
80
81
        $this->writer->addRow($row);
82
        $this->numberOfRows++;
83
84
        $this->processingFirstRow = false;
85
86
        return $this;
87
    }
88
89
    protected function writeHeaderFromRow(array $row)
90
    {
91
        $headerValues = array_keys($row);
92
93
        $headerRow = WriterEntityFactory::createRowFromArray($headerValues);
94
95
        $this->writer->addRow($headerRow);
96
        $this->numberOfRows++;
97
    }
98
99
    public function streamToBrowser()
100
    {
101
        $this->writer->close();
102
        exit;
103
    }
104
105
    public function close()
106
    {
107
        $this->writer->close();
108
    }
109
110
    public function __destruct()
111
    {
112
        $this->close();
113
    }
114
}
115