Completed
Push — master ( 378c5b...5052d2 )
by Markus
08:56
created

ExcelWriter::finish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Mathielen\DataImport\Writer;
4
5
use Ddeboer\DataImport\Writer\AbstractWriter;
6
use PHPExcel;
7
use PHPExcel_IOFactory;
8
9
/**
10
 * Writes to an Excel file.
11
 *
12
 * @author David de Boer <[email protected]>
13
 */
14
class ExcelWriter extends AbstractWriter
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $filename;
20
21
    /**
22
     * @var null|string
23
     */
24
    protected $sheet;
25
26
    /**
27
     * @var string
28
     */
29
    protected $type;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $prependHeaderRow;
35
36
    /**
37
     * @var PHPExcel
38
     */
39
    protected $excel;
40
41
    /**
42
     * @var int
43
     */
44
    protected $row = 1;
45
46
    /**
47
     * @param \SplFileObject $file             File
48
     * @param string         $sheet            Sheet title (optional)
49
     * @param string         $type             Excel file type (defaults to Excel2007)
50
     * @param bool           $prependHeaderRow
51
     */
52 2
    public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
53
    {
54 2
        $this->filename = $file->getPathname();
55 2
        $this->sheet = $sheet;
56 2
        $this->type = $type;
57 2
        $this->prependHeaderRow = $prependHeaderRow;
58 2
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function prepare()
64
    {
65 2
        $reader = PHPExcel_IOFactory::createReader($this->type);
66 2
        if ($reader->canRead($this->filename)) {
67
            $this->excel = $reader->load($this->filename);
68
        } else {
69 2
            $this->excel = new PHPExcel();
70
        }
71
72 2
        if (null !== $this->sheet) {
73
            if (!$this->excel->sheetNameExists($this->sheet)) {
74
                $this->excel->createSheet()->setTitle($this->sheet);
75
            }
76
            $this->excel->setActiveSheetIndexByName($this->sheet);
77
        }
78 2
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function writeItem(array $item)
84
    {
85 2
        $count = count($item);
86
87 2
        if ($this->prependHeaderRow && 1 == $this->row) {
88
            $headers = array_keys($item);
89
90 View Code Duplication
            for ($i = 0; $i < $count; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
                $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]);
92
            }
93
            ++$this->row;
94
        }
95
96 2
        $values = array_values($item);
97
98 2 View Code Duplication
        for ($i = 0; $i < $count; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99 2
            $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
100
        }
101
102 2
        ++$this->row;
103 2
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function finish()
109
    {
110 2
        $writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
111 2
        $writer->save($this->filename);
112 2
    }
113
}
114