|
12 | 12 | namespace App\Twig\Components;
|
13 | 13 |
|
14 | 14 | use App\Service\DocumentStorage;
|
15 |
| -use DateTimeImmutable; |
16 |
| -use SplTempFileObject; |
17 | 15 | use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
18 | 16 | use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
|
19 | 17 | use Symfony\UX\LiveComponent\Attribute\LiveAction;
|
|
27 | 25 | final class DownloadFiles
|
28 | 26 | {
|
29 | 27 | use DefaultActionTrait;
|
30 |
| - |
| 28 | + |
31 | 29 | #[LiveProp(writable: true)]
|
32 | 30 | public int $year = 2025;
|
33 |
| - |
| 31 | + |
34 | 32 | public function __construct(
|
35 | 33 | private readonly DocumentStorage $documentStorage,
|
36 | 34 | ) {
|
37 | 35 | }
|
38 |
| - |
| 36 | + |
39 | 37 | #[LiveAction]
|
40 | 38 | public function download(): BinaryFileResponse
|
41 | 39 | {
|
42 | 40 | $file = $this->documentStorage->getFile('demos/empty.html');
|
43 |
| - |
| 41 | + |
44 | 42 | return new LiveDownloadResponse($file);
|
45 | 43 | }
|
46 |
| - |
| 44 | + |
47 | 45 | #[LiveAction]
|
48 | 46 | public function generate(#[LiveArg] string $format): BinaryFileResponse
|
49 | 47 | {
|
50 |
| - $report = match($format) { |
| 48 | + $report = match ($format) { |
51 | 49 | 'csv' => $this->generateCsvReport($this->year),
|
52 | 50 | 'json' => $this->generateJsonReport($this->year),
|
53 | 51 | 'md' => $this->generateMarkdownReport($this->year),
|
54 | 52 | default => throw new \InvalidArgumentException('Invalid format provided'),
|
55 | 53 | };
|
56 |
| - |
57 |
| - $file = new SplTempFileObject(); |
| 54 | + |
| 55 | + $file = new \SplTempFileObject(); |
58 | 56 | $file->fwrite($report);
|
59 |
| - |
| 57 | + |
60 | 58 | return new LiveDownloadResponse($file, 'report.'.$format);
|
61 | 59 | }
|
62 |
| - |
| 60 | + |
63 | 61 | private function generateCsvReport(int $year): string
|
64 | 62 | {
|
65 |
| - $file = new SplTempFileObject(); |
| 63 | + $file = new \SplTempFileObject(); |
66 | 64 | // $file->fputcsv(['Month', 'Number', 'Name', 'Number of days']);
|
67 | 65 | foreach ($this->getReportData($year) as $row) {
|
68 | 66 | $file->fputcsv($row);
|
69 | 67 | }
|
70 |
| - |
| 68 | + |
71 | 69 | return $file->fread($file->ftell());
|
72 | 70 | }
|
73 |
| - |
| 71 | + |
74 | 72 | private function generateMarkdownReport(int $year): string
|
75 | 73 | {
|
76 | 74 | $rows = iterator_to_array($this->getReportData($year));
|
77 |
| - |
| 75 | + |
78 | 76 | foreach ($rows as $key => $row) {
|
79 | 77 | $rows[$key] = '|'.implode('|', $row).'|';
|
80 | 78 | }
|
81 |
| - |
| 79 | + |
82 | 80 | return implode("\n", $rows);
|
83 | 81 | }
|
84 | 82 |
|
85 | 83 | private function generateJsonReport(int $year): string
|
86 | 84 | {
|
87 | 85 | $rows = iterator_to_array($this->getReportData($year));
|
88 |
| - |
89 |
| - return \json_encode($rows, JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR); |
| 86 | + |
| 87 | + return json_encode($rows, \JSON_FORCE_OBJECT | \JSON_THROW_ON_ERROR); |
90 | 88 | }
|
91 | 89 |
|
92 | 90 | /**
|
93 | 91 | * @param int<2000,2025> $year The year to generate the report for (2000-2025)
|
94 |
| - * |
| 92 | + * |
95 | 93 | * @return iterable<string, array{string, string}>
|
96 | 94 | */
|
97 | 95 | private function getReportData(int $year): iterable
|
98 | 96 | {
|
99 | 97 | foreach (range(1, 12) as $month) {
|
100 |
| - $startDate = DateTimeImmutable::createFromFormat('Y', $year)->setDate($year, $month, 1); |
| 98 | + $startDate = \DateTimeImmutable::createFromFormat('Y', $year)->setDate($year, $month, 1); |
101 | 99 | $endDate = $startDate->modify('last day of this month');
|
102 | 100 | yield $month => [
|
103 | 101 | 'name' => $startDate->format('F'),
|
|
0 commit comments