Test Failed
Pull Request — master (#1)
by Tibor
02:26
created

CategoryUrlPathChecker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 10
loc 52
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 6 1
A resolveParentCategoryId() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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);
0 ignored issues
show
Bug introduced by
The method resolveAllByUrlKey() does not exist on Tkotosz\CatalogRouter\Ap...tegoryResolverInterface. Did you maybe mean resolveByUrlKey()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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