Completed
Push — master ( 762802...4e2811 )
by Markus
14:15 queued 25s
created

ExcelWriter::prepare()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 8.8571
cc 6
eloc 12
nc 9
nop 0
crap 6
1
<?php
2
3
namespace Ddeboer\DataImport\Writer;
4
5
use Ddeboer\DataImport\Writer;
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 implements Writer
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 boolean
33
     */
34
    protected $prependHeaderRow;
35
36
    /**
37
     * @var PHPExcel
38
     */
39
    protected $excel;
40
41
    /**
42
     * @var integer
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 boolean        $prependHeaderRow
51
     */
52 4
    public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
53
    {
54 4
        $this->filename = $file->getPathname();
55 4
        $this->sheet = $sheet;
56 4
        $this->type = $type;
57 4
        $this->prependHeaderRow = $prependHeaderRow;
58 4
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    public function prepare()
64
    {
65 4
        $reader = PHPExcel_IOFactory::createReader($this->type);
66 4
        if ($reader->canRead($this->filename)) {
67 1
            $this->excel = $reader->load($this->filename);
68 1
        } else {
69 4
            $this->excel = new PHPExcel();
70 4
            if(null !== $this->sheet && !$this->excel->sheetNameExists($this->sheet))
71 4
            {
72 1
                $this->excel->removeSheetByIndex(0);
73 1
            }
74
        }
75
76 4
        if (null !== $this->sheet) {
77 1
            if (!$this->excel->sheetNameExists($this->sheet)) {
78 1
                $this->excel->createSheet()->setTitle($this->sheet);
79 1
            }
80 1
            $this->excel->setActiveSheetIndexByName($this->sheet);
81 1
        }
82 4
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function writeItem(array $item)
88
    {
89 4
        $count = count($item);
90
91 4
        if ($this->prependHeaderRow && 1 == $this->row) {
92 1
            $headers = array_keys($item);
93
94 1
            for ($i = 0; $i < $count; $i++) {
95 1
                $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]);
96 1
            }
97 1
            $this->row++;
98 1
        }
99
100 4
        $values = array_values($item);
101
102 4
        for ($i = 0; $i < $count; $i++) {
103 4
            $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
104 4
        }
105
106 4
        $this->row++;
107 4
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 4
    public function finish()
113
    {
114 4
        $writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
115 4
        $writer->save($this->filename);
116 4
    }
117
}
118