CachedCategoryResolver::resolveById()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Tkotosz\CatalogRouter\Model\Service\CategoryResolver;
4
5
use Tkotosz\CatalogRouter\Api\CacheInterface;
6
use Tkotosz\CatalogRouter\Api\CategoryResolverInterface;
7
use Tkotosz\CatalogRouter\Model\EntityData;
8
9
class CachedCategoryResolver implements CategoryResolverInterface
10
{
11
    const CACHE_KEY_RESOLVE_BY_URL_KEY = 'resolve_category_by_ulkey';
12
    const CACHE_KEY_RESOLVE_BY_ID = 'resolve_category_by_id';
13
    const CACHE_KEY_RESOLVE_PARENT_IDS = 'resolve_category_parent_ids';
14
15
    /**
16
     * @var CategoryResolverInterface
17
     */
18
    private $categoryResolver;
19
    
20
    /**
21
     * @var CacheInterface
22
     */
23
    private $cache;
24
    
25
    /**
26
     * @param CategoryResolverInterface $categoryResolver
27
     * @param CacheInterface            $cache
28
     */
29
    public function __construct(CategoryResolverInterface $categoryResolver, CacheInterface $cache)
30
    {
31
        $this->categoryResolver = $categoryResolver;
32
        $this->cache = $cache;
33
    }
34
    
35
    /**
36
     * @param string $urlKey
37
     * @param int    $storeId
38
     * @param int    $parentId
39
     *
40
     * @return EntityData
41
     */
42
    public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : EntityData
43
    {
44
        $cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_BY_URL_KEY, $urlKey, $storeId, $parentId]);
45
46
        if (!$this->cache->has($cacheKey)) {
47
            $data = $this->categoryResolver->resolveByUrlKey($urlKey, $storeId, $parentId);
48
            $this->cache->set($cacheKey, $data);
49
        }
50
        
51
        return $this->cache->get($cacheKey);
52
    }
53
54
    public function resolveAllByUrlKey(string $urlKey, int $storeId, int $parentId) : array
55
    {
56
        return $this->categoryResolver->resolveAllByUrlKey($urlKey, $storeId, $parentId);
57
    }
58
59
    /**
60
     * @param int $categoryId
61
     * @param int $storeId
62
     *
63
     * @return EntityData
64
     */
65
    public function resolveById(int $categoryId, int $storeId) : EntityData
66
    {
67
        $cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_BY_ID, $storeId, $categoryId]);
68
69
        if (!$this->cache->has($cacheKey)) {
70
            $data = $this->categoryResolver->resolveById($categoryId, $storeId);
71
            $this->cache->set($cacheKey, $data);
72
        }
73
        
74
        return $this->cache->get($cacheKey);
75
    }
76
77
    /**
78
     * @param int $categoryId
79
     * @param int $storeId
80
     *
81
     * @return int[]
82
     */
83
    public function resolveParentIds(int $categoryId, int $storeId) : array
84
    {
85
        $cacheKey = implode('_', [self::CACHE_KEY_RESOLVE_PARENT_IDS, $categoryId, $storeId]);
86
87
        if (!$this->cache->has($cacheKey)) {
88
            $data = $this->categoryResolver->resolveParentIds($categoryId, $storeId);
89
            $this->cache->set($cacheKey, $data);
90
        }
91
        
92
        return $this->cache->get($cacheKey);
93
    }
94
}
95