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

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