1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tkotosz\CatalogRouter\Model\Service\ProductResolver; |
4
|
|
|
|
5
|
|
|
use Tkotosz\CatalogRouter\Api\CacheInterface; |
6
|
|
|
use Tkotosz\CatalogRouter\Api\ProductResolverInterface; |
7
|
|
|
use Tkotosz\CatalogRouter\Model\EntityData; |
8
|
|
|
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException; |
9
|
|
|
use Magento\Catalog\Model\ResourceModel\ProductFactory; |
10
|
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; |
11
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
12
|
|
|
|
13
|
|
|
class CachedProductResolver implements ProductResolverInterface |
14
|
|
|
{ |
15
|
|
|
const CACHE_KEY_RESOLVE_BY_URL_KEY = 'resolve_product_by_ulkey'; |
16
|
|
|
const CACHE_KEY_RESOLVE_BY_ID = 'resolve_product_by_id'; |
17
|
|
|
const CACHE_KEY_RESOLVE_CATEGORIES = 'resolve_product_categories'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ProductResolverInterface |
21
|
|
|
*/ |
22
|
|
|
private $productResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var CacheInterface |
26
|
|
|
*/ |
27
|
|
|
private $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ProductResolverInterface $productResolver |
31
|
|
|
* @param CacheInterface $cache |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ProductResolverInterface $productResolver, CacheInterface $cache) |
34
|
|
|
{ |
35
|
|
|
$this->productResolver = $productResolver; |
36
|
|
|
$this->cache = $cache; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $urlKey |
41
|
|
|
* @param int $storeId |
42
|
|
|
* |
43
|
|
|
* @return EntityData |
44
|
|
|
*/ |
45
|
|
|
public function resolveByUrlKey(string $urlKey, int $storeId) : EntityData |
46
|
|
|
{ |
47
|
|
|
$cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_BY_URL_KEY, $urlKey, $storeId]); |
48
|
|
|
|
49
|
|
|
if (!$this->cache->has($cacheKey)) { |
50
|
|
|
$data = $this->productResolver->resolveByUrlKey($urlKey, $storeId); |
51
|
|
|
$this->cache->set($cacheKey, $data); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->cache->get($cacheKey); |
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
|
|
|
return $this->productResolver->resolveAllByUrlKey($urlKey, $storeId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $productId |
70
|
|
|
* @param int $storeId |
71
|
|
|
* |
72
|
|
|
* @return EntityData |
73
|
|
|
*/ |
74
|
|
|
public function resolveById(int $productId, int $storeId) : EntityData |
75
|
|
|
{ |
76
|
|
|
$cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_BY_ID, $productId, $storeId]); |
77
|
|
|
|
78
|
|
|
if (!$this->cache->has($cacheKey)) { |
79
|
|
|
$data = $this->productResolver->resolveById($productId, $storeId); |
80
|
|
|
$this->cache->set($cacheKey, $data); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->cache->get($cacheKey); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $productId |
88
|
|
|
* @param int $storeId |
89
|
|
|
* |
90
|
|
|
* @return int[] |
91
|
|
|
*/ |
92
|
|
|
public function resolveCategoryIds(int $productId, int $storeId) : array |
93
|
|
|
{ |
94
|
|
|
$cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_CATEGORIES, $productId, $storeId]); |
95
|
|
|
|
96
|
|
|
if (!$this->cache->has($cacheKey)) { |
97
|
|
|
$data = $this->productResolver->resolveCategoryIds($productId, $storeId); |
98
|
|
|
$this->cache->set($cacheKey, $data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->cache->get($cacheKey); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|