StoreProcessor.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Controller\HttpHeaderProcessor;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  10. use Magento\GraphQl\Controller\HttpHeaderProcessorInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. /**
  13. * Process the "Store" header entry
  14. */
  15. class StoreProcessor implements HttpHeaderProcessorInterface
  16. {
  17. /**
  18. * @var StoreManagerInterface
  19. */
  20. private $storeManager;
  21. /**
  22. * StoreProcessor constructor.
  23. *
  24. * @param StoreManagerInterface $storeManager
  25. */
  26. public function __construct(StoreManagerInterface $storeManager)
  27. {
  28. $this->storeManager = $storeManager;
  29. }
  30. /**
  31. * Handle the value of the store and set the scope
  32. *
  33. * {@inheritDoc}
  34. * @throws NoSuchEntityException
  35. */
  36. public function processHeaderValue(string $headerValue) : void
  37. {
  38. if ($headerValue) {
  39. $storeCode = ltrim(rtrim($headerValue));
  40. $stores = $this->storeManager->getStores(false, true);
  41. if (isset($stores[$storeCode])) {
  42. $this->storeManager->setCurrentStore($storeCode);
  43. } elseif (strtolower($storeCode) !== 'default') {
  44. throw new GraphQlInputException(
  45. new \Magento\Framework\Phrase('Store code %1 does not exist', [$storeCode])
  46. );
  47. }
  48. }
  49. }
  50. }