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

View File

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

View File

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

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);
}
}