|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © OpenGento, All rights reserved. |
| 4 | + */ |
| 5 | +declare(strict_types=1); |
| 6 | + |
| 7 | +namespace Opengento\StorePathUrl\Model; |
| 8 | + |
| 9 | +use Magento\Directory\Helper\Data; |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\Serialize\SerializerInterface; |
| 12 | +use Magento\Store\Api\Data\StoreInterface; |
| 13 | +use Magento\Store\Model\ScopeInterface; |
| 14 | +use Magento\Store\Model\Store; |
| 15 | +use Opengento\StorePathUrl\Model\Config\PathType; |
| 16 | + |
| 17 | +class Config |
| 18 | +{ |
| 19 | + private const CONFIG_PATH_USE_STORE_PATH = 'web/url/use_store_path'; |
| 20 | + private const CONFIG_PATH_CUSTOM_PATH_MAPPER = 'web/url/custom_path_mapper'; |
| 21 | + |
| 22 | + private ?array $customPathMapper = null; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private ScopeConfigInterface $scopeConfig, |
| 26 | + private SerializerInterface $serializer |
| 27 | + ) {} |
| 28 | + |
| 29 | + public function isEnabled(): bool |
| 30 | + { |
| 31 | + return $this->scopeConfig->isSetFlag(Store::XML_PATH_STORE_IN_URL) |
| 32 | + && $this->getStorePathType() !== PathType::StoreCode; |
| 33 | + } |
| 34 | + |
| 35 | + public function getStorePathType(): PathType |
| 36 | + { |
| 37 | + return PathType::from($this->scopeConfig->getValue(self::CONFIG_PATH_USE_STORE_PATH)); |
| 38 | + } |
| 39 | + |
| 40 | + public function getCountry(StoreInterface $store): string |
| 41 | + { |
| 42 | + return (string)$this->scopeConfig->getValue( |
| 43 | + Data::XML_PATH_DEFAULT_COUNTRY, |
| 44 | + ScopeInterface::SCOPE_STORE, |
| 45 | + $store->getId() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function getLocale(StoreInterface $store): string |
| 50 | + { |
| 51 | + return (string)$this->scopeConfig->getValue( |
| 52 | + Data::XML_PATH_DEFAULT_LOCALE, |
| 53 | + ScopeInterface::SCOPE_STORE, |
| 54 | + $store->getId() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function getCustomPathMapper(): array |
| 59 | + { |
| 60 | + return $this->customPathMapper ??= $this->resolveCustomPathMapper(); |
| 61 | + } |
| 62 | + |
| 63 | + private function resolveCustomPathMapper(): array |
| 64 | + { |
| 65 | + $customPaths = $this->serializer->unserialize( |
| 66 | + $this->scopeConfig->getValue(self::CONFIG_PATH_CUSTOM_PATH_MAPPER) ?? '{}' |
| 67 | + ); |
| 68 | + |
| 69 | + $mapper = []; |
| 70 | + foreach ($customPaths as $customPath) { |
| 71 | + if (isset($customPath['store'], $customPath['path'])) { |
| 72 | + $mapper[(int)$customPath['store']] = (string)$customPath['path']; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return $mapper; |
| 77 | + } |
| 78 | +} |
0 commit comments