1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tkotosz\CatalogRouter\Model\Service; |
4
|
|
|
|
5
|
|
|
use Tkotosz\CatalogRouter\Api\CategoryResolverInterface; |
6
|
|
|
use Tkotosz\CatalogRouter\Api\ProductResolverInterface; |
7
|
|
|
use Tkotosz\CatalogRouter\Model\EntityData; |
8
|
|
|
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException; |
9
|
|
|
use Tkotosz\CatalogRouter\Model\UrlPath; |
10
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
11
|
|
|
|
12
|
|
|
class CatalogUrlPathResolver |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var CategoryResolverInterface |
16
|
|
|
*/ |
17
|
|
|
private $categoryResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ProductResolverInterface |
21
|
|
|
*/ |
22
|
|
|
private $productResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var StoreManagerInterface |
26
|
|
|
*/ |
27
|
|
|
private $storeManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param CategoryResolverInterface $categoryResolver |
31
|
|
|
* @param ProductResolverInterface $productResolver |
32
|
|
|
* @param StoreManagerInterface $storeManager |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
CategoryResolverInterface $categoryResolver, |
36
|
|
|
ProductResolverInterface $productResolver, |
37
|
|
|
StoreManagerInterface $storeManager |
38
|
|
|
) { |
39
|
|
|
$this->categoryResolver = $categoryResolver; |
40
|
|
|
$this->productResolver = $productResolver; |
41
|
|
|
$this->storeManager = $storeManager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param UrlPath $urlPath |
46
|
|
|
* @param int $storeId |
47
|
|
|
* |
48
|
|
|
* @return EntityData |
49
|
|
|
*/ |
50
|
|
|
public function resolve(UrlPath $urlPath, int $storeId) : EntityData |
51
|
|
|
{ |
52
|
|
|
$parentCategoryId = $this->resolveParentCategoryId($urlPath, $storeId); |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
$entity = $this->resolveCategory($urlPath->getLastPart(), $storeId, $parentCategoryId); |
56
|
|
|
} catch (EntityDataNotFoundException $e) { |
57
|
|
|
$entity = $this->resolveProductInCategory($urlPath->getLastPart(), $storeId, $parentCategoryId); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $entity; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param UrlPath $urlPath |
65
|
|
|
* @param int $storeId |
66
|
|
|
* |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
|
|
public function resolveParentCategoryId(UrlPath $urlPath, int $storeId) : int |
70
|
|
|
{ |
71
|
|
|
$parentCategoryId = $this->storeManager->getStore($storeId)->getRootCategoryId(); |
72
|
|
|
|
73
|
|
|
foreach ($urlPath->getBeginningParts() as $urlKey) { |
74
|
|
|
$parentCategoryId = $this->resolveCategory($urlKey, $storeId, $parentCategoryId)->getId(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $parentCategoryId; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $urlKey |
82
|
|
|
* @param int $storeId |
83
|
|
|
* @param int $parentCategoryId |
84
|
|
|
* |
85
|
|
|
* @return EntityData |
86
|
|
|
*/ |
87
|
|
|
private function resolveCategory(string $urlKey, int $storeId, int $parentCategoryId) : EntityData |
88
|
|
|
{ |
89
|
|
|
return $this->categoryResolver->resolveByUrlKey($urlKey, $storeId, $parentCategoryId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $urlKey |
94
|
|
|
* @param int $storeId |
95
|
|
|
* @param int $categoryId |
96
|
|
|
* |
97
|
|
|
* @return EntityData |
98
|
|
|
*/ |
99
|
|
|
private function resolveProductInCategory(string $urlKey, int $storeId, int $categoryId) : EntityData |
100
|
|
|
{ |
101
|
|
|
$product = $this->productResolver->resolveByUrlKey($urlKey, $storeId); |
102
|
|
|
|
103
|
|
|
$productCategoryIds = $this->productResolver->resolveCategoryIds($product->getId(), $storeId); |
104
|
|
|
|
105
|
|
|
if (!in_array($categoryId, $productCategoryIds)) { |
106
|
|
|
throw new EntityDataNotFoundException('product is not found in the given category'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $product; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|