Passed
Push — master ( 39863f...ab2034 )
by Radu
01:42
created

CsvCreator::getCsvFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
namespace WebServCo\Framework\Files;
3
4
use WebServCo\Framework\Exceptions\ApplicationException;
5
6
final class CsvCreator
7
{
8
    protected $delimiter;
9
    protected $enclosure;
10
11
    public function __construct($delimiter = ',', $enclosure = '"')
12
    {
13
        $this->delimiter = $delimiter;
14
        $this->enclosure = $enclosure;
15
    }
16
17
    public function getCsvFile(string $fileName, array $data, bool $addHeader = true)
18
    {
19
        if (empty($data)) {
20
            throw new ApplicationException('Empty data');
21
        }
22
        $csvData = $this->getCsvData($data, $addHeader);
23
        return new \WebServCo\Framework\Files\CsvFile($fileName, $csvData);
24
    }
25
26
    public function getCsvData(array $data, bool $addHeader = true)
27
    {
28
        try {
29
            // temporary memory wrapper; if bigger than 5MB will be written to temp file.
30
            $handle = fopen('php://temp/maxmemory: ' . (5*1024*1024), 'r+');
31
32
            if ($addHeader) {
33
                fputcsv($handle, array_keys(current($data)), $this->delimiter, $this->enclosure);
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

33
                fputcsv(/** @scrutinizer ignore-type */ $handle, array_keys(current($data)), $this->delimiter, $this->enclosure);
Loading history...
34
            }
35
36
            foreach ($data as $item) {
37
                fputcsv($handle, $item, $this->delimiter, $this->enclosure);
38
            }
39
40
            rewind($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of rewind() 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

40
            rewind(/** @scrutinizer ignore-type */ $handle);
Loading history...
41
42
            $csvData = stream_get_contents($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of stream_get_contents() 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

42
            $csvData = stream_get_contents(/** @scrutinizer ignore-type */ $handle);
Loading history...
43
44
            fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() 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

44
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
45
46
            return $csvData;
47
        } catch (\Exception $e) {
48
            throw new ApplicationException($e->getMessage());
49
        }
50
    }
51
}
52