Completed
Push — develop ( 950ae4...d73b85 )
by Baptiste
02:48
created

CsvWriter::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 2
nop 3
crap 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Spreadsheet\Writer;
5
6
use Spreadsheet\{
7
    SpreadsheetInterface,
8
    Exception\InvalidArgumentException,
9
    SheetInterface,
10
    RowInterface,
11
    CellInterface,
12
    File\Csv,
13
    Formatter\FormatterInterface
14
};
15
use Innmind\Filesystem\{
16
    FileInterface,
17
    Directory,
18
    Stream\Stream
19
};
20
use Innmind\Immutable\MapInterface;
21
22
final class CsvWriter implements WriterInterface
23
{
24
    private $delimiter;
25
    private $withHeader;
26
27 8
    public function __construct(
28
        string $delimiter,
29
        bool $withHeader,
30
        MapInterface $formatters
31
    ) {
32
        if (
33 8
            empty($delimiter) ||
34 7
            (string) $formatters->keyType() !== 'string' ||
35 8
            (string) $formatters->valueType() !== FormatterInterface::class
36
        ) {
37 2
            throw new InvalidArgumentException;
38
        }
39
40 6
        $this->delimiter = $delimiter;
41 6
        $this->withHeader = $withHeader;
42 6
        $this->formatters = $formatters;
0 ignored issues
show
Bug introduced by
The property formatters does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43 6
    }
44
45 5
    public function write(SpreadsheetInterface $spreadsheet): FileInterface
46
    {
47
        $directory = $spreadsheet
48 5
            ->sheets()
49 5
            ->reduce(
50 5
                new Directory($spreadsheet->name()),
51
                function(Directory $carry, string $name, SheetInterface $sheet): Directory {
52 5
                    return $carry->add($this->buildFile($sheet));
53 5
                }
54
            );
55
56 5
        if ($directory->count() === 1) {
57 4
            return $directory->get(
58 4
                $spreadsheet->sheets()->values()->first()->name().'.csv'
59
            );
60
        }
61
62 1
        return $directory;
63
    }
64
65 5
    private function buildFile(SheetInterface $sheet): FileInterface
66
    {
67 5
        $csv = tmpfile();
68
        $columns = $sheet
69 5
            ->columns()
70 5
            ->keys()
71
            ->sort(function($a, $b): bool {
72 4
                return $a > $b;
73 5
            })
74 5
            ->toPrimitive();
75 5
        $default = array_fill_keys(array_values($columns), '');
76
77 5
        if ($this->withHeader) {
78 2
            fputcsv($csv, $columns, $this->delimiter);
79
        }
80
81
        $sheet
82 5
            ->rows()
83 5
            ->values()
84
            ->sort(function(RowInterface $a, RowInterface $b): bool {
85 4
                return $a->identifier() > $b->identifier();
86 5
            })
87 5
            ->reduce(
88
                $csv,
89
                function($carry, RowInterface $row) use ($default) {
90 5
                    fputcsv(
91
                        $carry,
92 5
                        $this->fill($default, $row->cells()),
93 5
                        $this->delimiter
94
                    );
95
96 5
                    return $carry;
97 5
                }
98
            );
99
100 5
        return new Csv($sheet->name(), new Stream($csv));
101
    }
102
103 5
    private function fill(array $default, MapInterface $cells): array
104
    {
105 5
        $line = $cells->reduce(
106
            $default,
107 5
            function(array $carry, $column, CellInterface $cell): array {
108 5
                $class = get_class($cell);
109
110 5
                if ($this->formatters->contains($class)) {
111 1
                    $carry[$column] = $this
112 1
                        ->formatters
113 1
                        ->get($class)
114 1
                        ->format($cell);
115
                } else {
116 4
                    $carry[$column] = (string) $cell;
117
                }
118
119 5
                return $carry;
120 5
            }
121
        );
122
123 5
        return array_values($line);
124
    }
125
}
126