mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 06:26:15 +00:00
Refactor SpreadsheetListFormatter
to streamline response generation and improve type safety.
This commit is contained in:
parent
7533cb3a71
commit
d4704dc2ef
@ -13,6 +13,7 @@ namespace Chill\MainBundle\Export\Formatter;
|
|||||||
|
|
||||||
use Chill\MainBundle\Export\ExportGenerationContext;
|
use Chill\MainBundle\Export\ExportGenerationContext;
|
||||||
use Chill\MainBundle\Export\ExportManagerAwareInterface;
|
use Chill\MainBundle\Export\ExportManagerAwareInterface;
|
||||||
|
use Chill\MainBundle\Export\FormattedExportGeneration;
|
||||||
use Chill\MainBundle\Export\FormatterInterface;
|
use Chill\MainBundle\Export\FormatterInterface;
|
||||||
use Chill\MainBundle\Export\Helper\ExportManagerAwareTrait;
|
use Chill\MainBundle\Export\Helper\ExportManagerAwareTrait;
|
||||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||||
@ -21,6 +22,7 @@ use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
@ -46,15 +48,7 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
|
|||||||
|
|
||||||
protected $result;
|
protected $result;
|
||||||
|
|
||||||
/**
|
public function __construct(private readonly TranslatorInterface $translator) {}
|
||||||
* @var TranslatorInterface
|
|
||||||
*/
|
|
||||||
protected $translator;
|
|
||||||
|
|
||||||
public function __construct(TranslatorInterface $translatorInterface)
|
|
||||||
{
|
|
||||||
$this->translator = $translatorInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* build a form, which will be used to collect data required for the execution
|
* 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(
|
public function buildForm(
|
||||||
FormBuilderInterface $builder,
|
FormBuilderInterface $builder,
|
||||||
$exportAlias,
|
string $exportAlias,
|
||||||
array $aggregatorAliases,
|
array $aggregatorAliases,
|
||||||
): void {
|
): void {
|
||||||
$builder
|
$builder
|
||||||
@ -113,26 +107,8 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
|
|||||||
return 'Spreadsheet list formatter (.xlsx, .ods)';
|
return 'Spreadsheet list formatter (.xlsx, .ods)';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function generate($result, $formatterData, string $exportAlias, array $exportData, array $filtersData, array $aggregatorsData, ExportGenerationContext $context): FormattedExportGeneration
|
||||||
* 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,
|
|
||||||
) {
|
|
||||||
$this->result = $result;
|
$this->result = $result;
|
||||||
$this->exportAlias = $exportAlias;
|
$this->exportAlias = $exportAlias;
|
||||||
$this->exportData = $exportData;
|
$this->exportData = $exportData;
|
||||||
@ -204,19 +180,45 @@ class SpreadsheetListFormatter implements FormatterInterface, ExportManagerAware
|
|||||||
throw new \OutOfBoundsException('The format '.$this->formatterData['format'].' is not supported');
|
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(), '');
|
$tempfile = \tempnam(\sys_get_temp_dir(), '');
|
||||||
$writer->save($tempfile);
|
$writer->save($tempfile);
|
||||||
|
|
||||||
$f = \fopen($tempfile, 'rb');
|
$generated = new FormattedExportGeneration(
|
||||||
$response->setContent(\stream_get_contents($f));
|
file_get_contents($tempfile),
|
||||||
fclose($f);
|
$contentType,
|
||||||
|
);
|
||||||
|
|
||||||
// remove the temp file from disk
|
// remove the temp file from disk
|
||||||
\unlink($tempfile);
|
\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;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user