|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace App\Twig\Components; |
| 13 | + |
| 14 | +use App\Service\DocumentStorage; |
| 15 | +use DateTimeImmutable; |
| 16 | +use SplTempFileObject; |
| 17 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 18 | +use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
| 19 | +use Symfony\UX\LiveComponent\Attribute\LiveAction; |
| 20 | +use Symfony\UX\LiveComponent\Attribute\LiveArg; |
| 21 | +use Symfony\UX\LiveComponent\Attribute\LiveProp; |
| 22 | +use Symfony\UX\LiveComponent\DefaultActionTrait; |
| 23 | +use Symfony\UX\LiveComponent\LiveDownloadResponse; |
| 24 | + |
| 25 | +#[AsLiveComponent] |
| 26 | +// #[AsTaggedItem('controller.service_arguments')] |
| 27 | +final class DownloadFiles |
| 28 | +{ |
| 29 | + use DefaultActionTrait; |
| 30 | + |
| 31 | + #[LiveProp(writable: true)] |
| 32 | + public int $year = 2025; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + private readonly DocumentStorage $documentStorage, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + #[LiveAction] |
| 40 | + public function download(): BinaryFileResponse |
| 41 | + { |
| 42 | + $file = $this->documentStorage->getFile('demos/empty.html'); |
| 43 | + |
| 44 | + return new LiveDownloadResponse($file); |
| 45 | + } |
| 46 | + |
| 47 | + #[LiveAction] |
| 48 | + public function generate(#[LiveArg] string $format): BinaryFileResponse |
| 49 | + { |
| 50 | + $report = match($format) { |
| 51 | + 'csv' => $this->generateCsvReport($this->year), |
| 52 | + 'json' => $this->generateJsonReport($this->year), |
| 53 | + 'md' => $this->generateMarkdownReport($this->year), |
| 54 | + default => throw new \InvalidArgumentException('Invalid format provided'), |
| 55 | + }; |
| 56 | + |
| 57 | + $file = new SplTempFileObject(); |
| 58 | + $file->fwrite($report); |
| 59 | + |
| 60 | + return new LiveDownloadResponse($file, 'report.'.$format); |
| 61 | + } |
| 62 | + |
| 63 | + private function generateCsvReport(int $year): string |
| 64 | + { |
| 65 | + $file = new SplTempFileObject(); |
| 66 | + // $file->fputcsv(['Month', 'Number', 'Name', 'Number of days']); |
| 67 | + foreach ($this->getReportData($year) as $row) { |
| 68 | + $file->fputcsv($row); |
| 69 | + } |
| 70 | + |
| 71 | + return $file->fread($file->ftell()); |
| 72 | + } |
| 73 | + |
| 74 | + private function generateMarkdownReport(int $year): string |
| 75 | + { |
| 76 | + $rows = iterator_to_array($this->getReportData($year)); |
| 77 | + |
| 78 | + foreach ($rows as $key => $row) { |
| 79 | + $rows[$key] = '|'.implode('|', $row).'|'; |
| 80 | + } |
| 81 | + |
| 82 | + return implode("\n", $rows); |
| 83 | + } |
| 84 | + |
| 85 | + private function generateJsonReport(int $year): string |
| 86 | + { |
| 87 | + $rows = iterator_to_array($this->getReportData($year)); |
| 88 | + |
| 89 | + return \json_encode($rows, JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param int<2000,2025> $year The year to generate the report for (2000-2025) |
| 94 | + * |
| 95 | + * @return iterable<string, array{string, string}> |
| 96 | + */ |
| 97 | + private function getReportData(int $year): iterable |
| 98 | + { |
| 99 | + foreach (range(1, 12) as $month) { |
| 100 | + $startDate = DateTimeImmutable::createFromFormat('Y', $year)->setDate($year, $month, 1); |
| 101 | + $endDate = $startDate->modify('last day of this month'); |
| 102 | + yield $month => [ |
| 103 | + 'name' => $startDate->format('F'), |
| 104 | + 'month' => $startDate->format('F'), |
| 105 | + 'number' => $startDate->format('Y-m'), |
| 106 | + 'nb_days' => $endDate->diff($startDate)->days, |
| 107 | + ]; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments