Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StorePathInfoValidator.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 
17 {
23  private $config;
24 
28  private $storeRepository;
29 
33  private $pathInfo;
34 
40  public function __construct(
41  \Magento\Framework\App\Config\ReinitableConfigInterface $config,
42  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
43  \Magento\Framework\App\Request\PathInfo $pathInfo
44  ) {
45  $this->config = $config;
46  $this->storeRepository = $storeRepository;
47  $this->pathInfo = $pathInfo;
48  }
49 
57  public function getValidStoreCode(
58  \Magento\Framework\App\Request\Http $request,
59  string $pathInfo = ''
60  ) : ?string {
61  if (empty($pathInfo)) {
62  $pathInfo = $this->pathInfo->getPathInfo(
63  $request->getRequestUri(),
64  $request->getBaseUrl()
65  );
66  }
67  $storeCode = $this->getStoreCode($pathInfo);
68  if (!empty($storeCode)
70  && (bool)$this->config->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL)
71  ) {
72  try {
73  $this->storeRepository->getActiveStoreByCode($storeCode);
74 
75  if ((bool)$this->config->getValue(
79  )) {
80  return $storeCode;
81  }
82  } catch (NoSuchEntityException $e) {
83  //return null;
84  } catch (\Magento\Store\Model\StoreIsInactiveException $e) {
85  //return null;
86  }
87  }
88  return null;
89  }
90 
97  private function getStoreCode(string $pathInfo) : string
98  {
99  $pathParts = explode('/', ltrim($pathInfo, '/'), 2);
100  return current($pathParts);
101  }
102 }
getValidStoreCode(\Magento\Framework\App\Request\Http $request, string $pathInfo='')
$config
Definition: fraud_order.php:17
$storeCode
Definition: indexer.php:15
__construct(\Magento\Framework\App\Config\ReinitableConfigInterface $config, \Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Framework\App\Request\PathInfo $pathInfo)