Completed
Push — master ( e938ca...eb16f0 )
by Patrick
02:57
created

ExcelSerializer::serializeData()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 37
Code Lines 23

Duplication

Lines 8
Ratio 21.62 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 8
eloc 23
c 2
b 1
f 1
nc 14
nop 2
dl 8
loc 37
rs 5.3846
1
<?php
2
namespace Serialize;
3
4
require_once dirname(__FILE__).'/../libs/PHPExcel/Classes/PHPExcel.php';
5
6
class ExcelSerializer extends SpreadSheetSerializer
7
{
8
    protected $types = array('xlsx', 'xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel');
9
10
    public function serializeData($type, $array)
11
    {
12
        if($this->supportsType($type) === false)
13
        {
14
            return null;
15
        }
16
        if(count($array) === 0)
17
        {
18
            return null;
19
        }
20
        $data = $this->getArray($array);
21
        $ssheat = new \PHPExcel();
22
        $sheat = $ssheat->setActiveSheetIndex(0);
23
        $keys = array_shift($data);
24
        $rowCount = count($data);
25
        $colCount = count($keys);
26 View Code Duplication
        for($i = 0; $i < $colCount; $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...
27
        {
28
            $sheat->setCellValueByColumnAndRow($i, 1, $keys[$i]);
29
        }
30
        for($i = 0; $i < $rowCount; $i++)
31
        {
32 View Code Duplication
            for($j = 0; $j < $colCount; $j++)
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...
33
            {
34
                $sheat->setCellValueByColumnAndRow($j, (2 + $i), $data[$i][$j]);
35
            }
36
        }
37
        $writerType = 'Excel5';
38
        if(strcasecmp($type, 'xlsx') === 0 || strcasecmp($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') === 0)
39
        {
40
            $writerType = 'Excel2007';
41
        }
42
        $writer = \PHPExcel_IOFactory::createWriter($ssheat, $writerType);
43
        ob_start();
44
        $writer->save('php://output');
45
        return ob_get_clean();
46
    }
47
}
48