[export form] decouple data from PickCenter form

This commit is contained in:
Julien Fastré 2023-06-01 14:02:48 +02:00
parent 77f8cf0e1a
commit d1e1b1c4ce
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 68 additions and 24 deletions

View File

@ -13,6 +13,7 @@ namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Entity\SavedExport; use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\ExportFormHelper;
use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Form\SavedExportType; use Chill\MainBundle\Form\SavedExportType;
use Chill\MainBundle\Form\Type\Export\ExportType; use Chill\MainBundle\Form\Type\Export\ExportType;
@ -86,7 +87,8 @@ class ExportController extends AbstractController
LoggerInterface $logger, LoggerInterface $logger,
SessionInterface $session, SessionInterface $session,
TranslatorInterface $translator, TranslatorInterface $translator,
EntityManagerInterface $entityManager EntityManagerInterface $entityManager,
private readonly ExportFormHelper $exportFormHelper,
) { ) {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->redis = $chillRedis; $this->redis = $chillRedis;
@ -293,10 +295,15 @@ class ExportController extends AbstractController
$isGenerate = strpos($step, 'generate_') === 0; $isGenerate = strpos($step, 'generate_') === 0;
$builder = $this->formFactory $builder = $this->formFactory
->createNamedBuilder(null, FormType::class, [], [ ->createNamedBuilder(
'method' => $isGenerate ? 'GET' : 'POST', null,
'csrf_protection' => $isGenerate ? false : true, FormType::class,
]); $this->exportFormHelper->getDefaultData($step, $exportManager->getExport($alias)),
[
'method' => $isGenerate ? 'GET' : 'POST',
'csrf_protection' => $isGenerate ? false : true,
]
);
// TODO: add a condition to be able to select a regroupment of centers? // TODO: add a condition to be able to select a regroupment of centers?
@ -341,7 +348,7 @@ class ExportController extends AbstractController
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
*/ */
protected function exportFormStep(Request $request, $export, $alias) private function exportFormStep(Request $request, $export, $alias)
{ {
$exportManager = $this->exportManager; $exportManager = $this->exportManager;
@ -405,7 +412,7 @@ class ExportController extends AbstractController
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
*/ */
protected function formatterFormStep(Request $request, $export, $alias) private function formatterFormStep(Request $request, $export, $alias)
{ {
// check we have data from the previous step (export step) // check we have data from the previous step (export step)
$data = $this->session->get('export_step', null); $data = $this->session->get('export_step', null);
@ -462,7 +469,7 @@ class ExportController extends AbstractController
* *
* @return \Symfony\Component\HttpFoundation\RedirectResponse * @return \Symfony\Component\HttpFoundation\RedirectResponse
*/ */
protected function forwardToGenerate(Request $request, $export, $alias) private function forwardToGenerate(Request $request, $export, $alias)
{ {
$dataCenters = $this->session->get('centers_step_raw', null); $dataCenters = $this->session->get('centers_step_raw', null);
$dataFormatter = $this->session->get('formatter_step_raw', null); $dataFormatter = $this->session->get('formatter_step_raw', null);
@ -494,7 +501,7 @@ class ExportController extends AbstractController
return $this->redirectToRoute('chill_main_export_download', ['key' => $key, 'alias' => $alias]); return $this->redirectToRoute('chill_main_export_download', ['key' => $key, 'alias' => $alias]);
} }
protected function rebuildData($key) private function rebuildData($key)
{ {
$rawData = $this->rebuildRawData($key); $rawData = $this->rebuildRawData($key);
@ -527,7 +534,7 @@ class ExportController extends AbstractController
* *
* @return Response * @return Response
*/ */
protected function selectCentersStep(Request $request, $export, $alias) private function selectCentersStep(Request $request, $export, $alias)
{ {
/** @var \Chill\MainBundle\Export\ExportManager $exportManager */ /** @var \Chill\MainBundle\Export\ExportManager $exportManager */
$exportManager = $this->exportManager; $exportManager = $this->exportManager;

View File

@ -0,0 +1,37 @@
<?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\Export;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
final readonly class ExportFormHelper
{
public function __construct(
private ExportManager $exportManager,
private AuthorizationHelperForCurrentUserInterface $authorizationHelper,
) {
}
/**
* @param list<"centers"> $steps
*/
public function getDefaultData(string $step, ExportInterface $export): array
{
$data = [];
if ($step === 'centers') {
$data['centers'] = $this->authorizationHelper->getReachableCenters($export->requiredRole());
}
return $data;
}
}

View File

@ -22,7 +22,10 @@ use function count;
class ExportPickCenterDataMapper implements DataMapperInterface class ExportPickCenterDataMapper implements DataMapperInterface
{ {
protected RegroupmentRepository $regroupmentRepository; public function __construct(
private RegroupmentRepository $regroupmentRepository,
) {
}
public function mapDataToForms($data, $forms): void public function mapDataToForms($data, $forms): void
{ {
@ -37,15 +40,15 @@ class ExportPickCenterDataMapper implements DataMapperInterface
foreach ($this->regroupmentRepository->findAll() as $regroupment) { foreach ($this->regroupmentRepository->findAll() as $regroupment) {
/** @phpstan-ignore-next-line */ /** @phpstan-ignore-next-line */
[$contained, $notContained] = $regroupment->getCenters()->partition(static fn (Center $center): bool => false); [$contained, $notContained] = $regroupment->getCenters()->partition(static fn (int $id, Center $center): bool => false);
if (0 === count($notContained)) { if (0 === count($notContained)) {
$pickedRegroupment[] = $regroupment; $pickedRegroupment[] = $regroupment;
} }
} }
$form['regroupment']->setData($pickedRegroupment); $form['regroupment']->setData([]);
$form['centers']->setData($data); $form['center']->setData($data);
} }
public function mapFormsToData($forms, &$data): void public function mapFormsToData($forms, &$data): void

View File

@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\Regroupment;
use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper; use Chill\MainBundle\Form\DataMapper\ExportPickCenterDataMapper;
use Chill\MainBundle\Repository\RegroupmentRepository; use Chill\MainBundle\Repository\RegroupmentRepository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -33,22 +34,18 @@ final class PickCenterType extends AbstractType
{ {
public const CENTERS_IDENTIFIERS = 'c'; public const CENTERS_IDENTIFIERS = 'c';
private AuthorizationHelperInterface $authorizationHelper; private AuthorizationHelperForCurrentUserInterface $authorizationHelper;
private ExportManager $exportManager; private ExportManager $exportManager;
private RegroupmentRepository $regroupmentRepository; private RegroupmentRepository $regroupmentRepository;
private UserInterface $user;
public function __construct( public function __construct(
TokenStorageInterface $tokenStorage,
ExportManager $exportManager, ExportManager $exportManager,
RegroupmentRepository $regroupmentRepository, RegroupmentRepository $regroupmentRepository,
AuthorizationHelperInterface $authorizationHelper AuthorizationHelperForCurrentUserInterface $authorizationHelper
) { ) {
$this->exportManager = $exportManager; $this->exportManager = $exportManager;
$this->user = $tokenStorage->getToken()->getUser();
$this->authorizationHelper = $authorizationHelper; $this->authorizationHelper = $authorizationHelper;
$this->regroupmentRepository = $regroupmentRepository; $this->regroupmentRepository = $regroupmentRepository;
} }
@ -57,18 +54,16 @@ final class PickCenterType extends AbstractType
{ {
$export = $this->exportManager->getExport($options['export_alias']); $export = $this->exportManager->getExport($options['export_alias']);
$centers = $this->authorizationHelper->getReachableCenters( $centers = $this->authorizationHelper->getReachableCenters(
$this->user,
$export->requiredRole() $export->requiredRole()
); );
$builder->add('center', EntityType::class, [ $builder->add('center', EntityType::class, [
'class' => Center::class, 'class' => Center::class,
'label' => 'center',
'choices' => $centers, 'choices' => $centers,
'label' => 'center',
'multiple' => true, 'multiple' => true,
'expanded' => true, 'expanded' => true,
'choice_label' => static fn (Center $c) => $c->getName(), 'choice_label' => static fn (Center $c) => $c->getName(),
'data' => $centers,
]); ]);
if (count($this->regroupmentRepository->findAllActive()) > 0) { if (count($this->regroupmentRepository->findAllActive()) > 0) {
@ -82,7 +77,7 @@ final class PickCenterType extends AbstractType
]); ]);
} }
$builder->setDataMapper(new ExportPickCenterDataMapper()); $builder->setDataMapper(new ExportPickCenterDataMapper($this->regroupmentRepository));
} }
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)

View File

@ -6,6 +6,8 @@ services:
Chill\MainBundle\Export\Helper\: Chill\MainBundle\Export\Helper\:
resource: '../../Export/Helper' resource: '../../Export/Helper'
Chill\MainBundle\Export\ExportFormHelper: ~
chill.main.export_element_validator: chill.main.export_element_validator:
class: Chill\MainBundle\Validator\Constraints\Export\ExportElementConstraintValidator class: Chill\MainBundle\Validator\Constraints\Export\ExportElementConstraintValidator
tags: tags: