|
7 | 7 | namespace Magento\Store\App\Action\Plugin;
|
8 | 8 |
|
9 | 9 | use Magento\Framework\App\Http\Context as HttpContext;
|
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Framework\Exception\NotFoundException; |
10 | 12 | use Magento\Framework\Phrase;
|
| 13 | +use Magento\Store\Api\Data\StoreInterface; |
11 | 14 | use Magento\Store\Api\StoreCookieManagerInterface;
|
12 | 15 | use Magento\Store\Api\StoreResolverInterface;
|
13 | 16 | use Magento\Store\Model\StoreManagerInterface;
|
@@ -65,35 +68,94 @@ public function __construct(
|
65 | 68 | * @return void
|
66 | 69 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
67 | 70 | */
|
68 |
| - public function beforeDispatch(AbstractAction $subject, RequestInterface $request) |
69 |
| - { |
70 |
| - /** @var \Magento\Store\Model\Store $defaultStore */ |
71 |
| - $defaultStore = $this->storeManager->getWebsite()->getDefaultStore(); |
| 71 | + public function beforeDispatch( |
| 72 | + AbstractAction $subject, |
| 73 | + RequestInterface $request |
| 74 | + ) { |
| 75 | + if ($this->isAlreadySet()) { |
| 76 | + //If required store related value were already set for |
| 77 | + //HTTP processors then just continuing as we were. |
| 78 | + return; |
| 79 | + } |
72 | 80 |
|
| 81 | + /** @var string|array|null $storeCode */ |
73 | 82 | $storeCode = $request->getParam(
|
74 | 83 | StoreResolverInterface::PARAM_NAME,
|
75 | 84 | $this->storeCookieManager->getStoreCodeFromCookie()
|
76 | 85 | );
|
77 |
| - |
78 | 86 | if (is_array($storeCode)) {
|
79 | 87 | if (!isset($storeCode['_data']['code'])) {
|
80 |
| - throw new \InvalidArgumentException(new Phrase('Invalid store parameter.')); |
| 88 | + $this->processInvalidStoreRequested(); |
81 | 89 | }
|
82 | 90 | $storeCode = $storeCode['_data']['code'];
|
83 | 91 | }
|
84 |
| - /** @var \Magento\Store\Model\Store $currentStore */ |
85 |
| - $currentStore = $storeCode ? $this->storeManager->getStore($storeCode) : $defaultStore; |
| 92 | + if ($storeCode === '') { |
| 93 | + //Empty code - is an invalid code and it was given explicitly |
| 94 | + //(the value would be null if the code wasn't found). |
| 95 | + $this->processInvalidStoreRequested(); |
| 96 | + } |
| 97 | + try { |
| 98 | + $currentStore = $this->storeManager->getStore($storeCode); |
| 99 | + } catch (NoSuchEntityException $exception) { |
| 100 | + $this->processInvalidStoreRequested($exception); |
| 101 | + } |
| 102 | + |
| 103 | + $this->updateContext($currentStore); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Take action in case of invalid store requested. |
| 108 | + * |
| 109 | + * @param \Throwable|null $previousException |
| 110 | + * |
| 111 | + * @throws NotFoundException |
| 112 | + */ |
| 113 | + private function processInvalidStoreRequested( |
| 114 | + \Throwable $previousException = null |
| 115 | + ) { |
| 116 | + $store = $this->storeManager->getStore(); |
| 117 | + $this->updateContext($store); |
86 | 118 |
|
| 119 | + throw new NotFoundException( |
| 120 | + $previousException |
| 121 | + ? __($previousException->getMessage()) |
| 122 | + : __('Invalid store requested.'), |
| 123 | + $previousException |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Update context accordingly to the store found. |
| 129 | + * |
| 130 | + * @param StoreInterface $store |
| 131 | + */ |
| 132 | + private function updateContext(StoreInterface $store) |
| 133 | + { |
87 | 134 | $this->httpContext->setValue(
|
88 | 135 | StoreManagerInterface::CONTEXT_STORE,
|
89 |
| - $currentStore->getCode(), |
| 136 | + $store->getCode(), |
90 | 137 | $this->storeManager->getDefaultStoreView()->getCode()
|
91 | 138 | );
|
92 | 139 |
|
| 140 | + /** @var StoreInterface $defaultStore */ |
| 141 | + $defaultStore = $this->storeManager->getWebsite()->getDefaultStore(); |
93 | 142 | $this->httpContext->setValue(
|
94 | 143 | HttpContext::CONTEXT_CURRENCY,
|
95 |
| - $this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(), |
| 144 | + $this->session->getCurrencyCode() |
| 145 | + ?: $store->getDefaultCurrencyCode(), |
96 | 146 | $defaultStore->getDefaultCurrencyCode()
|
97 | 147 | );
|
98 | 148 | }
|
| 149 | + |
| 150 | + /** |
| 151 | + * Check if there a need to find the current store. |
| 152 | + * |
| 153 | + * @return bool |
| 154 | + */ |
| 155 | + private function isAlreadySet(): bool |
| 156 | + { |
| 157 | + $storeKey = StoreManagerInterface::CONTEXT_STORE; |
| 158 | + |
| 159 | + return $this->httpContext->getValue($storeKey) !== null; |
| 160 | + } |
99 | 161 | }
|
0 commit comments