Passed
Pull Request — master (#59)
by Raúl
04:04
created

CsvHandler::serialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 9.9332
1
<?php
2
/**
3
 * Mime Type: text/csv
4
 * @author Raja Kapur <[email protected]>
5
 */
6
7
namespace Httpful\Handlers;
8
9
class CsvHandler extends MimeHandlerAdapter
10
{
11
    /**
12
     * @param string $body
13
     * @return mixed
14
     * @throws \Exception
15
     */
16
    public function parse($body)
17
    {
18
        if (empty($body))
19
            return null;
20
21
        $parsed = array();
22
        $fp = fopen('data://text/plain;base64,' . base64_encode($body), 'r');
23
        while (($r = fgetcsv($fp)) !== FALSE) {
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fgetcsv() 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

23
        while (($r = fgetcsv(/** @scrutinizer ignore-type */ $fp)) !== FALSE) {
Loading history...
24
            $parsed[] = $r;
25
        }
26
27
        if (empty($parsed))
28
            throw new \Exception("Unable to parse response as CSV");
29
        return $parsed;
30
    }
31
32
    /**
33
     * @param mixed $payload
34
     * @return string
35
     */
36
    public function serialize($payload)
37
    {
38
        $fp = fopen('php://temp/maxmemory:'. (6*1024*1024), 'r+');
39
        $i = 0;
40
        foreach ($payload as $fields) {
41
            if($i++ == 0) {
42
                fputcsv($fp, array_keys($fields));
0 ignored issues
show
Bug introduced by
It seems like $fp 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

42
                fputcsv(/** @scrutinizer ignore-type */ $fp, array_keys($fields));
Loading history...
43
            }
44
            fputcsv($fp, $fields);
45
        }
46
        rewind($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp 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

46
        rewind(/** @scrutinizer ignore-type */ $fp);
Loading history...
47
        $data = stream_get_contents($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp 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

47
        $data = stream_get_contents(/** @scrutinizer ignore-type */ $fp);
Loading history...
48
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp 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

48
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
49
        return $data;
50
    }
51
}
52