vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/Controller/CurrencySwitchController.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\Controller;
  12. use Sylius\Component\Channel\Context\ChannelContextInterface;
  13. use Sylius\Component\Core\Currency\CurrencyStorageInterface;
  14. use Sylius\Component\Core\Model\ChannelInterface;
  15. use Sylius\Component\Currency\Context\CurrencyContextInterface;
  16. use Sylius\Component\Currency\Model\CurrencyInterface;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Templating\EngineInterface;
  22. use Twig\Environment;
  23. final class CurrencySwitchController
  24. {
  25. use RedirectTrait;
  26. public function __construct(
  27. private EngineInterface|Environment $templatingEngine,
  28. private CurrencyContextInterface $currencyContext,
  29. private CurrencyStorageInterface $currencyStorage,
  30. private ChannelContextInterface $channelContext,
  31. private ?RouterInterface $router = null,
  32. ) {
  33. }
  34. public function renderAction(): Response
  35. {
  36. /** @var ChannelInterface $channel */
  37. $channel = $this->channelContext->getChannel();
  38. $availableCurrencies = array_map(
  39. fn (CurrencyInterface $currency) => $currency->getCode(),
  40. $channel->getCurrencies()->toArray(),
  41. );
  42. return new Response($this->templatingEngine->render('@SyliusShop/Menu/_currencySwitch.html.twig', [
  43. 'active' => $this->currencyContext->getCurrencyCode(),
  44. 'currencies' => $availableCurrencies,
  45. ]));
  46. }
  47. public function switchAction(Request $request, string $code): Response
  48. {
  49. /** @var ChannelInterface $channel */
  50. $channel = $this->channelContext->getChannel();
  51. $this->currencyStorage->set($channel, $code);
  52. return new RedirectResponse($this->getRedirectUrl($request, $this->router));
  53. }
  54. }