1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tkotosz\CatalogRouter\Model\Service; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\UrlInterface; |
6
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
7
|
|
|
use Tkotosz\CatalogRouter\Api\CatalogUrlPathProviderInterface; |
8
|
|
|
use Tkotosz\CatalogRouter\Api\CatalogUrlProviderInterface; |
9
|
|
|
|
10
|
|
|
class CatalogUrlProvider implements CatalogUrlProviderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CatalogUrlPathProviderInterface |
14
|
|
|
*/ |
15
|
|
|
private $catalogUrlPathProvider; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var UrlInterface |
19
|
|
|
*/ |
20
|
|
|
private $urlProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var StoreManagerInterface |
24
|
|
|
*/ |
25
|
|
|
private $storeManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param CatalogUrlPathProviderInterface $catalogUrlPathProvider |
29
|
|
|
* @param UrlInterface $urlProvider |
30
|
|
|
* @param StoreManagerInterface $storeManager |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
CatalogUrlPathProviderInterface $catalogUrlPathProvider, |
34
|
|
|
UrlInterface $urlProvider, |
35
|
|
|
StoreManagerInterface $storeManager |
36
|
|
|
) { |
37
|
|
|
$this->catalogUrlPathProvider = $catalogUrlPathProvider; |
38
|
|
|
$this->urlProvider = $urlProvider; |
39
|
|
|
$this->storeManager = $storeManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $categoryId |
44
|
|
|
* @param int $storeId |
45
|
|
|
* @param array $params |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getCategoryUrl(int $categoryId, int $storeId, array $params = []) : string |
50
|
|
|
{ |
51
|
|
|
$urlPath = $this->catalogUrlPathProvider->getCategoryUrlPath($categoryId, $storeId); |
52
|
|
|
|
53
|
|
|
return $this->getUrl($urlPath->getFullPath(), $storeId, $params); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $productId |
58
|
|
|
* @param int $storeId |
59
|
|
|
* @param array $params |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getProductUrl(int $productId, int $storeId, array $params = []) : string |
64
|
|
|
{ |
65
|
|
|
$urlPath = $this->catalogUrlPathProvider->getProductUrlPath($productId, $storeId); |
66
|
|
|
|
67
|
|
|
return $this->getUrl($urlPath->getFullPath(), $storeId, $params); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $urlPath |
72
|
|
|
* @param int $storeId |
73
|
|
|
* @param array $params |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function getUrl(string $urlPath, int $storeId, array $params = []) : string |
78
|
|
|
{ |
79
|
|
|
if ($storeId != $this->storeManager->getStore()->getId()) { |
80
|
|
|
$params['_scope_to_url'] = true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->urlProvider->setScope($storeId)->getDirectUrl($urlPath, $params); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|