Skip to content

Commit 87bc610

Browse files
committed
Merge branch 'ACP2E-3447' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-11-14-2024
2 parents a784332 + cc61e88 commit 87bc610

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

app/code/Magento/Store/App/Request/StorePathInfoValidator.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\Store\App\Request;
@@ -11,6 +12,7 @@
1112
use Magento\Framework\App\Request\Http;
1213
use Magento\Framework\App\Request\PathInfo;
1314
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1416
use Magento\Store\Api\StoreRepositoryInterface;
1517
use Magento\Store\Model\Store;
1618
use Magento\Store\Model\StoreIsInactiveException;
@@ -19,7 +21,7 @@
1921
/**
2022
* Gets the store from the path if valid
2123
*/
22-
class StorePathInfoValidator
24+
class StorePathInfoValidator implements ResetAfterRequestInterface
2325
{
2426
/**
2527
* Store Config
@@ -43,6 +45,11 @@ class StorePathInfoValidator
4345
*/
4446
private $storeCodeValidator;
4547

48+
/**
49+
* @var array
50+
*/
51+
private array $validatedStoreCodes = [];
52+
4653
/**
4754
* @param ScopeConfigInterface $config
4855
* @param StoreRepositoryInterface $storeRepository
@@ -79,17 +86,25 @@ public function getValidStoreCode(Http $request, string $pathInfo = '') : ?strin
7986
$pathInfo = $this->pathInfo->getPathInfo($request->getRequestUri(), $request->getBaseUrl());
8087
}
8188
$storeCode = $this->getStoreCode($pathInfo);
89+
8290
if (empty($storeCode) || $storeCode === Store::ADMIN_CODE || !$this->storeCodeValidator->isValid($storeCode)) {
8391
return null;
8492
}
8593

94+
if (array_key_exists($storeCode, $this->validatedStoreCodes)) {
95+
return $this->validatedStoreCodes[$storeCode];
96+
}
97+
8698
try {
8799
$this->storeRepository->getActiveStoreByCode($storeCode);
88100

101+
$this->validatedStoreCodes[$storeCode] = $storeCode;
89102
return $storeCode;
90103
} catch (NoSuchEntityException $e) {
104+
$this->validatedStoreCodes[$storeCode] = null;
91105
return null;
92106
} catch (StoreIsInactiveException $e) {
107+
$this->validatedStoreCodes[$storeCode] = null;
93108
return null;
94109
}
95110
}
@@ -105,4 +120,12 @@ private function getStoreCode(string $pathInfo) : string
105120
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
106121
return current($pathParts);
107122
}
123+
124+
/**
125+
* @inheritDoc
126+
*/
127+
public function _resetState(): void
128+
{
129+
$this->validatedStoreCodes = [];
130+
}
108131
}

app/code/Magento/Store/Test/Unit/App/Request/StorePathInfoValidatorTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\Store\Test\Unit\App\Request;
@@ -183,6 +184,36 @@ public function testGetValidStoreCode(string $pathInfo, bool $isStoreCodeValid,
183184
$this->assertEquals($expectedResult, $result);
184185
}
185186

187+
/**
188+
* @dataProvider getValidStoreCodeDataProvider
189+
* @param string $pathInfo
190+
* @param bool $isStoreCodeValid
191+
* @param string|null $expectedResult
192+
*/
193+
public function testGetValidStoreCodeResultIsCached(
194+
string $pathInfo,
195+
bool $isStoreCodeValid,
196+
?string $expectedResult
197+
): void {
198+
$this->configMock->method('getValue')
199+
->with(Store::XML_PATH_STORE_IN_URL)
200+
->willReturn(true);
201+
$this->pathInfoMock->method('getPathInfo')
202+
->willReturn('/store2/path2/');
203+
$this->storeCodeValidatorMock->method('isValid')
204+
->willReturn($isStoreCodeValid);
205+
$store = $this->createMock(Store::class);
206+
$this->storeRepositoryMock
207+
->expects($expectedResult ? $this->once() : $this->never())
208+
->method('getActiveStoreByCode')
209+
->willReturn($store);
210+
211+
$result1 = $this->storePathInfoValidator->getValidStoreCode($this->requestMock, $pathInfo);
212+
$result2 = $this->storePathInfoValidator->getValidStoreCode($this->requestMock, $pathInfo);
213+
$this->assertEquals($expectedResult, $result1);
214+
$this->assertEquals($result1, $result2);
215+
}
216+
186217
public static function getValidStoreCodeDataProvider(): array
187218
{
188219
return [

0 commit comments

Comments
 (0)