Refactor export handling to support ExportGeneration and SavedExport

Unified handling of ExportGeneration and SavedExport entities by introducing the SavedExportOrExportGenerationRepository. Simplified form data conversion using ExportConfigNormalizer, removing redundant FormBuilder logic. Adjusted dependent methods and controllers to accommodate these changes.
This commit is contained in:
Julien Fastré 2025-03-29 11:04:22 +01:00
parent 180437f637
commit 1375a41de2
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 59 additions and 56 deletions

View File

@ -26,6 +26,7 @@ use Chill\MainBundle\Form\Type\Export\FormatterType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Chill\MainBundle\Messenger\Stamp\AuthenticationStamp;
use Chill\MainBundle\Redis\ChillRedis;
use Chill\MainBundle\Repository\SavedExportOrExportGenerationRepository;
use Chill\MainBundle\Repository\SavedExportRepositoryInterface;
use Chill\MainBundle\Security\Authorization\SavedExportVoter;
use Doctrine\ORM\EntityManagerInterface;
@ -71,6 +72,7 @@ class ExportController extends AbstractController
private readonly MessageBusInterface $messageBus,
private readonly ClockInterface $clock,
private readonly ExportConfigNormalizer $exportConfigNormalizer,
private readonly SavedExportOrExportGenerationRepository $savedExportOrExportGenerationRepository,
) {
$this->filterStatsByCenters = $parameterBag->get('chill_main')['acl']['filter_stats_by_center'];
}
@ -586,7 +588,7 @@ class ExportController extends AbstractController
*
* @return Response
*/
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport = null)
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ExportGeneration|SavedExport|null $savedExport = null)
{
if (!$this->filterStatsByCenters) {
return $this->redirectToRoute('chill_main_export_new', [
@ -737,11 +739,11 @@ class ExportController extends AbstractController
return $rawData;
}
private function getSavedExportFromRequest(Request $request): ?SavedExport
private function getSavedExportFromRequest(Request $request): SavedExport|ExportGeneration|null
{
$savedExport = match ($savedExportId = $request->query->get('from_saved', '')) {
'' => null,
default => $this->savedExportRepository->find($savedExportId),
default => $this->savedExportOrExportGenerationRepository->findById($savedExportId),
};
if (null !== $savedExport && !$this->security->isGranted(SavedExportVoter::EDIT, $savedExport)) {

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Chill\MainBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Export\ExportManager;
use Symfony\Component\HttpFoundation\JsonResponse;

View File

@ -12,12 +12,10 @@ declare(strict_types=1);
namespace Chill\MainBundle\Export;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Form\Type\Export\FilterType;
use Chill\MainBundle\Form\Type\Export\FormatterType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
final readonly class ExportFormHelper
@ -26,6 +24,7 @@ final readonly class ExportFormHelper
private AuthorizationHelperForCurrentUserInterface $authorizationHelper,
private ExportManager $exportManager,
private FormFactoryInterface $formFactory,
private ExportConfigNormalizer $configNormalizer,
) {}
public function getDefaultData(string $step, DirectExportInterface|ExportInterface $export, array $options = []): array
@ -91,7 +90,7 @@ final readonly class ExportFormHelper
}
public function savedExportDataToFormData(
ExportGeneration $savedExport,
ExportGeneration|SavedExport $savedExport,
string $step,
array $formOptions = [],
): array {
@ -104,67 +103,38 @@ final readonly class ExportFormHelper
}
private function savedExportDataToFormDataStepCenter(
ExportGeneration $savedExport,
ExportGeneration|SavedExport $savedExport,
): array {
$builder = $this->formFactory
->createBuilder(
FormType::class,
[],
[
'csrf_protection' => false,
]
);
$builder->add('centers', PickCenterType::class, [
'export_alias' => $savedExport->getExportAlias(),
]);
$form = $builder->getForm();
$form->submit($savedExport->getOptions()['centers']);
return $form->getData();
return [
'centers' => $this->configNormalizer->denormalizeConfig($savedExport->getExportAlias(), $savedExport->getOptions(), true)['centers'],
];
}
private function savedExportDataToFormDataStepExport(
ExportGeneration $savedExport,
ExportGeneration|SavedExport $savedExport,
array $formOptions,
): array {
$builder = $this->formFactory
->createBuilder(
FormType::class,
[],
[
'csrf_protection' => false,
]
);
$data = $this->configNormalizer->denormalizeConfig($savedExport->getExportAlias(), $savedExport->getOptions(), true);
$builder->add('export', ExportType::class, [
'export_alias' => $savedExport->getExportAlias(), ...$formOptions,
]);
$form = $builder->getForm();
$form->submit($savedExport->getOptions()['export']);
return $form->getData();
return [
'export' => [
'export' => $data['export'],
'filters' => $data['filters'],
'pick_formatter' => ['alias' => $data['pick_formatter']],
'aggregators' => $data['aggregators'],
],
];
}
private function savedExportDataToFormDataStepFormatter(
ExportGeneration $savedExport,
ExportGeneration|SavedExport $savedExport,
array $formOptions,
): array {
$builder = $this->formFactory
->createBuilder(
FormType::class,
[],
[
'csrf_protection' => false,
]
);
$data = $this->configNormalizer->denormalizeConfig($savedExport->getExportAlias(), $savedExport->getOptions(), true);
$builder->add('formatter', FormatterType::class, [
'export_alias' => $savedExport->getExportAlias(), ...$formOptions,
]);
$form = $builder->getForm();
$form->submit($savedExport->getOptions()['formatter']);
return $form->getData();
return [
'formatter' => $data['formatter'],
];
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Entity\SavedExport;
readonly class SavedExportOrExportGenerationRepository
{
public function __construct(
private SavedExportRepositoryInterface $savedExportRepository,
private ExportGenerationRepository $exportGenerationRepository,
) {}
public function findById(string $uuid): SavedExport|ExportGeneration|null
{
if (null !== $savedExport = $this->savedExportRepository->find($uuid)) {
return $savedExport;
}
return $this->exportGenerationRepository->find($uuid);
}
}