-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathStorePathInfoValidator.php
137 lines (120 loc) · 3.59 KB
/
StorePathInfoValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Copyright 2011 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\Store\App\Request;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\Request\PathInfo;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
use Magento\Framework\App\Area;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreIsInactiveException;
use Magento\Store\Model\Validation\StoreCodeValidator;
/**
* Gets the store from the path if valid
*/
class StorePathInfoValidator implements ResetAfterRequestInterface
{
/**
* Store Config
*
* @var ScopeConfigInterface
*/
private $config;
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;
/**
* @var PathInfo
*/
private $pathInfo;
/**
* @var StoreCodeValidator
*/
private $storeCodeValidator;
/**
* @var array
*/
private array $validatedStoreCodes = [];
/**
* @param ScopeConfigInterface $config
* @param StoreRepositoryInterface $storeRepository
* @param PathInfo $pathInfo
* @param StoreCodeValidator $storeCodeValidator
*/
public function __construct(
ScopeConfigInterface $config,
StoreRepositoryInterface $storeRepository,
PathInfo $pathInfo,
StoreCodeValidator $storeCodeValidator
) {
$this->config = $config;
$this->storeRepository = $storeRepository;
$this->pathInfo = $pathInfo;
$this->storeCodeValidator = $storeCodeValidator;
}
/**
* Get store code from path info validate if config value. If path info is empty the try to calculate from request.
*
* @param Http $request
* @param string $pathInfo
* @return string|null
*/
public function getValidStoreCode(Http $request, string $pathInfo = '') : ?string
{
$useStoreCodeInUrl = (bool) $this->config->getValue(Store::XML_PATH_STORE_IN_URL);
if (!$useStoreCodeInUrl) {
return null;
}
if (empty($pathInfo)) {
$pathInfo = $this->pathInfo->getPathInfo($request->getRequestUri(), $request->getBaseUrl());
}
$storeCode = $this->getStoreCode($pathInfo);
if (
empty($storeCode)
|| $storeCode === Area::AREA_GRAPHQL
|| $storeCode === Store::ADMIN_CODE
|| !$this->storeCodeValidator->isValid($storeCode)
) {
return null;
}
if (array_key_exists($storeCode, $this->validatedStoreCodes)) {
return $this->validatedStoreCodes[$storeCode];
}
try {
$this->storeRepository->getActiveStoreByCode($storeCode);
$this->validatedStoreCodes[$storeCode] = $storeCode;
return $storeCode;
} catch (NoSuchEntityException $e) {
$this->validatedStoreCodes[$storeCode] = null;
return null;
} catch (StoreIsInactiveException $e) {
$this->validatedStoreCodes[$storeCode] = null;
return null;
}
}
/**
* Get store code from path info string
*
* @param string $pathInfo
* @return string
*/
private function getStoreCode(string $pathInfo) : string
{
$pathParts = explode('/', ltrim($pathInfo, '/'), 2);
return current($pathParts);
}
/**
* @inheritDoc
*/
public function _resetState(): void
{
$this->validatedStoreCodes = [];
}
}