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

ExcelReader::count()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 12
cp 0.9167
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 5
nop 0
crap 4.0092
1
<?php
2
3
namespace Mathielen\DataImport\Reader;
4
5
class ExcelReader extends \Ddeboer\DataImport\Reader\ExcelReader
6
{
7
    private $activeSheet;
8
    private $file;
9
10
    /**
11
     * @var \PHPExcel_Reader_IReader
12
     */
13
    private $reader;
14
    private $maxRows = null;
15
    private $maxCol = null;
16
17 4
    public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null, $maxCol = null)
18
    {
19 4
        $this->file = $file;
20 4
        $this->activeSheet = $activeSheet;
21
22 4
        $this->reader = \PHPExcel_IOFactory::createReaderForFile($file->getPathName());
23 4
        $this->reader->setReadDataOnly($readOnly);
24
25 4
        if (!is_null($headerRowNumber)) {
26 2
            $this->headerRowNumber = $headerRowNumber;
27 2
            $headerReader = clone $this->reader;
28 2
            $headerReader->setReadFilter(new ReadFilter($headerRowNumber + 1));
29
30
            /** @var \PHPExcel $excel */
31 2
            $excel = $headerReader->load($file->getPathname());
32
33 2
            if (null !== $activeSheet) {
34
                $excel->setActiveSheetIndex($activeSheet);
35
            }
36
37 2
            $rows = $excel->getActiveSheet()->toArray();
38 2
            $this->columnHeaders = $rows[$headerRowNumber];
39
40
            //set max col from header length if not already given
41 2
            if (is_null($maxCol)) {
42 2
                $maxCol = \PHPExcel_Cell::stringFromColumnIndex(count($this->columnHeaders) - 1);
43
            }
44
        }
45
46 4
        $this->setBoundaries($maxRows, $maxCol);
47 4
    }
48
49 4
    public function setBoundaries($maxRows = null, $maxCol = null)
50
    {
51 4
        $this->setMaxRows($maxRows);
52 4
        $this->setMaxCol($maxCol);
53 4
    }
54
55 4
    public function setMaxRows($maxRows = null)
56
    {
57 4
        $this->maxRows = $maxRows;
58 4
    }
59
60 4
    public function setMaxCol($maxCol = null)
61
    {
62 4
        $this->maxCol = $maxCol;
63 4
    }
64
65 2
    public function count()
66
    {
67 2
        if (is_null($this->count)) {
68 2
            $countReader = clone $this->reader;
69 2
            $countReader->setReadFilter(new ReadFilter($this->maxRows, 'A'));
70
71
            /** @var \PHPExcel $excel */
72 2
            $excel = $countReader->load($this->file->getPathname());
73
74 2
            if (null !== $this->activeSheet) {
75
                $excel->setActiveSheetIndex($this->activeSheet);
76
            }
77
78 2
            $maxRowMaxCol = $excel->getActiveSheet()->getHighestRowAndColumn();
79 2
            $this->count = $maxRowMaxCol['row'];
80
81 2
            if (null !== $this->headerRowNumber) {
82 2
                --$this->count;
83
            }
84
        }
85
86 2
        return $this->count;
87
    }
88
89 2
    public function rewind()
90
    {
91 2
        if (is_null($this->worksheet)) {
92 2
            $this->reader->setReadFilter(new ReadFilter($this->maxRows, $this->maxCol));
93
94
            /** @var \PHPExcel $excel */
95 2
            $excel = $this->reader->load($this->file->getPathname());
96
97 2
            if (null !== $this->activeSheet) {
98
                $excel->setActiveSheetIndex($this->activeSheet);
99
            }
100
101 2
            $this->worksheet = $excel->getActiveSheet()->toArray();
102
        }
103
104 2
        parent::rewind();
105 2
    }
106
}
107
108
class ReadFilter implements \PHPExcel_Reader_IReadFilter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
109
{
110
    private $maxRows;
111
    private $maxColIdx;
112
113 4
    public function __construct($maxRows = null, $maxCol = null)
114
    {
115 4
        $this->maxRows = $maxRows;
116 4
        $this->maxColIdx = $maxCol ? \PHPExcel_Cell::columnIndexFromString($maxCol) : null;
117 4
    }
118
119 4
    public function readCell($column, $row, $worksheetName = '')
120
    {
121 4
        if (isset($this->maxRows) && $row > $this->maxRows) {
122 2
            return false;
123
        }
124 4
        if (isset($this->maxColIdx) && \PHPExcel_Cell::columnIndexFromString($column) > $this->maxColIdx) {
125 2
            return false;
126
        }
127
128 4
        return true;
129
    }
130
}
131