Skip to content

Commit

Permalink
Fix session on controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 14, 2023
1 parent cdb7af7 commit 1c60adf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 43 deletions.
16 changes: 10 additions & 6 deletions src/Controller/Action/AddGiftCardToOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
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\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
use Webmozart\Assert\Assert;
Expand All @@ -28,27 +29,27 @@ final class AddGiftCardToOrderAction

private CartContextInterface $cartContext;

private FlashBagInterface $flashBag;

private GiftCardApplicatorInterface $giftCardApplicator;

private RedirectUrlResolverInterface $redirectRouteResolver;

private ?Environment $twig;

private RequestStack $requestStack;

public function __construct(
ViewHandlerInterface $viewHandler,
FormFactoryInterface $formFactory,
CartContextInterface $cartContext,
FlashBagInterface $flashBag,
RequestStack $requestStack,
GiftCardApplicatorInterface $giftCardApplicator,
RedirectUrlResolverInterface $redirectRouteResolver,
Environment $twig = null,
) {
$this->viewHandler = $viewHandler;
$this->formFactory = $formFactory;
$this->cartContext = $cartContext;
$this->flashBag = $flashBag;
$this->requestStack = $requestStack;
$this->giftCardApplicator = $giftCardApplicator;
$this->redirectRouteResolver = $redirectRouteResolver;
$this->twig = $twig;
Expand All @@ -72,7 +73,10 @@ public function __invoke(Request $request): Response
Assert::notNull($giftCard);
$this->giftCardApplicator->apply($order, $giftCard);

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');
$session = $this->requestStack->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('success', 'setono_sylius_gift_card.gift_card_added');
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
}
Expand Down
16 changes: 10 additions & 6 deletions src/Controller/Action/DownloadGiftCardPdfAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Setono\SyliusGiftCardPlugin\Security\GiftCardVoter;
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\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;
Expand All @@ -26,8 +27,6 @@ final class DownloadGiftCardPdfAction

private AuthorizationCheckerInterface $authChecker;

private FlashBagInterface $flashBag;

private GiftCardChannelConfigurationProviderInterface $configurationProvider;

private Environment $twig;
Expand All @@ -36,22 +35,24 @@ final class DownloadGiftCardPdfAction

private UrlGeneratorInterface $urlGenerator;

private RequestStack $requestStack;

public function __construct(
GiftCardRepositoryInterface $giftCardRepository,
AuthorizationCheckerInterface $authChecker,
FlashBagInterface $flashBag,
RequestStack $requestStack,
GiftCardChannelConfigurationProviderInterface $configurationProvider,
Environment $twig,
Pdf $snappy,
UrlGeneratorInterface $urlGenerator,
) {
$this->giftCardRepository = $giftCardRepository;
$this->authChecker = $authChecker;
$this->flashBag = $flashBag;
$this->configurationProvider = $configurationProvider;
$this->twig = $twig;
$this->snappy = $snappy;
$this->urlGenerator = $urlGenerator;
$this->requestStack = $requestStack;
}

public function __invoke(Request $request, int $id): Response
Expand Down Expand Up @@ -86,7 +87,10 @@ public function __invoke(Request $request, int $id): Response

private function sendErrorResponse(string $redirectUrl, string $message): RedirectResponse
{
$this->flashBag->add('error', $message);
$session = $this->requestStack->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('error', $message);
}

return new RedirectResponse($redirectUrl);
}
Expand Down
16 changes: 10 additions & 6 deletions src/Controller/Action/RemoveGiftCardFromOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@
use Sylius\Component\Order\Context\CartContextInterface;
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\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Webmozart\Assert\Assert;

final class RemoveGiftCardFromOrderAction
{
private CartContextInterface $cartContext;

private FlashBagInterface $flashBag;

private GiftCardApplicatorInterface $giftCardApplicator;

private RedirectUrlResolverInterface $redirectRouteResolver;

private RequestStack $requestStack;

public function __construct(
CartContextInterface $cartContext,
FlashBagInterface $flashBag,
RequestStack $requestStack,
GiftCardApplicatorInterface $giftCardApplicator,
RedirectUrlResolverInterface $redirectRouteResolver,
) {
$this->cartContext = $cartContext;
$this->flashBag = $flashBag;
$this->giftCardApplicator = $giftCardApplicator;
$this->redirectRouteResolver = $redirectRouteResolver;
$this->requestStack = $requestStack;
}

public function __invoke(Request $request): Response
Expand All @@ -44,7 +45,10 @@ public function __invoke(Request $request): Response

$this->giftCardApplicator->remove($order, $request->attributes->get('giftCard'));

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_removed');
$session = $this->requestStack->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('success', 'setono_sylius_gift_card.gift_card_removed');
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
}
Expand Down
53 changes: 32 additions & 21 deletions src/Controller/Action/ResendGiftCardEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use Sylius\Component\Core\Model\CustomerInterface;
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\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class ResendGiftCardEmailAction
Expand All @@ -23,51 +24,61 @@ final class ResendGiftCardEmailAction

