Failed Conditions
Pull Request — master (#1)
by Tibor
04:33 queued 01:50
created

CategoryUrlPathChecker::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Tkotosz\CatalogRouter\Model\Service\UrlPathUsedChecker;
4
5
use Magento\Store\Model\StoreManagerInterface;
6
use Tkotosz\CatalogRouter\Api\CategoryResolverInterface;
7
use Tkotosz\CatalogRouter\Api\ProductResolverInterface;
8
use Tkotosz\CatalogRouter\Api\UrlPathUsedChecker;
9
use Tkotosz\CatalogRouter\Model\UrlPath;
10
use Tkotosz\CatalogRouter\Model\EntityData;
11
12
class CategoryUrlPathChecker implements UrlPathUsedChecker
13
{
14
    /**
15
     * @var CategoryResolverInterface
16
     */
17
    private $categoryResolver;
18
    
19
    /**
20
     * @var StoreManagerInterface
21
     */
22
    private $storeManager;
23
    
24
    /**
25
     * @param CategoryResolverInterface $categoryResolver
26
     * @param StoreManagerInterface     $storeManager
27
     */
28
    public function __construct(CategoryResolverInterface $categoryResolver, StoreManagerInterface $storeManager)
29
    {
30
        $this->categoryResolver = $categoryResolver;
31
        $this->storeManager = $storeManager;
32
    }
33
    
34
    /**
35
     * @param UrlPath $urlPath
36
     * @param int     $storeId
37
     *
38
     * @return EntityData[]
39
     */
40
    public function check(UrlPath $urlPath, int $storeId) : array
41
    {
42
        $parentCategoryId = $this->resolveParentCategoryId($urlPath, $storeId);
43
44
        return $this->categoryResolver->resolveAllByUrlKey($urlPath->getLastPart(), $storeId, $parentCategoryId);
45
    }
46
47
    /**
48
     * @param UrlPath $urlPath
49
     * @param int     $storeId
50
     *
51
     * @return int
52
     */
53 View Code Duplication
    private function resolveParentCategoryId(UrlPath $urlPath, int $storeId) : int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
    {
55
        $parentCategoryId = $this->storeManager->getStore($storeId)->getRootCategoryId();
56
57
        foreach ($urlPath->getBeginningParts() as $urlKey) {
58
            $parentCategoryId = $this->categoryResolver->resolveByUrlKey($urlKey, $storeId, $parentCategoryId)->getId();
59
        }
60
61
        return $parentCategoryId;
62
    }
63
}
64