Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AbstractGenerator | |
0.00% |
0 / 23 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
generateResponse | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
getDate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\Reporting\Generator\Csv; |
25 | |
26 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
27 | use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
28 | |
29 | abstract class AbstractGenerator implements GeneratorInterface |
30 | { |
31 | public function generateResponse(string $documentName): BinaryFileResponse |
32 | { |
33 | $currentDate = (new \DateTimeImmutable())->format('Ymd'); |
34 | $fileName = "{$currentDate}-{$documentName}.csv"; |
35 | $tempFile = \tempnam(\sys_get_temp_dir(), $fileName); |
36 | |
37 | $outputBuffer = fopen($tempFile, 'w'); |
38 | fprintf($outputBuffer, chr(0xEF) . chr(0xBB) . chr(0xBF)); // need this line to support accent on Excel |
39 | foreach ($this->initializeExtract() as $row) { |
40 | fputcsv($outputBuffer, $row, ';', '"'); |
41 | } |
42 | fclose($outputBuffer); |
43 | |
44 | // Create response and return it |
45 | $response = new BinaryFileResponse($tempFile); |
46 | $response->headers->set('Content-Encoding', 'UTF-8'); |
47 | $response->headers->set('Content-Type', 'text/csv; charset=utf-8'); |
48 | $response->setContentDisposition( |
49 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
50 | $fileName |
51 | ); |
52 | |
53 | return $response; |
54 | } |
55 | |
56 | /** |
57 | * Format a date for Word document. |
58 | * |
59 | * @param \DateTimeInterface|null $dateTime The date to parse |
60 | * |
61 | * @throws \Exception |
62 | * |
63 | * @return string The parsed date with the good timezone |
64 | */ |
65 | protected function getDate(?\DateTimeInterface $dateTime, ?string $format = null): string |
66 | { |
67 | if (\is_null($dateTime)) { |
68 | return ''; |
69 | } |
70 | |
71 | $format = $format ?? self::DATE_TIME_FORMAT; |
72 | |
73 | $parsedDateTime = new \DateTimeImmutable(); |
74 | $parsedDateTime = $parsedDateTime->setTimestamp($dateTime->getTimestamp()); |
75 | $parsedDateTime = $parsedDateTime->setTimezone(new \DateTimeZone(self::DATE_TIME_ZONE)); |
76 | |
77 | return $parsedDateTime->format($format); |
78 | } |
79 | } |