CsvReader   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 8
dl 0
loc 85
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
B read() 0 24 4
B buildSheet() 0 42 6
1
<?php
2
declare(strict_types = 1);
3
4
namespace Spreadsheet\Reader;
5
6
use Spreadsheet\{
7
    SpreadsheetInterface,
8
    Spreadsheet,
9
    Exception\NestedDirectoryReadNotSupportedException,
10
    Exception\InvalidArgumentException,
11
    SheetInterface,
12
    Sheet,
13
    Cell,
14
    Position
15
};
16
use Innmind\Filesystem\{
17
    FileInterface,
18
    DirectoryInterface
19
};
20
21
final class CsvReader implements ReaderInterface
22
{
23
    private $delimiter;
24
    private $useFirstLineAsColumnIdentifier;
25
26 6
    public function __construct(
27
        string $delimiter,
28
        bool $useFirstLineAsColumnIdentifier
29
    ) {
30 6
        if (empty($delimiter)) {
31 1
            throw new InvalidArgumentException;
32
        }
33
34 5
        $this->delimiter = $delimiter;
35 5
        $this->useFirstLineAsColumnIdentifier = $useFirstLineAsColumnIdentifier;
36 5
    }
37
38 4
    public function read(FileInterface $file): SpreadsheetInterface
39
    {
40 4
        $spreadsheet = new Spreadsheet(
41 4
            basename((string) $file->name(), '.csv')
42
        );
43
44 4
        if ($file instanceof DirectoryInterface) {
45 2
            foreach ($file as $subFile) {
46 2
                if ($subFile instanceof DirectoryInterface) {
47 1
                    throw new NestedDirectoryReadNotSupportedException;
48
                }
49
50 1
                $spreadsheet = $spreadsheet->add(
51 1
                    $this->buildSheet($subFile)
52
                );
53
            }
54
        } else {
55 2
            $spreadsheet = $spreadsheet->add(
56 2
                $this->buildSheet($file)
57
            );
58
        }
59
60 3
        return $spreadsheet;
61
    }
62
63 3
    private function buildSheet(FileInterface $file): SheetInterface
64
    {
65 3
        $csv = tmpfile();
66 3
        $stream = $file->content()->rewind();
67
68 3
        while (!$stream->isEof()) {
69 3
            fwrite($csv, $stream->read(8192));
70
        }
71
72 3
        rewind($csv);
73 3
        $sheet = new Sheet(
74 3
            basename((string) $file->name(), '.csv')
75
        );
76
77 3
        $position = 1;
78 3
        $firstLine = fgetcsv($csv, 0, $this->delimiter);
79
80 3
        if (!$this->useFirstLineAsColumnIdentifier) {
81 1
            rewind($csv);
82
        }
83
84 3
        while (($line = fgetcsv($csv, 0, $this->delimiter)) !== false) {
85 3
            foreach ($line as $key => $value) {
86 3
                $sheet = $sheet->add(
87 3
                    new Cell(
88 3
                        new Position(
89 3
                            $this->useFirstLineAsColumnIdentifier ?
90 3
                                $firstLine[$key] : $key,
91
                            $position
92
                        ),
93
                        $value
94
                    )
95
                );
96
            }
97
98 3
            ++$position;
99
        }
100
101 3
        fclose($csv);
102
103 3
        return $sheet;
104
    }
105
}
106