1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tkotosz\CatalogRouter\Model\Service\ProductResolver; |
4
|
|
|
|
5
|
|
|
use Tkotosz\CatalogRouter\Api\ProductResolverInterface; |
6
|
|
|
use Tkotosz\CatalogRouter\Model\EntityData; |
7
|
|
|
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException; |
8
|
|
|
use Magento\Catalog\Model\ResourceModel\ProductFactory; |
9
|
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; |
10
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
11
|
|
|
|
12
|
|
|
class ProductResolver implements ProductResolverInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ProductCollectionFactory |
16
|
|
|
*/ |
17
|
|
|
private $productCollectionFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ProductFactory |
21
|
|
|
*/ |
22
|
|
|
private $productFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ProductCollectionFactory $productCollectionFactory |
26
|
|
|
* @param ProductFactory $productFactory |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
ProductCollectionFactory $productCollectionFactory, |
30
|
|
|
ProductFactory $productFactory |
31
|
|
|
) { |
32
|
|
|
$this->productCollectionFactory = $productCollectionFactory; |
33
|
|
|
$this->productFactory = $productFactory; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $urlKey |
38
|
|
|
* @param int $storeId |
39
|
|
|
* |
40
|
|
|
* @return EntityData |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function resolveByUrlKey(string $urlKey, int $storeId) : EntityData |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$productId = $this->productCollectionFactory->create() |
45
|
|
|
->addStoreFilter($storeId) |
46
|
|
|
->addAttributeToFilter('url_key', $urlKey) |
47
|
|
|
->getFirstItem() |
48
|
|
|
->getId(); |
49
|
|
|
|
50
|
|
|
if (!$productId) { |
51
|
|
|
throw new EntityDataNotFoundException('Product does not exist'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new EntityData('product', $productId, $urlKey); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $urlKey |
59
|
|
|
* @param int $storeId |
60
|
|
|
* |
61
|
|
|
* @return EntityData[] |
62
|
|
|
*/ |
63
|
|
|
public function resolveAllByUrlKey(string $urlKey, int $storeId) : array |
64
|
|
|
{ |
65
|
|
|
$products = []; |
66
|
|
|
|
67
|
|
|
$productCollection = $this->productCollectionFactory->create() |
68
|
|
|
->addStoreFilter($storeId) |
69
|
|
|
->addAttributeToFilter('url_key', $urlKey); |
70
|
|
|
|
71
|
|
|
foreach ($productCollection as $product) { |
72
|
|
|
$products[] = new EntityData('product', $product->getId(), $urlKey); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $products; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $productId |
80
|
|
|
* @param int $storeId |
81
|
|
|
* |
82
|
|
|
* @return EntityData |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function resolveById(int $productId, int $storeId) : EntityData |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$urlKey = $this->productCollectionFactory->create() |
87
|
|
|
->addStoreFilter($storeId) |
88
|
|
|
->addAttributeToSelect('url_key') |
89
|
|
|
->addAttributeToFilter('entity_id', $productId) |
90
|
|
|
->getFirstItem() |
91
|
|
|
->getUrlKey(); |
92
|
|
|
|
93
|
|
|
if (!$urlKey) { |
94
|
|
|
throw new EntityDataNotFoundException('Product does not exist'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return new EntityData('product', $productId, $urlKey); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $productId |
102
|
|
|
* @param int $storeId |
103
|
|
|
* |
104
|
|
|
* @return int[] |
105
|
|
|
*/ |
106
|
|
|
public function resolveCategoryIds(int $productId, int $storeId) : array |
107
|
|
|
{ |
108
|
|
|
$productResource = $this->productFactory->create(); |
109
|
|
|
$connection = $productResource->getConnection(); |
110
|
|
|
|
111
|
|
|
$select = $connection->select() |
112
|
|
|
->distinct() |
113
|
|
|
->from($productResource->getTable('catalog_category_product_index'), ['category_id']) |
114
|
|
|
->where('store_id = ?', $storeId) |
115
|
|
|
->where('product_id = ?', $productId); |
116
|
|
|
|
117
|
|
|
return $connection->fetchCol($select); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.