StorePathInfoValidator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Store\App\Request;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Store\Model\Store;
  10. /**
  11. * Gets the store from the path if valid
  12. */
  13. class StorePathInfoValidator
  14. {
  15. /**
  16. * Store Config
  17. *
  18. * @var \Magento\Framework\App\Config\ReinitableConfigInterface
  19. */
  20. private $config;
  21. /**
  22. * @var \Magento\Store\Api\StoreRepositoryInterface
  23. */
  24. private $storeRepository;
  25. /**
  26. * @var \Magento\Framework\App\Request\PathInfo
  27. */
  28. private $pathInfo;
  29. /**
  30. * @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
  31. * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
  32. * @param \Magento\Framework\App\Request\PathInfo $pathInfo
  33. */
  34. public function __construct(
  35. \Magento\Framework\App\Config\ReinitableConfigInterface $config,
  36. \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
  37. \Magento\Framework\App\Request\PathInfo $pathInfo
  38. ) {
  39. $this->config = $config;
  40. $this->storeRepository = $storeRepository;
  41. $this->pathInfo = $pathInfo;
  42. }
  43. /**
  44. * Get store code from path info validate if config value. If path info is empty the try to calculate from request.
  45. *
  46. * @param \Magento\Framework\App\Request\Http $request
  47. * @param string $pathInfo
  48. * @return string|null
  49. */
  50. public function getValidStoreCode(
  51. \Magento\Framework\App\Request\Http $request,
  52. string $pathInfo = ''
  53. ) : ?string {
  54. if (empty($pathInfo)) {
  55. $pathInfo = $this->pathInfo->getPathInfo(
  56. $request->getRequestUri(),
  57. $request->getBaseUrl()
  58. );
  59. }
  60. $storeCode = $this->getStoreCode($pathInfo);
  61. if (!empty($storeCode)
  62. && $storeCode != Store::ADMIN_CODE
  63. && (bool)$this->config->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL)
  64. ) {
  65. try {
  66. $this->storeRepository->getActiveStoreByCode($storeCode);
  67. if ((bool)$this->config->getValue(
  68. \Magento\Store\Model\Store::XML_PATH_STORE_IN_URL,
  69. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  70. $storeCode
  71. )) {
  72. return $storeCode;
  73. }
  74. } catch (NoSuchEntityException $e) {
  75. //return null;
  76. } catch (\Magento\Store\Model\StoreIsInactiveException $e) {
  77. //return null;
  78. }
  79. }
  80. return null;
  81. }
  82. /**
  83. * Get store code from path info string
  84. *
  85. * @param string $pathInfo
  86. * @return string
  87. */
  88. private function getStoreCode(string $pathInfo) : string
  89. {
  90. $pathParts = explode('/', ltrim($pathInfo, '/'), 2);
  91. return current($pathParts);
  92. }
  93. }