These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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.