Refactor SpreadsheetListFormatter to streamline response generation and improve type safety.

This commit is contained in:
Julien Fastré 2025-06-27 15:20:13 +02:00
parent 7533cb3a71
commit d4704dc2ef
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -13,6 +13,7 @@ namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportGenerationContext;
use Chill\MainBundle\Export\ExportManagerAwareInterface;
use Chill\MainBundle\Export\FormattedExportGeneration;
use Chill\MainBundle\Export\FormatterInterface;
use Chill\MainBundle\Export\Helper\ExportManagerAwareTrait;
use PhpOffice\PhpSpreadsheet\Shared\Date;
@ -21,6 +22,7 @@ use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -46,15 +48,7 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
protected $result;
/**
* @var TranslatorInterface
*/
protected $translator;
public function __construct(TranslatorInterface $translatorInterface)
{
$this->translator = $translatorInterface;
}
public function __construct(private readonly TranslatorInterface $translator) {}
/**
* build a form, which will be used to collect data required for the execution
@ -66,7 +60,7 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
*/
public function buildForm(
FormBuilderInterface $builder,
$exportAlias,
string $exportAlias,
array $aggregatorAliases,
): void {
$builder
@ -113,26 +107,8 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
return 'Spreadsheet list formatter (.xlsx, .ods)';
}
/**
* Generate a response from the data collected on differents ExportElementInterface.
*
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
*
* @return Response The response to be shown
*/
public function getResponse(
$result,
$formatterData,
$exportAlias,
array $exportData,
array $filtersData,
array $aggregatorsData,
ExportGenerationContext $context,
) {
public function generate($result, $formatterData, string $exportAlias, array $exportData, array $filtersData, array $aggregatorsData, ExportGenerationContext $context): FormattedExportGeneration
{
$this->result = $result;
$this->exportAlias = $exportAlias;
$this->exportData = $exportData;
@ -204,19 +180,45 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
throw new \OutOfBoundsException('The format '.$this->formatterData['format'].' is not supported');
}
$response = new Response();
$response->headers->set('content-type', $contentType);
$tempfile = \tempnam(\sys_get_temp_dir(), '');
$writer->save($tempfile);
$f = \fopen($tempfile, 'rb');
$response->setContent(\stream_get_contents($f));
fclose($f);
$generated = new FormattedExportGeneration(
file_get_contents($tempfile),
$contentType,
);
// remove the temp file from disk
\unlink($tempfile);
return $generated;
}
/**
* Generate a response from the data collected on differents ExportElementInterface.
*
* @param mixed[] $result The result, as given by the ExportInterface
* @param mixed[] $formatterData collected from the current form
* @param string $exportAlias the id of the current export
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
*
* @return Response The response to be shown
*/
public function getResponse(
$result,
$formatterData,
$exportAlias,
array $exportData,
array $filtersData,
array $aggregatorsData,
ExportGenerationContext $context,
) {
$generated = $this->generate($result, $formatterData, $exportAlias, $exportData, $filtersData, $aggregatorsData, $context);
$response = new BinaryFileResponse($generated->content);
$response->headers->set('Content-Type', $generated->contentType);
return $response;
}