Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit ebb266f

Browse files
committed
Merge branch 'MAGETWO-70332' into 2.3-develop-pr9
2 parents efe1e4e + df87cda commit ebb266f

File tree

3 files changed

+229
-57
lines changed

3 files changed

+229
-57
lines changed

app/code/Magento/Store/App/Action/Plugin/Context.php

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
namespace Magento\Store\App\Action\Plugin;
88

99
use Magento\Framework\App\Http\Context as HttpContext;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Exception\NotFoundException;
1012
use Magento\Framework\Phrase;
13+
use Magento\Store\Api\Data\StoreInterface;
1114
use Magento\Store\Api\StoreCookieManagerInterface;
1215
use Magento\Store\Api\StoreResolverInterface;
1316
use Magento\Store\Model\StoreManagerInterface;
@@ -58,42 +61,102 @@ public function __construct(
5861
}
5962

6063
/**
61-
* Set store and currency to http context
64+
* Set store and currency to http context.
6265
*
6366
* @param AbstractAction $subject
6467
* @param RequestInterface $request
6568
* @return void
6669
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6770
*/
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+
}
7280

81+
/** @var string|array|null $storeCode */
7382
$storeCode = $request->getParam(
7483
StoreResolverInterface::PARAM_NAME,
7584
$this->storeCookieManager->getStoreCodeFromCookie()
7685
);
77-
7886
if (is_array($storeCode)) {
7987
if (!isset($storeCode['_data']['code'])) {
80-
throw new \InvalidArgumentException(new Phrase('Invalid store parameter.'));
88+
$this->processInvalidStoreRequested();
8189
}
8290
$storeCode = $storeCode['_data']['code'];
8391
}
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+
* @return void
111+
* @throws NotFoundException
112+
*/
113+
private function processInvalidStoreRequested(
114+
\Throwable $previousException = null
115+
) {
116+
$store = $this->storeManager->getStore();
117+
$this->updateContext($store);
86118

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+
* @return void
132+
*/
133+
private function updateContext(StoreInterface $store)
134+
{
87135
$this->httpContext->setValue(
88136
StoreManagerInterface::CONTEXT_STORE,
89-
$currentStore->getCode(),
137+
$store->getCode(),
90138
$this->storeManager->getDefaultStoreView()->getCode()
91139
);
92140

141+
/** @var StoreInterface $defaultStore */
142+
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
93143
$this->httpContext->setValue(
94144
HttpContext::CONTEXT_CURRENCY,
95-
$this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(),
145+
$this->session->getCurrencyCode()
146+
?: $store->getDefaultCurrencyCode(),
96147
$defaultStore->getDefaultCurrencyCode()
97148
);
98149
}
150+
151+
/**
152+
* Check if there is a need to find the current store.
153+
*
154+
* @return bool
155+
*/
156+
private function isAlreadySet(): bool
157+
{
158+
$storeKey = StoreManagerInterface::CONTEXT_STORE;
159+
160+
return $this->httpContext->getValue($storeKey) !== null;
161+
}
99162
}

app/code/Magento/Store/Model/StoreManagerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Store\Model;
88

9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
911
/**
1012
* Store manager interface
1113
*
@@ -46,6 +48,7 @@ public function isSingleStoreMode();
4648
*
4749
* @param null|string|bool|int|\Magento\Store\Api\Data\StoreInterface $storeId
4850
* @return \Magento\Store\Api\Data\StoreInterface
51+
* @throws NoSuchEntityException If given store doesn't exist.
4952
*/
5053
public function getStore($storeId = null);
5154

0 commit comments

Comments
 (0)