GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Exporter::getResponse()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.0808
c 0
b 0
f 0
cc 5
nc 5
nop 4
1
<?php
2
3
namespace Sigmapix\Sonata\ImportBundle\Export;
4
5
use Exporter\Source\SourceIteratorInterface;
6
use Exporter\Writer\CsvWriter;
7
use Exporter\Writer\JsonWriter;
8
use Exporter\Writer\XlsWriter;
9
use Exporter\Writer\XmlWriter;
10
use Symfony\Component\HttpFoundation\StreamedResponse;
11
12
@trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
13
    'The '.__NAMESPACE__.'\Exporter class is deprecated since version 3.1 and will be removed in 4.0.'.
14
    ' Use Exporter\Exporter instead',
15
    E_USER_DEPRECATED
16
);
17
18
/**
19
 * NEXT_MAJOR: remove this class, and the dev dependency.
20
 */
21
class Exporter
22
{
23
    /**
24
     * @param string                  $format
25
     * @param string                  $filename
26
     * @param SourceIteratorInterface $source
27
     *
28
     * @throws \RuntimeException
29
     *
30
     * @return StreamedResponse
31
     */
32
    public function getResponse($format, $filename, SourceIteratorInterface $source, array $defaultHeaders)
33
    {
34
        switch ($format) {
35
            case 'xls':
36
                $writer = new XlsWriter('php://output');
37
                $contentType = 'application/vnd.ms-excel';
38
                break;
39
            case 'xml':
40
                $writer = new XmlWriter('php://output');
41
                $contentType = 'text/xml';
42
                break;
43
            case 'json':
44
                $writer = new JsonWriter('php://output');
45
                $contentType = 'application/json';
46
                break;
47
            case 'csv':
48
                $writer = new CsvWriter('php://output', ';', '"', '\\', true, true);
49
                $contentType = 'text/csv';
50
                break;
51
            default:
52
                throw new \RuntimeException('Invalid format');
53
        }
54
55
        $callback = function () use ($source, $writer, $defaultHeaders) {
56
            $handler = Handler::create($source, $writer, $defaultHeaders);
57
            $handler->export();
58
        };
59
60
        return new StreamedResponse($callback, 200, [
61
            'Content-Type' => $contentType,
62
            'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
63
        ]);
64
    }
65
}
66