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

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