Passed
Push — develop_3.0 ( a665b9...78b663 )
by Adrien
04:18 queued 01:35
created

RowManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Box\Spout\Reader\XLSX\Manager;
4
5
use Box\Spout\Reader\Common\Entity\Row;
6
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory;
7
8
/**
9
 * Class RowManager
10
 */
11
class RowManager
12
{
13
    /** @var InternalEntityFactory Factory to create entities */
14
    private $entityFactory;
15
16
    /**
17
     * @param InternalEntityFactory $entityFactory Factory to create entities
18
     */
19 44
    public function __construct(InternalEntityFactory $entityFactory)
20
    {
21 44
        $this->entityFactory = $entityFactory;
22 44
    }
23
24
    /**
25
     * Detect whether a row is considered empty.
26
     * An empty row has all of its cells empty.
27
     *
28
     * @param Row $row
29
     * @return bool
30
     */
31 37
    public function isEmpty(Row $row)
32
    {
33 37
        foreach ($row->getCells() as $cell) {
34 36
            if (!$cell->isEmpty()) {
35 36
                return false;
36
            }
37
        }
38
39 4
        return true;
40
    }
41
42
    /**
43
     * Fills the missing indexes of a row with empty cells.
44
     *
45
     * @param Row $row
46
     * @return Row
47
     */
48 23
    public function fillMissingIndexesWithEmptyCells(Row $row)
49
    {
50 23
        $rowCells = $row->getCells();
51 23
        if (count($rowCells) === 0) {
52 1
            return $row;
53
        }
54
55 22
        $maxCellIndex = max(array_keys($rowCells));
56
57 22
        for ($cellIndex = 0; $cellIndex < $maxCellIndex; $cellIndex++) {
58 20
            if (!isset($rowCells[$cellIndex])) {
59 2
                $row->setCellAtIndex($this->entityFactory->createCell(''), $cellIndex);
60
            }
61
        }
62
63 22
        return $row;
64
    }
65
}
66