private GiftCardRepositoryInterface $giftCardRepository;

private FlashBagInterface $flashBag;

private UrlGeneratorInterface $router;

private RequestStack $requestStack;

public function __construct(
GiftCardEmailManagerInterface $giftCardEmailManager,
GiftCardRepositoryInterface $giftCardRepository,
FlashBagInterface $flashBag,
RequestStack $requestStack,
UrlGeneratorInterface $router,
) {
$this->giftCardEmailManager = $giftCardEmailManager;
$this->giftCardRepository = $giftCardRepository;
$this->flashBag = $flashBag;
$this->router = $router;
$this->requestStack = $requestStack;
}

public function __invoke(Request $request, int $id): Response
{
$giftCard = $this->giftCardRepository->find($id);
if (!$giftCard instanceof GiftCardInterface) {
$this->flashBag->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.not_found',
'parameters' => ['%id%' => $id],
]);
$session = $this->requestStack->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.not_found',
'parameters' => ['%id%' => $id],
]);
}

return new RedirectResponse($this->getRedirectRoute($request));
}
$session = $this->requestStack->getSession();

if ($giftCard->getOrder() instanceof OrderInterface) {
$this->giftCardEmailManager->sendEmailWithGiftCardsFromOrder($giftCard->getOrder(), [$giftCard]);
$this->flashBag->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
}
} elseif ($giftCard->getCustomer() instanceof CustomerInterface) {
$this->giftCardEmailManager->sendEmailToCustomerWithGiftCard($giftCard->getCustomer(), $giftCard);
$this->flashBag->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
}
} else {
$this->flashBag->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.impossible_to_resend_email',
'parameters' => ['%id%' => $id],
]);
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.impossible_to_resend_email',
'parameters' => ['%id%' => $id],
]);
}
}

return new RedirectResponse($this->getRedirectRoute($request));
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<argument type="service" id="fos_rest.view_handler.default"/>
<argument type="service" id="form.factory"/>
<argument type="service" id="sylius.context.cart"/>
<argument type="service" id="session.flash_bag"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="setono_sylius_gift_card.applicator.gift_card"/>
<argument type="service" id="setono_sylius_gift_card.resolver.redirect_url"/>
<argument type="service" id="twig"/>
Expand All @@ -21,7 +21,7 @@
<service id="setono_sylius_gift_card.controller.action.remove_gift_card_from_order"
class="Setono\SyliusGiftCardPlugin\Controller\Action\RemoveGiftCardFromOrderAction">
<argument type="service" id="sylius.context.cart"/>
<argument type="service" id="session.flash_bag"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="setono_sylius_gift_card.applicator.gift_card"/>
<argument type="service" id="setono_sylius_gift_card.resolver.redirect_url"/>
</service>
Expand All @@ -44,7 +44,7 @@
class="Setono\SyliusGiftCardPlugin\Controller\Action\DownloadGiftCardPdfAction">
<argument type="service" id="setono_sylius_gift_card.repository.gift_card"/>
<argument type="service" id="security.authorization_checker"/>
<argument type="service" id="session.flash_bag"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="setono_sylius_gift_card.provider.gift_card_configuration"/>
<argument type="service" id="twig"/>
<argument type="service" id="knp_snappy.pdf"/>
Expand All @@ -55,7 +55,7 @@
class="Setono\SyliusGiftCardPlugin\Controller\Action\ResendGiftCardEmailAction">
<argument type="service" id="setono_sylius_gift_card.email_manager.gift_card_order"/>
<argument type="service" id="setono_sylius_gift_card.repository.gift_card"/>
<argument type="service" id="session.flash_bag"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="router"/>
</service>

Expand Down

0 comments on commit 1c60adf

Please sign in to comment.