Completed
Pull Request — master (#2)
by René
08:24 queued 04:55
created

Csv   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutput() 0 16 1
A getCsvContentsFromHandle() 0 11 2
1
<?php
2
3
namespace ReneDeKat\Blm\Drivers;
4
5
use ReneDeKat\Blm\Reader;
6
7
class Csv extends Reader
8
{
9
    /**
10
     * Returns a string with CSV data.
11
     *
12
     * @return string
13
     */
14
    public function getOutput()
15
    {
16
        $delimiter = ',';
17
18
        $handle = fopen('php://memory', 'w');
19
20
        fputcsv($handle, $this->getDefinitions()->toArray(), $delimiter);
21
22
        $this->getData()->each(function ($record) use ($handle, $delimiter) {
23
            fputcsv($handle, $record, $delimiter);
24
        });
25
26
        fseek($handle, 0);
27
28
        return $this->getCsvContentsFromHandle($handle);
29
    }
30
31
    /**
32
     * Get CSV contents from file handle.
33
     *
34
     * @param $handle
35
     *
36
     * @return string
37
     */
38
    private function getCsvContentsFromHandle($handle)
39
    {
40
        ob_start();
41
        while (($line = fgets($handle)) !== false) {
42
            echo $line;
43
        }
44
        $actualContents = ob_get_contents();
45
        ob_end_clean();
46
47
        return $actualContents;
48
    }
49
}
50