Completed
Push — develop ( c9795e...bd3285 )
by Adrien
24:13
created

ChunkReadFilter::setRows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Samples\Sample12;
4
5
use PhpOffice\PhpSpreadsheet\IOFactory;
6
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
7
8
require __DIR__ . '/../Header.php';
9
10
$inputFileType = 'Xls';
11
$inputFileName = __DIR__ . '/sampleData/example2.xls';
12
13
/**  Define a Read Filter class implementing IReadFilter  */
14 View Code Duplication
class ChunkReadFilter implements IReadFilter
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    private $startRow = 0;
17
    private $endRow = 0;
18
19
    /**
20
     * Set the list of rows that we want to read.
21
     *
22
     * @param mixed $startRow
23
     * @param mixed $chunkSize
24
     */
25
    public function setRows($startRow, $chunkSize)
26
    {
27
        $this->startRow = $startRow;
28
        $this->endRow = $startRow + $chunkSize;
29
    }
30
31
    public function readCell($column, $row, $worksheetName = '')
32
    {
33
        //  Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
34
        if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
35
            return true;
36
        }
37
38
        return false;
39
    }
40
}
41
42
$helper->log('Loading file ' . pathinfo($inputFileName, PATHINFO_BASENAME) . ' using IOFactory with a defined reader type of ' . $inputFileType);
43
// Create a new Reader of the type defined in $inputFileType
44
$reader = IOFactory::createReader($inputFileType);
45
46
// Define how many rows we want to read for each "chunk"
47
$chunkSize = 20;
48
// Create a new Instance of our Read Filter
49
$chunkFilter = new ChunkReadFilter();
50
51
// Tell the Reader that we want to use the Read Filter that we've Instantiated
52
$reader->setReadFilter($chunkFilter);
53
54
// Loop to read our worksheet in "chunk size" blocks
55
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
56
    $helper->log('Loading WorkSheet using configurable filter for headings row 1 and for rows ' . $startRow . ' to ' . ($startRow + $chunkSize - 1));
57
    // Tell the Read Filter, the limits on which rows we want to read this iteration
58
    $chunkFilter->setRows($startRow, $chunkSize);
59
    // Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object
60
    $spreadsheet = $reader->load($inputFileName);
61
62
    //	Do some processing here
63
64
    $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
65
    var_dump($sheetData);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($sheetData); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
66
}
67