Get session from requestStack instead of injecting SessionInterface

This commit is contained in:
Julie Lenaerts 2025-05-26 14:19:01 +02:00
parent 6e5dbe4e58
commit 5cdfee40fb
3 changed files with 29 additions and 31 deletions

View File

@ -18,7 +18,7 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use TheNetworg\OAuth2\Client\Provider\Azure;
use TheNetworg\OAuth2\Client\Token\AccessToken;
@ -29,12 +29,12 @@ class OnBehalfOfUserTokenStorage
{
final public const MS_GRAPH_ACCESS_TOKEN = 'msgraph_access_token';
public function __construct(private readonly Azure $azure, private readonly SessionInterface $session) {}
public function __construct(private readonly Azure $azure, private readonly RequestStack $requestStack) {}
public function getToken(): AccessToken
{
/** @var ?AccessToken $token */
$token = $this->session->get(self::MS_GRAPH_ACCESS_TOKEN, null);
$token = $this->requestStack->getSession()->get(self::MS_GRAPH_ACCESS_TOKEN, null);
if (null === $token) {
throw new \LogicException('unexisting token');
@ -53,11 +53,11 @@ class OnBehalfOfUserTokenStorage
public function hasToken(): bool
{
return $this->session->has(self::MS_GRAPH_ACCESS_TOKEN);
return $this->requestStack->getSession()->has(self::MS_GRAPH_ACCESS_TOKEN);
}
public function setToken(AccessToken $token): void
{
$this->session->set(self::MS_GRAPH_ACCESS_TOKEN, $token);
$this->requestStack->getSession()->set(self::MS_GRAPH_ACCESS_TOKEN, $token);
}
}

View File

@ -34,8 +34,8 @@ use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\SecurityBundle\Security;
@ -54,7 +54,7 @@ class ExportController extends AbstractController
private readonly ExportManager $exportManager,
private readonly FormFactoryInterface $formFactory,
private readonly LoggerInterface $logger,
private readonly SessionInterface $session,
private readonly RequestStack $requestStack,
private readonly TranslatorInterface $translator,
private readonly EntityManagerInterface $entityManager,
private readonly ExportFormHelper $exportFormHelper,
@ -66,7 +66,7 @@ class ExportController extends AbstractController
}
#[Route(path: '/{_locale}/exports/download/{alias}', name: 'chill_main_export_download', methods: ['GET'])]
public function downloadResultAction(Request $request, mixed $alias): \Symfony\Component\HttpFoundation\Response
public function downloadResultAction(Request $request, mixed $alias): Response
{
/** @var ExportManager $exportManager */
$exportManager = $this->exportManager;
@ -327,7 +327,7 @@ class ExportController extends AbstractController
$exportManager = $this->exportManager;
// check we have data from the previous step (export step)
$data = $this->session->get('centers_step', []);
$data = $this->requestStack->getSession()->get('centers_step', []);
if (null === $data && true === $this->filterStatsByCenters) {
return $this->redirectToRoute('chill_main_export_new', [
@ -349,11 +349,11 @@ class ExportController extends AbstractController
// store data for reusing in next steps
$data = $form->getData();
$this->session->set(
$this->requestStack->getSession()->set(
'export_step_raw',
$request->request->all()
);
$this->session->set('export_step', $data);
$this->requestStack->getSession()->set('export_step', $data);
// redirect to next step
return $this->redirectToRoute('chill_main_export_new', [
@ -383,7 +383,7 @@ class ExportController extends AbstractController
private function formatterFormStep(Request $request, DirectExportInterface|ExportInterface $export, string $alias, ?SavedExport $savedExport = null): Response
{
// check we have data from the previous step (export step)
$data = $this->session->get('export_step', null);
$data = $this->requestStack->getSession()->get('export_step', null);
if (null === $data) {
return $this->redirectToRoute('chill_main_export_new', [
@ -399,8 +399,8 @@ class ExportController extends AbstractController
if ($form->isValid()) {
$dataFormatter = $form->getData();
$this->session->set('formatter_step', $dataFormatter);
$this->session->set(
$this->requestStack->getSession()->set('formatter_step', $dataFormatter);
$this->requestStack->getSession()->set(
'formatter_step_raw',
$request->request->all()
);
@ -431,14 +431,12 @@ class ExportController extends AbstractController
* The data from previous steps is removed from session.
*
* @param string $alias
*
* @return RedirectResponse
*/
private function forwardToGenerate(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport): \Symfony\Component\HttpFoundation\RedirectResponse
private function forwardToGenerate(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport): RedirectResponse
{
$dataCenters = $this->session->get('centers_step_raw', null);
$dataFormatter = $this->session->get('formatter_step_raw', null);
$dataExport = $this->session->get('export_step_raw', null);
$dataCenters = $this->requestStack->getSession()->get('centers_step_raw', null);
$dataFormatter = $this->requestStack->getSession()->get('formatter_step_raw', null);
$dataExport = $this->requestStack->getSession()->get('export_step_raw', null);
if (null === $dataFormatter && $export instanceof ExportInterface) {
return $this->redirectToRoute('chill_main_export_new', [
@ -460,10 +458,10 @@ class ExportController extends AbstractController
$this->redis->setEx($key, 3600, \serialize($parameters));
// remove data from session
$this->session->remove('export_step_raw');
$this->session->remove('export_step');
$this->session->remove('formatter_step_raw');
$this->session->remove('formatter_step');
$this->requestStack->getSession()->remove('export_step_raw');
$this->requestStack->getSession()->remove('export_step');
$this->requestStack->getSession()->remove('formatter_step_raw');
$this->requestStack->getSession()->remove('formatter_step');
return $this->redirectToRoute('chill_main_export_download', [
'key' => $key,
@ -509,7 +507,7 @@ class ExportController extends AbstractController
*
* @return Response
*/
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport = null): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
private function selectCentersStep(Request $request, DirectExportInterface|ExportInterface $export, $alias, ?SavedExport $savedExport = null): RedirectResponse|Response
{
if (!$this->filterStatsByCenters) {
return $this->redirectToRoute('chill_main_export_new', [
@ -544,11 +542,11 @@ class ExportController extends AbstractController
throw $this->createAccessDeniedException('you do not have access to this export for those centers');
}
$this->session->set(
$this->requestStack->getSession()->set(
'centers_step_raw',
$request->request->all()
);
$this->session->set('centers_step', $data);
$this->requestStack->getSession()->set('centers_step', $data);
return $this->redirectToRoute('chill_main_export_new', [
'step' => $this->getNextStep('centers', $export),

View File

@ -24,8 +24,8 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -34,7 +34,7 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class SavedExportController
{
public function __construct(private readonly \Twig\Environment $templating, private readonly EntityManagerInterface $entityManager, private readonly ExportManager $exportManager, private readonly FormFactoryInterface $formFactory, private readonly SavedExportRepositoryInterface $savedExportRepository, private readonly Security $security, private readonly SessionInterface $session, private readonly TranslatorInterface $translator, private readonly UrlGeneratorInterface $urlGenerator) {}
public function __construct(private readonly \Twig\Environment $templating, private readonly EntityManagerInterface $entityManager, private readonly ExportManager $exportManager, private readonly FormFactoryInterface $formFactory, private readonly SavedExportRepositoryInterface $savedExportRepository, private readonly Security $security, private readonly RequestStack $requestStack, private readonly TranslatorInterface $translator, private readonly UrlGeneratorInterface $urlGenerator) {}
#[Route(path: '/{_locale}/exports/saved/{id}/delete', name: 'chill_main_export_saved_delete')]
public function delete(SavedExport $savedExport, Request $request): Response
@ -51,7 +51,7 @@ class SavedExportController
$this->entityManager->remove($savedExport);
$this->entityManager->flush();
$this->session->getFlashBag()->add('success', $this->translator->trans('saved_export.Export is deleted'));
$this->requestStack->getSession()->getFlashBag()->add('success', $this->translator->trans('saved_export.Export is deleted'));
return new RedirectResponse(
$this->urlGenerator->generate('chill_main_export_saved_list_my')
@ -83,7 +83,7 @@ class SavedExportController
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->flush();
$this->session->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
$this->requestStack->getSession()->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
return new RedirectResponse(
$this->urlGenerator->generate('chill_main_export_saved_list_my')