1 | <?php |
||||||||
2 | |||||||||
3 | declare(strict_types=1); |
||||||||
4 | |||||||||
5 | namespace Odiseo\SyliusReportPlugin\Response; |
||||||||
6 | |||||||||
7 | use Odiseo\SyliusReportPlugin\DataFetcher\Data; |
||||||||
8 | use Symfony\Component\HttpFoundation\Response; |
||||||||
9 | |||||||||
10 | class CsvResponse extends Response |
||||||||
11 | { |
||||||||
12 | |||||||||
13 | protected ?string $data = null; |
||||||||
14 | protected string $filename = 'export.csv'; |
||||||||
15 | |||||||||
16 | public function __construct(?Data $data = null, int $status = 200, array $headers = []) |
||||||||
17 | { |
||||||||
18 | parent::__construct('', $status, $headers); |
||||||||
19 | $this->setData($data); |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
20 | } |
||||||||
21 | |||||||||
22 | public static function create(?Data $data = null, int $status = 200, array $headers = []) |
||||||||
23 | { |
||||||||
24 | return new static($labels, $data, $status, $headers); |
||||||||
0 ignored issues
–
show
The call to
Odiseo\SyliusReportPlugi...Response::__construct() has too many arguments starting with $headers .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() $status of type integer is incompatible with the type array expected by parameter $headers of Odiseo\SyliusReportPlugi...Response::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() $data of type Odiseo\SyliusReportPlugin\DataFetcher\Data|null is incompatible with the type integer expected by parameter $status of Odiseo\SyliusReportPlugi...Response::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() Comprehensibility
Best Practice
introduced
by
|
|||||||||
25 | } |
||||||||
26 | |||||||||
27 | public function setData(Data $data): self |
||||||||
28 | { |
||||||||
29 | $output = fopen('php://temp', 'r+'); |
||||||||
30 | |||||||||
31 | if ($output === false) { |
||||||||
32 | throw new \RuntimeException('Could not create a buffer for CSV output.'); |
||||||||
33 | } |
||||||||
34 | |||||||||
35 | foreach ($data->getData() as $row) { |
||||||||
36 | fputcsv($output, $row); |
||||||||
37 | } |
||||||||
38 | |||||||||
39 | rewind($output); |
||||||||
40 | $this->data = ''; |
||||||||
41 | |||||||||
42 | while ($line = fgets($output)) { |
||||||||
43 | $this->data .= $line; |
||||||||
44 | } |
||||||||
45 | |||||||||
46 | $this->data .= fgets($output); |
||||||||
47 | |||||||||
48 | return $this->update(); |
||||||||
49 | } |
||||||||
50 | |||||||||
51 | public function getFilename(): string |
||||||||
52 | { |
||||||||
53 | return $this->filename; |
||||||||
54 | } |
||||||||
55 | |||||||||
56 | public function setFilename(string $filename): self |
||||||||
57 | { |
||||||||
58 | $this->filename = $filename; |
||||||||
59 | |||||||||
60 | return $this->update(); |
||||||||
61 | } |
||||||||
62 | |||||||||
63 | protected function update(): self |
||||||||
64 | { |
||||||||
65 | $this->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $this->filename)); |
||||||||
66 | if (!$this->headers->has('Content-Type')) { |
||||||||
67 | $this->headers->set('Content-Type', 'text/csv'); |
||||||||
68 | } |
||||||||
69 | |||||||||
70 | return $this->setContent($this->data); |
||||||||
71 | } |
||||||||
72 | } |
||||||||
73 |