Passed
Push — master ( 066045...f88ddb )
by Daniel
01:51
created

Writer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addRows() 0 7 3
A setHeader() 0 4 1
A addRow() 0 4 1
A write() 0 13 3
A updateNewLine() 0 4 3
A closeStream() 0 3 1
A openStream() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Csv-Machine package.
5
 *
6
 * (c) Dan McAdams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace RoadBunch\Csv;
13
14
use RoadBunch\Csv\Exception\InvalidInputArrayException;
15
16
/**
17
 * Class Writer
18
 *
19
 * @author  Dan McAdams
20
 * @package RoadBunch\Csv
21
 */
22
class Writer extends Csv implements WriterInterface
23
{
24
    /** @var array */
25
    protected $header;
26
27
    /** @var array */
28
    protected $rows = [];
29
30
    /**
31
     * @param array $header
32
     */
33 3
    public function setHeader(array $header)
34
    {
35 3
        $this->header = $header;
36 3
        array_unshift($this->rows, $header);
37 3
    }
38
39
    /**
40
     * @param  array $row
41
     * @return WriterInterface
42
     */
43 1
    public function addRow(array $row): WriterInterface
44
    {
45 1
        $this->rows[] = $row;
46 1
        return $this;
47
    }
48
49
    /**
50
     * @param array $rows
51
     * @throws InvalidInputArrayException
52
     */
53 3
    public function addRows(array $rows)
54
    {
55 3
        foreach ($rows as $row) {
56 3
            if (!is_array($row)) {
57 1
                throw new InvalidInputArrayException('Element must be an array');
58
            }
59 2
            $this->rows[] = $row;
60
        }
61 2
    }
62
63
    /**
64
     * Write the CSV to the stream
65
     *
66
     * @param string $filename
67
     * @throws \Exception
68
     */
69 2
    public function write(string $filename)
70
    {
71 2
        $handle = $this->openStream($filename);
72
73 2
        foreach ($this->rows as $row) {
74 2
            fputcsv($handle, $row, $this->delimiter, $this->enclosure, $this->escape);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            fputcsv(/** @scrutinizer ignore-type */ $handle, $row, $this->delimiter, $this->enclosure, $this->escape);
Loading history...
75
76 2
            if (is_file($filename)) {
77 2
                $this->updateNewLine($handle);
78
            }
79
        }
80
81 2
        $this->closeStream($handle);
82 2
    }
83
84
    /**
85
     * @param string $filename
86
     * @return bool|resource
87
     * @throws \Exception
88
     */
89 2
    private function openStream(string $filename)
90
    {
91 2
        return fopen($filename, 'w+');
92
    }
93
94
    /**
95
     * @param $handle
96
     */
97 2
    private function closeStream($handle)
98
    {
99 2
        fclose($handle);
100 2
    }
101
102 2
    private function updateNewLine($handle)
103
    {
104 2
        if ((Newline::NEWLINE_LF !== $this->newline) && (0 === fseek($handle, -1, SEEK_CUR))) {
105 1
            fwrite($handle, $this->newline);
106
        }
107 2
    }
108
}
109