|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * A Magento 2 module named Experius MultipleWebsiteStoreCodeUrl |
| 4 | + * Copyright (C) 2017 Experius |
| 5 | + * |
| 6 | + * This file is part of Experius MultipleWebsiteStoreCodeUrl. |
| 7 | + * |
| 8 | + * Experius MultipleWebsiteStoreCodeUrl is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace Experius\MultipleWebsiteStoreCodeUrl\Helper; |
| 23 | + |
| 24 | +use Magento\Framework\App\Helper\AbstractHelper; |
| 25 | +use Magento\Store\Model\StoreManagerInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class Data |
| 29 | + * @package Experius\MultipleWebsiteStoreCodeUrl\Helper |
| 30 | + */ |
| 31 | +class Data extends AbstractHelper |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var StoreManagerInterface |
| 35 | + */ |
| 36 | + protected $storeManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * Data constructor. |
| 40 | + * @param StoreManagerInterface $storeManager |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + StoreManagerInterface $storeManager |
| 44 | + ) { |
| 45 | + $this->storeManager = $storeManager; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param string $websiteCode |
| 50 | + * @param string $path |
| 51 | + * @param bool $isUrl |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + public function setCorrectWebsiteCodeUrl($websiteCode, $path, $isUrl = false) |
| 55 | + { |
| 56 | + $element = ($isUrl) ? 3 : 0; |
| 57 | + $pathParts = explode('/', ltrim($path, '/'), 5); |
| 58 | + $storeCode = "{$websiteCode}_{$pathParts[$element]}"; |
| 59 | + |
| 60 | + if ($this->validateStore($storeCode)) { |
| 61 | + $pathParts[$element] = $storeCode; |
| 62 | + $path = implode('/', $pathParts); |
| 63 | + return $path; |
| 64 | + } |
| 65 | + //returns the original path if request havenot a valid store |
| 66 | + return $path; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param string $storeCode |
| 71 | + * @return bool |
| 72 | + */ |
| 73 | + public function validateStore($storeCode) |
| 74 | + { |
| 75 | + try { |
| 76 | + /** @var \Magento\Store\Api\Data\StoreInterface $store */ |
| 77 | + $this->storeManager->getStore($storeCode); |
| 78 | + return true; |
| 79 | + } catch (\Exception $e) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | + |
0 commit comments