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

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