CategoryResolver::resolveById()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Tkotosz\CatalogRouter\Model\Service\CategoryResolver;
4
5
use Tkotosz\CatalogRouter\Api\CategoryResolverInterface;
6
use Tkotosz\CatalogRouter\Model\EntityData;
7
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException;
8
use Magento\Catalog\Model\Category;
9
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
10
use Magento\Store\Model\StoreManagerInterface;
11
12
class CategoryResolver implements CategoryResolverInterface
13
{
14
    /**
15
     * @var CategoryCollectionFactory
16
     */
17
    private $categoryCollectionFactory;
18
    
19
    /**
20
     * @var StoreManagerInterface
21
     */
22
    private $storeManager;
23
    
24
    /**
25
     * @param CategoryCollectionFactory $categoryCollectionFactory
26
     * @param StoreManagerInterface     $storeManager
27
     */
28
    public function __construct(
29
        CategoryCollectionFactory $categoryCollectionFactory,
30
        StoreManagerInterface $storeManager
31
    ) {
32
        $this->categoryCollectionFactory = $categoryCollectionFactory;
33
        $this->storeManager = $storeManager;
34
    }
35
    
36
    /**
37
     * @param string $urlKey
38
     * @param int    $storeId
39
     * @param int    $parentId
40
     *
41
     * @return EntityData
42
     */
43
    public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : EntityData
44
    {
45
        $categoryId = $this->categoryCollectionFactory->create()
46
            ->setStoreId($storeId)
47
            ->addAttributeToFilter('url_key', $urlKey)
48
            ->addFieldToFilter('parent_id', $parentId)
49
            ->getFirstItem()
50
            ->getId();
51
52
        if (!$categoryId) {
53
            throw new EntityDataNotFoundException('Category does not exist');
54
        }
55
56
        return new EntityData('category', $categoryId, $urlKey);
57
    }
58
59
    /**
60
     * @param string $urlKey
61
     * @param int    $storeId
62
     * @param int    $parentId
63
     *
64
     * @return EntityData[]
65
     */
66
    public function resolveAllByUrlKey(string $urlKey, int $storeId, int $parentId) : array
67
    {
68
        $categories = [];
69
        
70
        $categoryCollection = $this->categoryCollectionFactory->create()
71
            ->setStoreId($storeId)
72
            ->addAttributeToFilter('url_key', $urlKey)
73
            ->addFieldToFilter('parent_id', $parentId);
74
75
        foreach ($categoryCollection as $category) {
76
            $categories[] = new EntityData('category', $category->getId(), $urlKey);        
77
        }
78
79
        return $categories;
80
    }
81
82
    /**
83
     * @param int $categoryId
84
     * @param int $storeId
85
     *
86
     * @return EntityData
87
     */
88
    public function resolveById(int $categoryId, int $storeId) : EntityData
89
    {
90
        $urlKey = $this->categoryCollectionFactory->create()
91
            ->setStoreId($storeId)
92
            ->addAttributeToSelect('url_key')
93
            ->addFieldToFilter('entity_id', $categoryId)
94
            ->getFirstItem()
95
            ->getUrlKey();
96
97
        if (!$urlKey) {
98
            throw new EntityDataNotFoundException('Category does not exist');
99
        }
100
101
        return new EntityData('category', $categoryId, $urlKey);
102
    }
103
104
    /**
105
     * @param int $categoryId
106
     * @param int $storeId
107
     *
108
     * @return int[]
109
     */
110
    public function resolveParentIds(int $categoryId, int $storeId) : array
111
    {
112
        $idPath = $this->categoryCollectionFactory->create()
113
            ->addFieldToSelect('path')
114
            ->addFieldToFilter('entity_id', $categoryId)
115
            ->getFirstItem()
116
            ->getPath();
117
118
        if (!$idPath) {
119
            throw new EntityDataNotFoundException('Category does not exist');
120
        }
121
122
        $parents = explode('/', $idPath);
123
124
        $toIgnore = [
125
            Category::TREE_ROOT_ID,
126
            Category::ROOT_CATEGORY_ID,
127
            $this->storeManager->getStore($storeId)->getRootCategoryId(),
128
            $categoryId
129
        ];
130
        
131
        return array_diff($parents, $toIgnore);
132
    }
133
}
134