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

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\Bundle\FrameworkBundle\Templating\EngineInterface;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Twig\Environment;
  22. final class CurrencySwitchController
  23. {
  24.     public function __construct(
  25.         private EngineInterface|Environment $templatingEngine,
  26.         private CurrencyContextInterface $currencyContext,
  27.         private CurrencyStorageInterface $currencyStorage,
  28.         private ChannelContextInterface $channelContext
  29.     ) {
  30.     }
  31.     public function renderAction(): Response
  32.     {
  33.         /** @var ChannelInterface $channel */
  34.         $channel $this->channelContext->getChannel();
  35.         $availableCurrencies array_map(
  36.             fn(CurrencyInterface $currency) => $currency->getCode(),
  37.             $channel->getCurrencies()->toArray()
  38.         );
  39.         return new Response($this->templatingEngine->render('@SyliusShop/Menu/_currencySwitch.html.twig', [
  40.             'active' => $this->currencyContext->getCurrencyCode(),
  41.             'currencies' => $availableCurrencies,
  42.         ]));
  43.     }
  44.     public function switchAction(Request $requeststring $code): Response
  45.     {
  46.         /** @var ChannelInterface $channel */
  47.         $channel $this->channelContext->getChannel();
  48.         $this->currencyStorage->set($channel$code);
  49.         return new RedirectResponse($request->headers->get('referer'$request->getSchemeAndHttpHost()));
  50.     }
  51. }